import sys

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

for i in range(T):
    n = int(sys.stdin.readline())
    abc = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
    abc.sort(key=lambda x: x[1]-x[0])
    
    n_list = [0]*(2*n+1)
    a_list = [0]*(2*n+1)
    b_list = [0]*(2*n+1)

    m_val = 0

    for a, b, c in abc:
        val = a_list[a] + b_list[b] + c
        n_list[a] += c
        n_list[b] += c

        if val > m_val:
            m_val = val

        a_list[a] = c
        b_list[b] = c

    print(max(m_val, max(n_list)))
