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

def check2(n, boys, current, factor):
    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 = list(set([boys[current[0]][0], boys[current[0]][1], boys[current[1]][0],boys[current[1]][1]]))
    del t[t.index(factor)]

    for x in range(n):
        if boys[x][0] in t and boys[x][1] in t:
            ans2+=boys[x][2]
            break

    return max(ans1,ans2)


def solve(n, boys):
    def comp(elem):
        return elem[2]
    
    boys.sort(key = comp)
    res = 0
    boys = list(reversed(boys))

    for x in range(0,n-1):
        ans = boys[x][2]
        for y in range(x+1, n):
            if boys[y][0] == boys[x][0] or boys[y][0] == boys[x][1]:
                ans1 = boys[x][2]
                ans1 += boys[y][2]
                ans1+=check2(n, boys, [x,y], boys[y][0])
                if ans1>ans:
                    ans = ans1
            if boys[y][1] == boys[x][0] or boys[y][1] == boys[x][1]:
                ans11 = boys[x][2]
                ans11 += boys[y][2]
                ans11+=check2(n, boys, [x,y], boys[y][1])
                if ans11>ans:
                    ans = ans11
        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)
