import sys
t = int(input())
for i in range(t):
    dist = 0
    n = int(input())
    w = [list(map(int, sys.stdin.readline().split())) for j in range(n)]
    k = max(max(w, key=lambda x: x[1])[1], max(w, key=lambda x: x[0])[0])
    g = [[] for j in range(k)]
    for i in w:
        g[i[0] - 1].append((i[1] - 1, i[2]))
        g[i[1] - 1].append((i[0] - 1, i[2]))
        #print(g)
    for v in range(k):
        if g[v]:
            for s in g[v]:
                #print(g)
                for i in g[s[0]]:
                    for j in range(len(g[v])):
                        if i[0] == g[v][j][0]:
                            dist = max(dist, g[v][j][1] + i[1] + s[1])
    for v in range(k):
        q = 0
        for i in g[v]:
            q += i[1]
        dist = max(q, dist)
    print(dist)

