import sys

def bfs(v):
    s=0
    q=gr[v]
    for i in q: #яке число перевіряємо у інших
        for j in q: #в чому превіряємо
            if i not in gr[j] and i!=j:
                kgri=0
                ilis=[]
                kgrj=0
                jlis=[]
                for k in gr[i]:
                    if k in q and k!=v and k not in gr[j]:
                        kgri+=c[k][2]
                        ilis.append(k)
                for k in gr[j]:
                    if k in q and k!=v and k not in gr[i]:
                        kgrj+=c[k][2]
                        jlis.append(k)
                if kgri<=kgrj:
                    for k in ilis:
                        q.remove(k)
                else:
                    for k in jlis:
                        q.remove(k)
    for i in q:
        s+=c[i][2]
    return s
                    
                    
            
t = int(sys.stdin.readline())
for _ in range(t):
    n = int(sys.stdin.readline())
    c=[[int(i) for i in sys.stdin.readline().split()] for _ in range(n)]
    gr=[[] for _ in range(n)]
    for i in range(n):
        for j in range(n):
            if i!=j and c[i][0] in c[j][0:2] or c[i][1] in c[j][0:2]:
                gr[i].append(j)
    maxgr=0
    for i in range(n):
        curgr=bfs(i)
        if curgr>maxgr:
            maxgr=curgr
    print(maxgr)
    

