Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well.

You are given an integer sequence $A$ with length $N$. Find the number of ordered pairs of positive integers $(a, b)$ such that $a$ occurs in $A$ at least $b$ times and $b$ occurs in $A$ at least $a$ times. 

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$.

------  Output ------
For each test case, print a single line containing one integer — the number of pairs.

------  Constraints  ------
$1 ≤ T ≤ 1,000$
$2 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$ for each valid $i$
the sum of $N$ over all test cases does not exceed $3 \cdot 10^{6}$

------  Subtasks ------
Subtask #1 (30 points):
$1 ≤ N ≤ 10^{5}$
the sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$

Subtask #2 (70 points): original constraints

----- Sample Input 1 ------ 
3

5

1 2 3 4 5

5

1 1 2 2 3

5

3 3 2 2 2
----- Sample Output 1 ------ 
1

4

3

Difficulty: MEDIUM_HARD
Tags: Data structures
Skill types: Data structures

Starter code:

Solutions:
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for j in a:
		d[j] = d.get(j, 0) + 1
	a.sort()
	s = 0
	c = 0
	for k in range(len(a)):
		if k == 0:
			s = 1
		elif a[k] != a[k - 1]:
			s = 1
		else:
			s += 1
		if d.get(s, 0) >= a[k]:
			c += 1
	print(c)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	for i in a:
		dic[i] = dic.get(i, 0) + 1
	a.sort()
	f = 0
	count = 0
	for i in range(len(a)):
		if i == 0:
			f = 1
		elif a[i] != a[i - 1]:
			f = 1
		else:
			f += 1
		if dic.get(f, 0) >= a[i]:
			count += 1
	print(count)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	k = {}
	c = 1
	j = 1
	for i in range(n - 1):
		if a[i] == a[i + 1]:
			c += 1
			j = 0
		elif j == 0:
			k[a[i]] = c
			c = 1
		else:
			k[a[i]] = c
	if a[n - 1] != a[n - 2]:
		k[a[n - 1]] = 1
	else:
		k[a[i]] = c
	c = 0
	for i in k:
		for j in range(1, k[i] + 1):
			if j in k:
				if k[j] >= i:
					c += 1
	print(c)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	l = list(map(int, a.split()))
	m = Counter(l)
	c = 0
	for i in m:
		for j in range(1, m[i] + 1):
			if m[j] >= i:
				c += 1
	print(c)


def abc(val, l):
	i = 0
	j = len(l) - 1
	ans = -1
	while i <= j:
		mid = i + (j - i) // 2
		if l[mid][0] >= val:
			ans = mid
			j = mid - 1
		else:
			i = mid + 1
	return ans
t = int(input())
import random
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		try:
			d[i] += 1
		except:
			d[i] = 1
	x = 0
	c = []
	for i in d.keys():
		c.append([d[i], i])
	c.sort()
	ans = 0
	for i in d.keys():
		x = abc(i, c)
		if x + 1:
			for j in range(x, len(c)):
				if d[i] >= c[j][1]:
					ans += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr.sort()
	dic = dict()
	sett = set(arr)
	answer = 0
	cnt = [0] * 1000001
	for i in arr:
		cnt[i] += 1
	for x in sett:
		for y in range(cnt[x] + 1):
			if x <= cnt[y]:
				answer += 1
	print(answer)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	fd = defaultdict(int)
	count = 0
	for i in a:
		fd[i] += 1
	for i in list(fd):
		j = fd[i]
		if j >= i:
			count += 1
			for k in range(i + 1, j + 1):
				if fd[k] >= i:
					count += 2
	print(count)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	c = Counter(l)
	from bisect import bisect_right as br
	arr = sorted(list(c.keys()))
	cnt = 0
	alen = len(arr)
	s = set()
	for i in range(alen):
		a = arr[i]
		x = a
		for j in range(alen):
			if arr[j] > c[a]:
				break
			if c[arr[j]] >= a:
				print
				s.add((arr[i], arr[j]))
	print(len(s))


import itertools
from collections import Counter
t = int(input())
while t:
	n = int(input())
	ar = list(map(int, input().split()))
	ac = Counter(ar)
	count = 0
	for i in ac:
		cnt = ac[i]
		for j in range(1, cnt + 1):
			if j in ac and j != i:
				if ac[j] >= i:
					count += 1
		if ac[i] >= i:
			count += 1
	print(count)
	t = t - 1


