T = int(input())

def xor(pa,pb,pc):
    s=0
    p = 0
    ind = []
    for i in range(len(pa)):
        if(pa[i]!=0 and pb[i]!=0)\
                or (pb[i]!=0 and pc[i]!=0)\
                or (pa[i]!=0 and pc[i]!=0):
            ind.append(i)

    if(len(ind)==3):
        s += pa[ind[0]]+pb[ind[0]]+pc[ind[0]]
        s += pa[ind[1]]+pb[ind[1]]+pc[ind[1]]
        s += pa[ind[2]]+pb[ind[2]]+pc[ind[2]]
        s//=2
    return [len(ind),s]

def nd():
    n = int(input())
    p = [[0 for _ in range(2*n)] for _ in range(n)]
    d = [0 for _ in range(2*n)]
    for i in range(n):
        ai,bi,ci = map(int,input().split())
        p[i][ai-1]=ci
        p[i][bi-1]=ci
        d[ai-1]+=ci
        d[bi-1]+=ci
    #print(p)

    maxs = 0
    for ai in range(n):
        for bi in range(ai+1,n):
            for ci in range(bi+1,n):
                s, pac = xor(p[ai],p[bi],p[ci])
                if(s!=3):
                    continue
                if(pac>maxs):
                    maxs=pac
    #print(maxs)
    #print(p)
    #print(d)
    print(max(max(d),maxs))

for _ in range(T):
    nd()