t = int(input())

for _ in range(t):
    n = int(input())
    girl_likers = [[] for i in range(2*n+1)]
    girl_likers_sum = [0 for i in range(2*n+1)]

    maxi = 0
    s = [[]]
    for j in range(1,n+1):
        q = list(map(int, input().split()))

        s.append(q)
        girl_likers[q[0]].append(j)
        girl_likers[q[1]].append(j)
        girl_likers_sum[q[0]] += q[2]
        girl_likers_sum[q[1]] += q[2]

    for j in range(1, n+1):
        q = s[j]
        a = q[0]
        b = q[1]
        c = q[2]
        if girl_likers_sum[a]>=maxi:
            maxi = girl_likers_sum[a]
        if girl_likers_sum[b]>=maxi:
            maxi = girl_likers_sum[b]

        for u in girl_likers[a]:

            w = s[u]
            tru = c + w[2]

            jj = w[0] + w[1] - a
            for uu in girl_likers[jj]:
                if s[uu][0] in [b,jj] and s[uu][1] in [b,jj]:
                    tru += s[uu][2]
                    if tru > maxi:
                        maxi = tru
                    break

        for u in girl_likers[b]:
            w = s[u]
            tru = c + w[2]

            jj = w[0] + w[1] - b
            for uu in girl_likers[jj]:
                if s[uu][0] in [a, jj] and s[uu][1] in [a, jj]:
                    tru += s[uu][2]
                    if tru > maxi:
                        maxi = tru
                    break

    print(maxi)




