import sys

def find_popular():
    boys_types = int(sys.stdin.readline())
    boys = []
    girls = [0] * 2 * boys_types
    boys_like_popular = []
    combs_girls = []
    combs = []
    differences = []
    boys_like_pops_number = 0

    for _ in range(boys_types):
        a, b, c = map(int, sys.stdin.readline().split())
        for __ in range(c):
            boys.append([a, b])


    for boy_type in boys:
        for girl in boy_type:
            girls[girl - 1] += 1

    popular = girls.index(max(girls))+1

    for boy in boys:
        if boy[0] == popular or boy[1] == popular:
            boys_like_popular.append(boy)

    boys_like_pops_number = len(boys_like_popular)

    for popular_boy in boys_like_popular:
        if popular_boy[0] != popular and popular_boy[0] not in combs_girls:
            combs_girls.append(popular_boy[0])
        elif popular_boy[1] != popular and popular_boy[1] not in combs_girls:
            combs_girls.append(popular_boy[1])

    for i in combs_girls:
        for j in combs_girls:
            if i != j:
                if [i, j] not in combs:
                    combs.append([i, j])

    for combination in combs:
        combs_num = 0
        boys_not_combs = 0
        for boy in boys:
            if combination == boy:
                combs_num += 1
        for pop_boy in boys_like_popular:
            if pop_boy[0] != combination[0] and pop_boy[0] != combination[1] and pop_boy[1] != combination[0] and pop_boy[1] != combination[1]:
                boys_not_combs += 1

        if combs_num > boys_not_combs:
            differences.append(combs_num-boys_not_combs)

    if len(differences) != 0:
        boys_like_pops_number += max(differences)

    return boys_like_pops_number

tests = int(input())
tests_results = []

for _ in range(tests):
    tests_results.append(find_popular())

for result in tests_results:
    print(result)