import sys

T = int(sys.stdin.readline())
for _ in range(T):
    n = int(sys.stdin.readline())
    boy_types = []
    for _ in range(n):
        boy_types.append(tuple(map(int, sys.stdin.readline().split())))
    
    max_sum = 0
    seen = []
    for i in range(n):
        a, b, c = boy_types[i]
        max_sum = c if c > max_sum else max_sum
        f = lambda x: x != (a, b, c) and (x[0] in (a, b) or x[1] in (a, b))
        candidates = tuple(filter(f, boy_types))
        for candidate in candidates:
            pair = ((a, b, c), candidate)
            if pair in seen or pair[::-1] in seen:
                continue
            a2, b2, c2 = candidate
            common = a if a in [a2, b2] else b
            rest = [a, b, a2, b2]
            rest.remove(common)
            rest.remove(common)
            sum_ = c + c2
            max_sum = sum_ if sum_ > max_sum else max_sum
            
            f1 = lambda x: x[0] in rest and x[1] in rest
            candidates2type1 = tuple(filter(f1, candidates))
            max_ = max(candidates2type1, key=lambda x: x[2])[2] if candidates2type1 else 0
            sum_ = c + c2 + max_
            max_sum = sum_ if sum_ > max_sum else max_sum
            
            f2 = lambda x: x not in ((a, b, c), candidate) and common in x[:2]
            candidates2type2 = tuple(filter(f2, candidates))
            sum_ = c + c2 + sum(map(lambda x: x[2], candidates2type2))
            max_sum = sum_ if sum_ > max_sum else max_sum
            
            seen.append(((a, b, c), candidate))
    print(max_sum)