from collections import Counter from heapq import heapify, heappop, heappush for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) counter = Counter(A) H = [-cnt for cnt in counter.values()] heapify(H) ans = 0 while len(H) >= 3: a = -heappop(H) b = -heappop(H) c = -heappop(H) ans += c a -= c if a > 0: heappush(H, -a) b -= c if b > 0: heappush(H, -b) print(ans)