for i in range(int(input())):
    n = int(input())
    a, b, c = [], [], []
    d = {i : [] for i in range(1, 2 * n + 1)}
    for i in range(1, n + 1):
        a1, b1, c1 = map(int, input().split())
        a.append(a1)
        b.append(b1)
        c.append(c1)
        d[a1].append(i)
        d[b1].append(i)
    r = {i : [] for i in range(1, 2 * n + 1)}
    for i in range(1, n + 1):
        for j in range(1, len(d[i]) + 1):
            r[d[i][j - 1]].extend(d[i][:j - 1] + d[i][j:])
    ans = 0
    for i in range(1, 2 * n + 1):
        for j in r[i]:
            for o in r[i]:
                if o in r[j]:
                    ans = max(ans, c[i - 1] + c[j - 1] + c[o - 1])
                    
    ans1 = [0] * (2 * n)
    for i in range(n):
        ans1[a[i] - 1] += c[i]
        ans1[b[i] - 1] += c[i]
    print(max(ans, max(ans1)))