def countOrderPair(arr, n):
	hashmap = {}
	ans = 0
	for i in range(n):
		if arr[i] in hashmap:
			hashmap[arr[i]] += 1
		else:
			hashmap[arr[i]] = 1
	for num in hashmap:
		count = hashmap[num]
		for k in range(1, count + 1):
			if k in hashmap and k != num:
				if hashmap[k] >= num:
					ans += 1
		if hashmap[num] >= num:
			ans += 1
	return ans
for i in range(int(input())):
	N = int(input())
	arr = list(map(int, input().split()))
	print(countOrderPair(arr, N))


for __ in range(int(input())):
	N = input()
	A = map(int, input().split())
	d = {}
	for i in A:
		d[i] = d.get(i, 0) + 1
	m = max(d.keys())
	ans = 0
	for i in sorted(d.keys()):
		diff = d[i] - i
		if diff >= 0:
			ans += 1
			for j in range(i + 1, min(d[i] + 1, m + 1)):
				if d.get(j, -1) >= i:
					ans += 2
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	c = Counter(l)
	cnt = 0
	for i in c:
		curr = c[i]
		for j in range(1, c[i] + 1):
			if c[j] >= i:
				if i == j:
					cnt += 1
				else:
					cnt += 1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	cnt = [0] * 1000001
	for i in l:
		cnt[i] += 1
	ans = 0
	s = set(l)
	for i in s:
		for j in range(cnt[i] + 1):
			if cnt[j] >= i:
				ans += 1
	print(ans)


def main():
	T = int(input())
	for i in range(T):
		N = int(input())
		A = [int(item) for item in input().split()]
		d = [0] * (max(A) + 1)
		for a in A:
			d[a] += 1
		A = list(set(A))
		A.sort()
		R = set()
		for i in A:
			for j in A:
				if d[i] < j:
					break
				if d[i] >= j and d[j] >= i:
					R.add((i, j))
					R.add((j, i))
		print(len(R))
main()


for i in range(int(input())):
	count = {}
	pair = 0
	N = int(input())
	inp = input().split(' ')
	for j in range(N):
		if inp[j] in count:
			count[inp[j]] = count[inp[j]] + 1
		else:
			count[inp[j]] = 1
	for key in count.keys():
		for k in range(int(key), count[key] + 1):
			if str(k) in count and count[str(k)] >= int(key):
				if int(key) == k:
					pair = pair + 1
				else:
					pair = pair + 2
	print(pair)


from collections import Counter

def main():
	for _ in range(int(input())):
		n = int(input())
		alist = [int(x) for x in input().split()]
		actr = Counter(alist)
		tally = 0
		for key in actr:
			for i in range(actr[key] + 1):
				if i in actr and actr[i] >= key:
					tally += 1
		print(tally)
main()


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	L = list(map(int, input().split()))
	k = max(L)
	f = [0] * (k + 1)
	for i in L:
		f[i] += 1
	res = 0
	for i in range(1, k + 1):
		if f[i] >= i:
			res += 1
		for j in range(i + 1, f[i] + 1):
			if j <= k and f[j] >= i:
				res += 2
	print(res)


from collections import Counter
from itertools import takewhile
t = int(input())
for _ in range(t):
	input()
	f = Counter(map(int, input().split()))
	a = sorted(f)
	print(sum((f[j] >= i for i in a for j in takewhile(lambda j: f[i] >= j, a))))


import sys
try:
	sys.stdin = open('inp', 'r')
except:
	pass
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	ocr = {}
	for i in a:
		ocr[i] = 1 + ocr.get(i, 0)
	a = list(sorted(set(a)))
	res = 0
	for i in range(len(a)):
		ed = ocr[a[i]]
		for j in range(i, len(a)):
			if a[j] > ed:
				break
			if ocr[a[j]] >= a[i]:
				res += 1 if a[i] == a[j] else 2
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	k = max(arr)
	b = [0] * (k + 1)
	for i in arr:
		b[i] += 1
	count = 0
	for i in range(1, len(b)):
		c = b[i]
		if c >= i:
			count += 1
		for j in range(i + 1, c + 1):
			if j < k + 1:
				if b[j] >= i:
					count += 2
	print(count)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(ans, c) = (0, 0)
	d = Counter(a)
	a = list(d.keys())
	a.sort()
	for i in range(len(a)):
		for j in range(i, len(a)):
			if d[a[i]] < a[j]:
				break
			if d[a[i]] >= a[j] and d[a[j]] >= a[i]:
				if a[i] == a[j]:
					ans += 1
				else:
					ans += 2
	print(ans)


