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

def solve(n, boys,d):
    def c(e):
        return e[2]
    
    for x in range(0, 2*n):
        d[x].sort(reverse = True, key = c)

for _ in range(t):
    n = int(sys.stdin.readline())
    boys = list()
    d = [[] for _ in range(2*n)]
    for _ in range(n):
        a,b,c = map(int, sys.stdin.readline().split())
        boy = [a,b,c]
        d[a-1].append(boy)
        d[b-1].append(boy)
        boys.append(boy)
    
    solve(n, boys, d)
