from collections import defaultdict
from sys import stdin


t = int(input())
def solve():
    n = int(stdin.readline())
    girls_girls = defaultdict(lambda: defaultdict(lambda: 0))
    girls_boys = [0 for _ in range(2*n)]
    d = [None for _ in range(n)]

    for i in range(n):
        d[i] = list(map(lambda n: int(n) - 1, stdin.readline().split()))
        d[i][2] +=1
        a, b, c = d[i]
        girls_boys[a] += c
        girls_boys[b] += c 
        girls_girls[a][b] += c
        girls_girls[b][a] += c
    ans = max(girls_boys)
    for ki, vi in girls_girls.items():
        for kj, vj in vi.items():
            for kk, vk in vi.items():
                ans = max(ans, vj, vk, girls_girls[kj][kk])
    return ans



for i in range(t):
    print(solve())