import sys
t = int(sys.stdin.readline())

def solve(n, boys):
    def comp(elem):
        return elem[2]
    
    boys.sort(key = comp)
    res = 0
    boys = list(reversed(boys))
    for z in range(0, n):
        ans = boys[z][2]

        factors = [(boys[z][0], boys[z][1])]

        for x in range(0, n):
            if not x == z:
                use = True
                for y in factors:
                    if (not boys[x][0] in y) and (not boys[x][1] in y):
                        use = False
                        break
                
                if use:
                    factors.append((boys[x][0], boys[x][1]))
                    ans+=boys[x][2]
        
        if ans>res:
            res = ans
    
    print(res)
    

for _ in range(t):
    n = int(sys.stdin.readline())
    boys = list()
    for _ in range(n):
        a,b,c = map(int, sys.stdin.readline().split())
        boys.append([a,b,c])
    
    solve(n, boys)
