from collections import defaultdict

t = int(input())

for _ in range(t):
    boys_num = int(input())
    girls_liked = {}
    linked = defaultdict(dict) # girl -> set[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][girl2] = nboys
        linked[girl2][girl1] = nboys
        boys[(girl1, girl2)] = nboys

    for (g1, g2), num in boys.items():
        it = linked[g1] if len(linked[g1]) <= len(linked[g2]) else linked[g2]
        check = linked[g2] if it != linked[g2] else linked[g1]
        for girl in it:
            if girl in check:
                mx = max(mx, num + linked[g1][girl] + linked[g2][girl])

    print(mx)





    # for ind, (girl1, girl2, nboys) in enumerate(boys):
    #     added = False
    #     if girl1 in girls_liked:
    #         girls_liked[girl1] += nboys
    #     elif girl2 in girls_liked:
    #         girls_liked[girl2] += nboys
    #     else: 
    #         if girl1 in temp:
    #             girls_liked[girl1] = nboys
    #             girls_liked[girl1] += boys[temp[girl1]][2]
    #             girls_liked[girl2] = 0 
    #             girls_liked[boys[temp[girl1]][1]] = 0
    #             del temp[boys[temp[girl1]][1]]
    #             if girl1 in temp:
    #                 del temp[girl1]
    #         elif girl2 in temp:
    #             girls_liked[girl2] = nboys
    #             girls_liked[girl1] += boys[temp[girl2]][2]
    #             girls_liked[girl1] = 0
    #             girls_liked[boys[temp[girl2]][1]] = 0
    #             del temp[boys[temp[girl2]][1]]
    #             if girl2 in temp:
    #                 del temp[girl2]
    #         else:
    #             temp[girl1] = ind
    #             temp[girl2] = ind
    #     print(f"Liked dict: {girls_liked}")
    #     print(f"Temp dict: {temp}")
    #     print()
        