t = int(input())

for _ in range(t):
    n = int(input())

    a = []

    for _ in range(n):
        a += [list(map(int,input().split()))]

    setMan = []

    for i in range(n):
        f = [0]*n
        girl = [a[i][0],a[i][1]]
        count = [0,0]
        for j in range(n):
            if i != j and f[j] == 0:
                if a[j][0] in girl:
                    count[0] += a[j][2]
                    f[j] = 1
                if a[j][1] in girl:
                    f[j] = 1
                    count[1] += a[j][2]

        setMan += [max(count)+a[i][2]]

    print(max(setMan))