def possiblilties_builder(X):
	counter = 0
	for (val, count) in X.items():
		for a in range(1, count + 1):
			if val == a:
				counter += 1
			else:
				try:
					if X[a] >= val:
						counter += 1
				except KeyError:
					continue
	return counter

def foo():
	T = int(input())
	for i in range(T):
		pair_count = 0
		N = int(input())
		A = map(int, input().split(' '))
		counts = {}
		for element in A:
			if element not in counts.keys():
				counts[element] = 1
			else:
				counts[element] += 1
		data_mat = possiblilties_builder(counts)
		print(data_mat)
foo()


from collections import Counter
t = int(input())
for _ in range(t):
	input()
	d = Counter(map(int, input().split()))
	print(sum((d[j] >= i for i in d for j in range(1, d[i] + 1))))


t = int(input())
for _ in range(t):
	a = [0] * 1000000
	input()
	m = 1
	for x in map(int, input().split()):
		m = max(m, x)
		a[x - 1] += 1
	print(sum((a[j] > i for i in range(m) for j in range(a[i]))))


t = int(input())
for _ in range(t):
	(mn, mx) = (1000000, 1)
	a = [0] * mn
	input()
	for x in map(int, input().split()):
		(mn, mx) = (min(mn, x), max(mx, x))
		a[x - 1] += 1
	print(sum((a[j] > i for i in range(mx) for j in range(a[i]))))


for i in range(int(input())):
	input()
	array = list(map(int, input().split()))
	srtd_set = sorted(list(set(array)))
	dict_array = {l: 0 for l in srtd_set}
	pairs = 0
	for i in array:
		dict_array[i] += 1
	for i in dict_array:
		for j in dict_array:
			if j > dict_array[i]:
				break
			if dict_array[j] >= i and dict_array[i] >= j:
				pairs += 1
	print(pairs)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	d = {}
	m = []
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			m.append(i)
			d[i] = 1
	ct = 0
	m.sort()
	for i in m:
		for j in m:
			if d[i] >= j:
				if d[j] >= i:
					ct += 1
			else:
				break
	print(ct)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	numbers = {}
	for i in range(len(a)):
		if numbers.get(a[i]) == None:
			numbers[a[i]] = 0
		numbers[a[i]] += 1
	pairs = set()
	for i in numbers:
		if i < numbers[i]:
			pairs.add((i, i))
			while i != numbers[i]:
				if numbers.get(numbers[i]) != None and numbers.get(numbers[i]) >= i:
					pairs.add((i, numbers[i]))
					pairs.add((numbers[i], i))
				numbers[i] -= 1
		elif i == numbers[i]:
			pairs.add((i, numbers[i]))
	print(len(list(pairs)))


from collections import Counter
tc = int(input())
while tc > 0:
	n = int(input())
	a = list(map(int, input().split()))
	b = list(set(a))
	b.sort()
	freqCounter = Counter(a)
	ans = 0
	for i in b:
		for j in b:
			if freqCounter[i] >= j and freqCounter[j] >= i:
				ans += 1
			elif freqCounter[i] < j:
				break
	print(ans)
	tc -= 1


tc = int(input())
while tc > 0:
	n = int(input())
	a = list(map(int, input().split()))
	mx = max(a)
	freqCounter = [0] * (mx + 5)
	for i in a:
		freqCounter[i] += 1
	z = list(filter(lambda x: freqCounter[x] != 0, range(mx + 1)))
	ans = 0
	for j in range(len(z)):
		for k in range(j, len(z)):
			if freqCounter[z[j]] >= z[k] and freqCounter[z[k]] >= z[j]:
				if k == j:
					ans += 1
				else:
					ans += 2
			elif freqCounter[z[j]] < z[k]:
				break
	print(ans)
	tc -= 1


from collections import *
import bisect

def get_key(o):
	return o[1]

