import sys

T = int(sys.stdin.readline())

for i in range(T):
    n = int(sys.stdin.readline())
    abc = [map(int, sys.stdin.readline().split()) for _ in range(n)]
    
    conn_to = [set() for _ in range(2*n+1)]
    n_list = [0]*(2*n+1)

    for i in range(n):
        a, b, c = abc[i]

        n_list[a] += c
        n_list[b] += c
        
        conn_to[a].add(i)
        conn_to[b].add(i)

    print(conn_to)

    print(max(n_list))
