def count(n,matr):
    l = []
    counted = []
    am = []
    ex = 0
    for i in range(n):
        a = matr[i][0]
        b = matr[i][1]
        c = matr[i][2]
        l.append([a,b])
        am.append(c)
    start_index = am.index(max(am))
    while True:
        ex += am[start_index]
        counted.append(start_index)
        if l[start_index][0] not in counted and l[start_index][0] <= n-1:
            start_index = l[start_index][0]
        elif l[start_index][1] not in counted and l[start_index][0] <= n-1:
            start_index = l[start_index][1]
        else:
            print(ex)
            break
t = int(input())
matrix = []
ns = []
tempp =[]
temp=[]
for numbers in range(t):
    n = int(input())
    ns.append(n)
    tempp = []
    for i in range(n):
        temp = input().split()
        for b in range(3):
            temp[b] = int(temp[b])
        tempp.append(temp)
    matrix.append(tempp)
for i in range(t):
    count(ns[i], matrix[i])