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
    for i in range(n):
        a, b, c = boy_types[i]
        max_sum = c if c > max_sum else max_sum
        candidates = []
        for triplet in boy_types[i+1:]:
            if triplet[0] in (a, b) or triplet[1] in (a, b):
                candidates.append(triplet)
        for candidate in candidates:
            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
            
            candidate2_type1 = None
            for triplet in candidates:
                if triplet[0] in rest and triplet[1] in rest:
                    sum_ = c + c2 + triplet[2]
                    max_sum = sum_ if sum_ > max_sum else max_sum
                    break
            
            sum_ = c + c2
            for triplet in candidates:
                if triplet == candidate:
                    continue
                if common in triplet[:2]:
                    sum_ += triplet[2]
            max_sum = sum_ if sum_ > max_sum else max_sum
    
    print(max_sum)