from collections import defaultdict

t = int(input())

for _ in range(t):
    boys_num = int(input())
    girls_liked = {}
    linked = defaultdict(set) # girl -> list[girls]
    boys = {}
    mx = 0

    for _ in range(boys_num):
        girl1, girl2, nboys = [int(i) for i in input().split()]
        mx = max(mx, nboys)
        if girl1 not in girls_liked:
            girls_liked[girl1] = 0
        if girl2 not in girls_liked:
            girls_liked[girl2] = 0
        girls_liked[girl1] += nboys
        girls_liked[girl2] += nboys
        mx = max([mx, girls_liked[girl1]])
        mx = max([mx, girls_liked[girl2]])
        linked[girl1].add(girl2)
        linked[girl2].add(girl1)
        boys[(girl1, girl2)] = nboys

    for (g1, g2), num in boys.items():
        common = linked[g1].intersection(linked[g2])
        for girl in common:
            path1 = boys[(g1, girl)] if (g1, girl) in boys else boys[(girl, g1)]
            path2 = boys[(g2, girl)] if (g2, girl) in boys else boys[(girl, g2)]
            mx = max(mx, num + path1 + path2)

    print(mx)
