import sys

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

class Node:
    def __init__(self):
        self.nxt = dict()
        self.pr = -1
        self.v = 0 

for i in range(T):
    n = int(sys.stdin.readline())
    abc = [map(int, sys.stdin.readline().split()) for _ in range(n)]

    nodes = [Node() for _ in range(2*n+1)]
    max_v = 0
    g = [0]*(2*n+1)

    for i in range(n):
        a, b, c = abc[i]
        nodes[a].nxt[b] = c
        nodes[b].nxt[a] = c
        g[a] += c
        g[b] += c

    for i in range(2*n+1):
        if nodes[i].pr != -1:
            continue

        to_check = {(nodes[i], 0)}
        loop = 0

        while len(to_check) > 0:
            new_to_check = set()

            for nd, v0 in to_check:
                if nd.pr == -1:
                    nd.pr = loop
                    nd.v = v0
                    for k, v in nd.nxt.items():
                        new_to_check.add((nodes[k], v0+v))
                elif loop-nd.pr == 1:
                    v = v0 + nd.v
                    if v > max_v:
                        max_v = v

            loop += 1
            to_check = new_to_check

    print(max(max_v, max(g)))