def solve(n, arr):
	h = defaultdict(int)
	for i in range(n):
		h[arr[i]] += 1
	l = list(h.items())
	l.sort(key=get_key)
	keys = []
	values = []
	for i in range(len(l)):
		keys.append(l[i][0])
		values.append(l[i][1])
	n = len(l)
	s = set()
	c = 0
	for i in range(n):
		index = bisect.bisect_left(values, keys[i])
		for j in range(index, n):
			if values[i] >= keys[j]:
				c += 1
	return c
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	m = max(ar)
	num = [0 for x in range(m + 1)]
	for j in range(n):
		num[ar[j]] += 1
	z = list(filter(lambda x: num[x] != 0, range(m + 1)))
	c = 0
	for j in range(len(z)):
		for k in range(j, len(z)):
			if num[z[j]] >= z[k] and num[z[k]] >= z[j]:
				if k == j:
					c += 1
				else:
					c += 2
			elif num[z[j]] < z[k]:
				break
	ans.append(c)
for i in ans:
	print(i)


def main():
	import collections
	test_cases = int(input())
	for _ in range(test_cases):
		n = int(input())
		a = [int(x) for x in input().split()]
		ans = 0
		counter = collections.Counter(a)
		items = [x for x in counter.keys()]
		for key in items:
			for i in range(0 + 1, counter[key] + 1):
				z = counter[i]
				if z >= key:
					ans = ans + 1
		print(ans)
main()


import math
from collections import defaultdict
from itertools import permutations
from itertools import combinations
for i in range(int(input())):
	n = int(input())
	d = defaultdict(int)
	l = list(map(int, input().split()))
	for i in range(len(l)):
		d[l[i]] += 1
	c = 0
	l = [0] * (n + 1)
	for i in d:
		l[i] = d[i]
	for i in d:
		for j in range(1, d[i] + 1):
			if i <= l[j]:
				c += 1
	print(c)


I = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	d = {}
	a = list(I())
	count = 0
	for i in a:
		d[i] = d.get(i, 0) + 1
	for y in d:
		x = d[y]
		for i in range(y, x + 1):
			if d.get(i, 0) >= y:
				if i != y:
					count += 2
				else:
					count += 1
	print(count)


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	carr = [0] * (arr[-1] + 1)
	count = 1
	sharr = []
	for i in range(n - 1):
		if arr[i] == arr[i + 1]:
			count += 1
		else:
			carr[arr[i]] = count
			sharr.append(arr[i])
			count = 1
	carr[arr[-1]] = count
	sharr.append(arr[-1])
	count = 0
	for i in sharr:
		for j in sharr:
			if carr[i] < j:
				break
			if carr[i] >= j and carr[j] >= i:
				count += 1
	print(count)
	t -= 1


T = int(input())
while T:
	N = int(input())
	A = list(map(int, input().split()))
	occur = dict()
	distinct_nums = []
	count_pairs = 0
	for i in range(N):
		if A[i] not in occur:
			occur[A[i]] = 1
			distinct_nums.append(A[i])
		else:
			occur[A[i]] += 1
	for i in range(len(distinct_nums)):
		num = distinct_nums[i]
		num_occur = occur[num]
		for j in range(num, num_occur + 1):
			if j in occur and occur[j] >= num:
				if num == j:
					count_pairs += 1
				else:
					count_pairs += 2
	print(count_pairs)
	T -= 1


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	P = {}
	k = []
	for x in A:
		if x in P:
			P[x] += 1
		else:
			k.append(x)
			P[x] = 1
	count = 0
	k.sort()
	for i in k:
		for j in k:
			if P[i] >= j:
				if P[j] >= i:
					count += 1
			else:
				break
	print(count)


from bisect import bisect_right as bl
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	ans = 0
	for i in a:
		d[i] = d.get(i, 0) + 1
	for i in d:
		for j in range(i, d[i] + 1):
			if d.get(j, 0) >= i:
				if j != i:
					ans += 2
				else:
					ans += 1
	print(ans)


for _ in range(int(input())):
	n = input()
	int(n)
	l = list(map(int, input().split()))
	d = {}
	for i in range(len(l)):
		if l[i] in d:
			d[l[i]] += 1
		else:
			d[l[i]] = 1
	c = 0
	for i in d:
		for j in range(1, d[i] + 1):
			if d[i] >= j and j in d:
				if d[j] >= i:
					c += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = 0
	d = {}
	for i in a:
		d[i] = 0
	for i in a:
		d[i] += 1
	a = list(set(a))
	a.sort()
	for i in range(len(a)):
		for j in range(i, len(a)):
			x = a[i]
			y = a[j]
			if d[x] >= y and d[y] >= x:
				if x == y:
					c += 1
				else:
					c += 2
			if d[x] < y:
				break
	print(c)


