def safe_inc(d: dict[int, int], k: int, c: int):
    d[k] = d.get(k, 0) + c

t = int(input())

for _ in range(t):
    n = int(input())

    girl_types: dict[int, int] = {}

    for i_type in range(1, n+1):
        a_girl_type, b_girl_type, c_guys_amount = map(int, input().split(" "))

        safe_inc(girl_types, a_girl_type, c_guys_amount)
        safe_inc(girl_types, b_girl_type, c_guys_amount)

    print(list(girl_types.values()))
    for k, v in girl_types.items():
        print(k, v)
        
    print(max(girl_types.values()))
