import sys
t = int(sys.stdin.readline())

def check2(n, boys, current, factor, memb,memr):
    ans1 = 0
    for x in range(n):
        if not x in current:
            if factor == boys[x][0] or factor == boys[x][1]:
                ans1+=boys[x][2]
    ans2 = 0
    t = [boys[current[0]][0],boys[current[0]][1],boys[current[1]][0],boys[current[1]][1]]
    del t[t.index(factor)]
    t.sort()
    try:
        ind = memb.index(t)
        ans2 = memr[ind]
    except ValueError:
        memb.append(t)
        del t[t.index(factor)]
        for x in range(0, n):
            if t[0] == boys[x][0] and t[1] == boys[x][1]:
                ans2+=boys[x][2]
                break
        memr.append(ans2)

    return max(ans1,ans2), memr,memb

def solve(n, boys):
    def comp(elem):
        return elem[2]
    boys.sort(key = comp)
    boys = list(reversed(boys))
    res = boys[0][2]
    memb = list()
    memr = list()
    for z in range(0, n-1):
        for x in range(1, n):
            if not x == z:
                if len({boys[z][0], boys[z][1], boys[x][0], boys[x][1]}) < 4:
                    ans = boys[z][2]+boys[x][2]
                    f = 0
                    if boys[x][0] in (boys[z][0], boys[z][1]):
                        t=check2(n, boys, [z,x], boys[x][0], memb, memr)
                        ans+=t[0]
                        memb = t[2]
                        memr = t[1]
                    else:
                        t=check2(n, boys, [z,x], boys[x][1], memb, memr)
                        ans+=t[0]
                        memb = t[2]
                        memr = t[1]

                    if ans>res:
                        res = ans
    print(res)
    

for _ in range(t):
    n = int(sys.stdin.readline())
    boys = list()
    for _ in range(n):
        a,b,c = map(int, sys.stdin.readline().split())
        boys.append([a,b,c])
    
    solve(n, boys)
