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

def solve(n, boys,d):
    def c(e):
        return e[2]

    ans = 0
    for x in range(0, 2*n):
        d[x].sort(reverse = True, key = c)
        ans1 = 0
        for y in range(0, len(d[x])):
            ans1+=d[x][y][2]
        if ans1>ans:
            ans = ans1
    
    for x in range(0, 2*n):
        if d[x] == []:
            continue
        if len(d[x]) == 1:
            pass
        else:
            ans1 = d[x][0][2]+d[x][1][2]
            t = []
            if d[x][0][0] == x+1:
                t.append(d[x][0][1])
                if d[t[0]] == []:
                    continue
            else:
                t.append(d[x][0][0])
                if d[t[0]] == []:
                    continue
            
            if d[x][1][0] == x+1:
                t.append(d[x][1][1])
                if d[t[0]] == [] or d[t[1]] == []:
                    continue
            else:
                t.append(d[x][1][0])
                if d[t[0]] == [] or d[t[1]] == []:
                    continue
            
            for boy in d[t[0]+1]:
                if boy in d[t[1]+1]:
                    ans1+=boy[2]
                    break
            
            if ans1>ans:
                ans=ans1
    
    print(ans)
        
    

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