from math import factorial
from random import shuffle
import sys
def main():
    t = int(input())
    for i in range(t):
        s = 0
        n = int(input())
        w = [list(map(int, sys.stdin.readline().split())) for i in range(n)]
        n = min(8, n)
        for i in range(factorial(n + 2)):
            s1 = 0
            m = []
            m.append(([w[0][0], w[0][1]], w[0][2]))
            for i in range(1, n):
                f = 1
                for j in m:
                    if w[i][0] not in j[0] and w[i][1] not in j[0]:
                        f = 0
                if f:
                    m.append(([w[i][0], w[i][1]], w[i][2]))
            for i in m:
                s1 += i[1]
            s = max(s1, s)
            shuffle(w)
        print(s)
main()