T = int(input())
while T != 0:
	count = 0
	N = int(input())
	l = list(map(int, input().split()))
	m = [0] * 1000000
	for i in range(N):
		m[l[i]] += 1
	t = max(l)
	for i in range(1, t + 1):
		a = m[i]
		for j in range(1, a + 1):
			if m[j] >= i:
				count += 1
	print(count)
	T = T - 1


t = int(input())
while t != 0:
	count = 0
	N = int(input())
	l = list(map(int, input().split()))
	q = [0] * 10 ** 6
	k = max(l)
	for i in range(len(l)):
		q[l[i]] += 1
	for i in range(1, k + 1):
		a = q[i]
		for j in range(1, a + 1):
			if a >= j and q[j] >= i:
				count += 1
	print(count)
	t = t - 1


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	dp = {}
	for i in a:
		if dp.get(i):
			dp[i] += 1
		else:
			dp[i] = 1
	ans = 0
	b = []
	for i in dp:
		if dp[i] >= i:
			ans += 1
		b.append((dp[i], i))
	a = list(dp)
	a.sort()
	for i in range(len(b)):
		for j in range(i + 1, len(b)):
			if dp[a[i]] >= a[j] and dp[a[j]] >= a[i]:
				ans += 2
			elif dp[a[i]] < a[j]:
				break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(c) for c in input().split()]
	s = 0
	d = []
	h = [0] * (max(a) + 1)
	for i in range(n):
		h[a[i]] += 1
	for i in range(len(h)):
		if h[i] >= 1:
			d.append([i, h[i]])
	d = sorted(d, key=lambda x: x[0])
	for i in range(len(d)):
		temp = d[i][0]
		temp1 = d[i][1]
		for j in range(i, len(d)):
			if d[j][0] > temp1:
				break
			if d[j][1] >= temp:
				if i == j:
					s += 1
				else:
					s += 2
	print(s)


