t = int(input())

for _ in range(t):
    n = int(input())
    a = []
    b = []
    c = []
    for _ in range(n):
        aa, bb, cc = map(int, input().split())
        a.append(aa)
        b.append(bb)
        c.append(cc)

    # for d1 in range(1,2*n+1):
    #     for d2 in range(1,2*n+1):
    #         for d3 in range(1,2*n+1):
    #             for i in range(n):
    #                 if a[i] in [d1,d2,d3] and b[i] in [d1,d2,d3]:

    maxres = 0
    for i in range(n):
        for j in range(i + 1, n):
            if a[i] == a[j] or a[i] == b[j] or b[i] == a[j] or b[i] == b[j]:
                
                d1 = a[i]
                d2 = b[i]
                if a[j] in [d1, d2]:
                    d3 = b[j]
                    dp = a[j]
                else:
                    d3 = a[j]
                    dp = b[j]
                
                # d=[d1,d2,d3]
                dres=0

                for k in range(j + 1, n):
                    if a[k] != dp and b[k] != dp:
                        if a[k] in [d1, d2, d3] and b[k] in [d1, d2, d3]:
                            res = c[i] + c[j] + c[k]
                            if res > maxres:
                                maxres = res
                    else:
                        dres+=c[k]
                res=dres+c[i] + c[j]
                if res > maxres:
                    maxres = res
                    
    print(max(maxres, max(c)))                        
