c = int(input())
res = []

for i in range(c):
    n = int(input())
    suma = [[0, 0, 0, 0] for _ in range(n * 2)]
    a = [list(map(int, input().split())) for _ in range(n)]
    for  h in range(n):
        u = 0
        for j in range(n):
            if  a[i][1] == a[u][0] and a[i][0] == a[u][1] and i != j:
                a[i][2] += a[j][2]
                a.pop(u)
                n -= 1
            else:
                u += 1
    for h in range(n):
        x = a[h][0] - 1
        y = a[h][1] - 1

        
        suma[x][0] += a[h][2]
        suma[x][1] += 1


        if suma[x][1] == 1:
            suma[x][2] = a[h][1]
        if suma[x][1] == 2:
            suma[x][3] = a[h][1]
        
        suma[y][0] += a[h][2]
        suma[y][1] += 1


        if suma[y][1] == 1:
            suma[y][2] = a[h][0]
        if suma[y][1] == 2:
            suma[y][3] = a[h][0]
        
    
    for h in range(n * 2):
        if suma[h][1] == 2:
            f = [0]
            for j in a:
                if (j[0] == suma[h][2] and j[1] == suma[h][3]) or (j[1] == suma[h][2] and j[0] == suma[h][3]):
                    f.append(j[2])
            suma[h][0] += max(f)

    res.append(max(suma)[0])
for i in range(c):
    print(res[i])