import sys

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

class Node:
    def __init__(self, c_i, a, b):
        self.c_i = c_i
        self.l = a
        self.r = b

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])

    d = dict()
    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
        d[a] = d.get(a, 0) + c
        d[b] = d.get(b, 0) + c

        if val > m_val:
            m_val = val

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

    max_d = max(d.values())
    print(max(m_val, max_d))
