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

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

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

    for x in range(1, n):
        if boys[x][0] in factors or boys[x][1] in factors:
            new = set()
            if boys[x][0] in factors:
                new.add(boys[x][0])
            if boys[x][1] in factors:
                new.add(boys[x][0])
            factors = new
            ans+=boys[x][2]
    
    print(ans)
    

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)
