t = int(input())

def search(From,to):
    f = 0
    for i in a[From]:
        if i[0] == to:
            f = 1
            return i[1]
    if f == 0:
        return 0


for _ in range(t):
    n = int(input())

    a = [[] for _ in range(2*n+1)]

    for _ in range(n):
        u,v,c = map(int,input().split())

        a[u] += [[v,c]]
        a[v] += [[u,c]]

    mx = []

    for i in range(1,2*n+1):
        cmx = 0
        neighbor = []
        for j in a[i]:
            neighbor += [j[0]]
            cmx += j[1]

        po = 0
        for j in neighbor:
            for l in a[j]:
                for p in neighbor:
                    if p != j:
                        if l[0] == p:
                            ty = l[1]+search(i,l[0])+search(i,j)
                            if po < ty:
                                po = ty


        mx += [max(cmx,po)]


    print(max(mx))