for i in range(0, int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	d = {}
	for item in l1:
		if item not in d:
			d[item] = 1
		else:
			d[item] = d[item] + 1
	c = 0
	key_max = max(d.values())
	key_min = min(d.values())
	key_max2 = max(d.keys())
	d = dict(((k, v) for (k, v) in d.items() if k <= key_max and k <= key_max2))
	for i in d:
		for j in range(1, d[i] + 1):
			if j in d:
				if d[i] >= j and d[j] >= i:
					c += 1
	print(c)


test = int(input())
while test:
	input()
	num = input().split(' ')
	num = [int(i) for i in num]
	dic = {}
	for i in num:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	count = 0
	for key in dic:
		for val in range(1, dic[key] + 1):
			if val in dic and dic[val] >= key:
				count += 1
	print(count)
	test -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * (n + 1)
	for x in a:
		b[x] += 1
	c = 0
	for i in range(1, len(b)):
		if i <= b[i]:
			c += 1
		for j in range(i + 1, b[i] + 1):
			if i <= b[j] and j <= b[i]:
				c += 2
	print(c)


t = int(input())
for x in range(t):
	n = int(input())
	l = [int(y) for y in input().split()]
	d = {}
	for each in l:
		try:
			d[each] += 1
		except:
			d[each] = 1
	cnt = 0
	for (key, values) in d.items():
		for y in range(values, 0, -1):
			try:
				if d[y] >= key:
					cnt += 1
			except:
				pass
	print(cnt)


import heapq
from collections import Counter

def readInt():
	return int(input())

def readInts():
	return [int(x) for x in input().split()]

def cases():
	numCases = readInt()
	for _ in range(numCases):
		n = readInt()
		sequence = readInts()
		yield (n, sequence)

def solve(n, sequence):
	pairs = 0
	seen = []
	items = sorted(Counter(sequence).items())
	for (a, count) in items:
		while len(seen) > 0 and seen[0][0] < a:
			heapq.heappop(seen)
		for (bcount, b) in seen:
			if count >= b and bcount >= a:
				pairs += 2
		if count >= a:
			heapq.heappush(seen, (count, a))
			pairs += 1
	return pairs
for data in cases():
	print(solve(*data))


def search(b, ele, n):
	start = 0
	end = n - 1
	while start <= end:
		mid = int((start + end) / 2)
		if b[mid] == ele:
			return mid
		if b[mid] > ele:
			end = mid - 1
		else:
			start = mid + 1
	if b[mid] >= ele:
		return mid
	return mid - 1
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	b = []
	c = [0]
	i = 0
	j = 0
	while i < n:
		b.append(a[i])
		while i < n and a[i] == b[j]:
			c[j] += 1
			i += 1
		j += 1
		c.append(0)
	c.pop()
	ans = 0
	for i in range(j):
		k = i
		while k < j and c[i] >= b[k]:
			if c[k] >= b[i]:
				if k == i:
					ans += 1
				else:
					ans += 2
			k += 1
	print(ans)


for _ in range(int(input())):
	N = int(input())
	A = [int(x) for x in input().split()]
	biggestNumber = max(A)
	freq = ['*'] + [0] * biggestNumber
	for a in A:
		freq[a] += 1
	biggestFreq = max(freq[1:])
	goodAnswers = 0
	for a in range(1, min(biggestFreq, biggestNumber) + 1):
		for b in range(1, min(biggestFreq, biggestNumber, freq[a]) + 1):
			if freq[b] >= a:
				goodAnswers += 1
	print(goodAnswers)


import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	mp = {}
	alist = set(arr)
	alist = list(alist)
	alist.sort()
	l = len(alist)
	for i in arr:
		if i in mp:
			mp[i] += 1
		else:
			mp[i] = 1
	cnt = 0
	for i in alist:
		if mp[i] >= i:
			cnt += 1
		index = bisect.bisect_right(alist, mp[i])
		for j in range(index):
			if i == alist[j]:
				continue
			if mp[alist[j]] >= i:
				cnt += 1
	print(cnt)


from collections import defaultdict
t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	fq = defaultdict(int)
	for w in arr:
		fq[w] += 1
	dit = dict(fq)
	ans = 0
	for (key, value) in dit.items():
		if key <= value:
			i = key + 1
			ans += 1
			while i <= value:
				if i in dit and dit[i] >= key:
					ans += 2
				i += 1
	print(ans)
	t -= 1


from collections import Counter
T = int(input())
for q in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	count = Counter()
	for i in range(N):
		count[A[i]] += 1
	ans = 0
	for key in count:
		for b in range(1, count[key] + 1):
			if count[b] >= key:
				ans += 1
	print(ans)


def fun():
	test = int(input())
	for i in range(test):
		n = int(input())
		arr = list(map(int, input().split()))
		d = dict()
		for i in arr:
			d[i] = d.get(i, 0) + 1
		ans = 0
		doubles = 0
		for i in d.keys():
			if d[i] >= i:
				ans += 1
		for i in d.keys():
			if d[i] > i:
				for j in range(i + 1, d[i] + 1):
					if d.get(j, 0) >= i:
						doubles += 1
		print(doubles * 2 + ans)
fun()


def solve(N, A):
	F = [0] * (max(A) + 1)
	for x in A:
		F[x] += 1
	ret = 0
	for (i, f) in enumerate(F):
		for j in range(1, f + 1):
			if j >= len(F):
				break
			if F[j] >= i:
				ret += 1
	return ret
T = int(input())
for i in range(0, T):
	N = int(input())
	A = [int(s) for s in input().split(' ') if s != '']
	print(solve(N, A))


for q in range(int(input())):
	n = int(input())
	l = [int(e) for e in input().split()]
	m = {}
	s = []
	for i in l:
		if i in m:
			m[i] += 1
		else:
			m[i] = 1
			s.append(i)
	s.sort()
	c = 0
	for i in s:
		for j in range(i, m[i] + 1):
			if j == i:
				c += 1
			elif j in m and m[j] >= i:
				c += 2
	print(c)


tests = int(input())
for t in range(tests):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	count = {}
	for i in range(n):
		el = arr[i]
		count[el] = count.get(el, 0) + 1
	result = 0
	for key in count.keys():
		count1 = count[key]
		for i in range(1, count1 + 1):
			count2 = count.get(i, 0)
			if count2 >= key:
				result += 1
	print(result)


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		if a[i] in d:
			d[a[i]] += 1
		else:
			d[a[i]] = 1
	nos = list(d.keys())
	freq = list(d.values())
	l = len(nos)
	ans = 0
	for i in d:
		f = d[i]
		for j in range(1, f + 1):
			if j in d and d[j] >= i:
				ans += 1
	print(ans)