from collections import defaultdict

t = int(input())

for _ in range(t):
    boys_num = int(input())
    girls_liked = {}
    linked = defaultdict(dict)
    boys = {}
    mx = 0
    two_types = True

    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
        if len(linked[girl1]) > 2 or len(linked[girl2]) > 2:
            two_types = False
        boys[(girl1, girl2)] = nboys

    if not two_types:
        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])
    else:
        for girl, link in linked.items():
            if len(link) == 2:
                l1, l2 = sorted(list(link.keys()))
                bb = sorted([l1, l2, girl])
                if (l1, l2) in boys or (l2, l1) in boys:
                    mx = max(mx, boys[(bb[0], bb[1])] + boys[(bb[1], bb[2])] + boys[(bb[0], bb[2])])

    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()
        