You have $n$ students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the $i$-th student skill is denoted by an integer $a_i$ (different students can have the same skills).

So, about the teams. Firstly, these two teams should have the same size. Two more constraints:  The first team should consist of students with distinct skills (i.e. all skills in the first team are unique).  The second team should consist of students with the same skills (i.e. all skills in the second team are equal). 

Note that it is permissible that some student of the first team has the same skill as a student of the second team.

Consider some examples (skills are given):  $[1, 2, 3]$, $[4, 4]$ is not a good pair of teams because sizes should be the same;  $[1, 1, 2]$, $[3, 3, 3]$ is not a good pair of teams because the first team should not contain students with the same skills;  $[1, 2, 3]$, $[3, 4, 4]$ is not a good pair of teams because the second team should contain students with the same skills;  $[1, 2, 3]$, $[3, 3, 3]$ is a good pair of teams;  $[5]$, $[6]$ is a good pair of teams. 

Your task is to find the maximum possible size $x$ for which it is possible to compose a valid pair of teams, where each team size is $x$ (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team.

You have to answer $t$ independent test cases.


-----Input-----

The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.

The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of students. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the skill of the $i$-th student. Different students can have the same skills.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).


-----Output-----

For each test case, print the answer — the maximum possible size $x$ for which it is possible to compose a valid pair of teams, where each team size is $x$.


-----Example-----
Input
4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3

Output
3
1
0
2



-----Note-----

In the first test case of the example, it is possible to construct two teams of size $3$: the first team is $[1, 2, 4]$ and the second team is $[4, 4, 4]$. Note, that there are some other ways to construct two valid teams of size $3$.

Difficulty: EASY

Solutions:
import atexit
import io
import sys
_I_B = sys.stdin.read().splitlines()
input = iter(_I_B).__next__
_O_B = io.StringIO()
sys.stdout = _O_B

@atexit.register
def write():
	sys.__stdout__.write(_O_B.getvalue())

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		d = dict()
		for x in a:
			try:
				d[x] += 1
			except KeyError:
				d[x] = 1
		ld = len(d)
		ans = -1
		for i in d:
			if d[i] == 1:
				ans = max(ans, min(d[i], ld - 1))
			else:
				ans = max(ans, max(min(ld - 1, d[i]), min(ld, d[i] - 1)))
		print(ans)
main()


import sys

def main():
	t = int(input())
	for _ in range(t):
		n = int(sys.stdin.readline().strip())
		nums = list(map(int, sys.stdin.readline().split()))
		numDict = {}
		if n <= 1:
			print(0)
			continue
		maxKey = nums[0]
		for i in nums:
			if i not in numDict:
				numDict[i] = 1
			else:
				numDict[i] += 1
			if numDict[i] > numDict[maxKey]:
				maxKey = i
		if numDict[maxKey] == 1:
			print(1)
			continue
		elif numDict[maxKey] - 1 >= len(numDict):
			print(len(numDict))
		else:
			print(min(len(numDict) - 1, numDict[maxKey]))
	return
main()


n = int(input())
for i in range(n):
	a = int(input())
	b = [int(i) for i in input().split(' ')]
	count = {}
	for i in b:
		if i not in count:
			count[i] = 1
		else:
			count[i] += 1
	maxi = len(count)
	max2 = 0
	for i in count:
		if max2 < count[i]:
			max2 = count[i]
	if max2 < maxi:
		print(max2)
	elif max2 == maxi:
		print(max2 - 1)
	else:
		print(maxi)


from collections import Counter

def most_frequent(List):
	dict = {}
	(count, itm) = (0, '')
	for item in reversed(List):
		dict[item] = dict.get(item, 0) + 1
		if dict[item] >= count:
			(count, itm) = (dict[item], item)
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	mf = most_frequent(l)
	de = len(Counter(l).keys()) - 1
	if mf == de:
		print(mf)
	elif mf < de:
		print(mf)
	elif mf - de >= 2:
		print(de + 1)
	else:
		print(de)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		di = {}
		for i in a:
			if i not in di:
				di[i] = 0
			di[i] += 1
		maxi = 1
		for i in di:
			maxi = max(di[i], maxi)
		individual_solutions = len(set(a))
		if maxi > individual_solutions:
			print(individual_solutions)
		elif maxi == individual_solutions:
			print(individual_solutions - 1)
		else:
			print(maxi)


from collections import Counter
TC = int(input())
for __ in range(TC):
	n = int(input())
	a = list(map(int, input().split()))
	dist = len(Counter(a))
	same = Counter(a).most_common(1)[0][1]
	print(max(min(same - 1, dist), min(same, dist - 1)))


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	x.sort()
	count = 1
	count2 = 1
	ma = 0
	for i in range(1, n):
		if x[i] != x[i - 1]:
			count += 1
			count2 = 1
		else:
			count2 += 1
		ma = max(ma, count2)
	print(max(min(count, ma - 1), min(count - 1, ma)))


def distribution(n):
	return (n - 1) // 2

def problem_A():
	print(distribution(int(input())))
s = 'abcdefghijklmnopqrstuvwxyz'

def cons_str(n, b):
	a = s[:b]
	ans = ''
	while n > b:
		ans += a
		n -= b
	ans += a[:n]
	return ans

def problem_B():
	(n, a, b) = list(map(int, input().split()))
	print(cons_str(n, b))

def comp_teams(a):
	if len(a) < 2:
		return 0
	s = len(set(a))
	if s == len(a):
		return 1
	a.sort()
	t = 0
	temp = 1
	for i in range(1, len(a)):
		if a[i] == a[i - 1]:
			temp += 1
		else:
			if t < temp:
				t = temp
			temp = 1
	if t < temp:
		t = temp
	if s <= t - 1:
		return s
	if s == t:
		return s - 1
	else:
		return t

def problem_C():
	l = int(input())
	a = list(map(int, input().split()))
	print(comp_teams(a))

def antisudoku(a):
	for i in range(9):
		for j in range(9):
			if a[i][j] == '2':
				a[i][j] = '1'
	return a

def problem_D():
	a = []
	for _ in range(9):
		a.append(list(input()))
	b = antisudoku(a)
	for i in range(9):
		print(''.join(b[i]))

def three_block_palindrome(s):
	a = []
	b = []
	c = []
	l = len(s)
	for _ in range(l):
		a = []
		for _ in range(l + 1):
			a.append([0] * 26)
	l += 1
	for i in range(1, l):
		k = s[i - 1] - 1
		for j in range(26):
			a[i][j] = a[i - 1][j]
		a[i][k] += 1
	maxlength = 0
	for i in range(l):
		for j in range(i, l):
			maxa = 0
			maxb = 0
			for k in range(26):
				if a[l - 1][k] - a[j][k] > a[i][k]:
					if maxa < a[i][k]:
						maxa = a[i][k]
				elif maxa < a[l - 1][k] - a[j][k]:
					maxa = a[l - 1][k] - a[j][k]
				if maxb < a[j][k] - a[i][k]:
					maxb = a[j][k] - a[i][k]
			if maxlength < 2 * maxa + maxb:
				maxlength = 2 * maxa + maxb
	return maxlength

def problem_E1():
	l = int(input())
	s = list(map(int, input().split()))
	print(three_block_palindrome(s))
cases = int(input())
for _ in range(cases):
	problem_C()


import collections

def solveAll():
	nbCases = int(input())
	for _ in range(nbCases):
		case = readCase()
		print(solve(case))

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

def solve(case):
	nbSkills = len(set(case))
	mostCounted = computeMostCounted(case)
	if nbSkills < mostCounted[1]:
		return nbSkills
	elif nbSkills > mostCounted[1]:
		return mostCounted[1]
	else:
		return nbSkills - 1

def computeMostCounted(case):
	counter = collections.Counter(case)
	return max(counter.items(), key=lambda item: item[1])
solveAll()


def bucket_sort(values):
	m = min(values)
	M = max(values)
	buckets = [0 for x in range(M - m + 1)]
	maxReps = 0
	for v in values:
		buckets[v - m] += 1
		if buckets[v - m] > maxReps:
			maxReps = buckets[v - m]
	different = 0
	for b in buckets:
		if b > 0:
			different += 1
	return (maxReps, different)
tests = int(input())
for _ in range(tests):
	n = int(input())
	skills = [int(x) for x in input().split()]
	(maxReps, diff) = bucket_sort(skills)
	res = min(n // 2, maxReps, diff)
	if res == maxReps and res == diff:
		res -= 1
	print(res)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	counter = Counter()
	max_x = 0
	for i in range(n):
		counter[a[i]] += 1
	for k in counter:
		max_x = max(max_x, counter[k])
	if n == 1:
		print(0)
		continue
	if len(counter) == 1:
		print(1)
	elif max_x > len(counter):
		print(len(counter))
	else:
		print(min(max_x, len(counter) - 1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = {}
	for i in a:
		if i in b:
			b[i] += 1
		else:
			b[i] = 1
	c = []
	d = {}
	for i in b:
		if b[i] == 1:
			c.append(i)
		else:
			d[i] = b[i]
	l = len(d)
	p = len(c)
	if p == 0:
		if l == 0:
			print(0)
		if l == 1:
			print(1)
		if l > 1:
			s = 0
			for i in d:
				x = d[i]
				s = max(s, min(x, l - 1))
				s = max(s, min(x - 1, l))
			print(s)
	elif l == 0:
		if p == 1:
			print(0)
		if p >= 2:
			print(1)
	else:
		s = 0
		for i in d:
			x = d[i]
			s = max(s, min(x, p + l - 1))
			s = max(s, min(x - 1, p + l))
		print(s)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	q = Counter(a)
	a2 = []
	cv = list(set(a))
	cv1 = len(cv)
	for (x, y) in q.items():
		a2.append(y)
	a2.sort(reverse=True)
	ss = a2[0]
	if ss == cv1:
		print(ss - 1)
	else:
		print(min(ss, cv1))


import math

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	maxfrq = -1
	frqEle = -1
	frq = {}
	for x in a:
		if x not in frq:
			frq[x] = 1
		else:
			frq[x] += 1
		if frq[x] > maxfrq:
			maxfrq = frq[x]
			frqEle = x
	distincts = 0
	for x in frq:
		if x != frqEle:
			distincts += 1
	if distincts < maxfrq - 1:
		print(distincts + 1)
	else:
		print(min(distincts, maxfrq))
for _ in range(int(input())):
	solve()


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = Counter(a)
	maxCnt = max(c.values())
	ans = 0
	ans = max(min(maxCnt, len(c) - 1), min(maxCnt - 1, len(c)))
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	a_set = set(a)
	a_dict = {}
	maxim = 1
	for k in a:
		if k in a_dict.keys():
			a_dict[k] += 1
			if a_dict[k] > maxim:
				maxim = a_dict[k]
		else:
			a_dict[k] = 1
	count1 = len(a_set)
	count2 = maxim
	if count2 == count1:
		print(count1 - 1)
	else:
		print(min(count1, count2))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = dict()
	for i in l:
		if i not in x:
			x[i] = 1
		else:
			x[i] += 1
	count = 0
	maxi = 0
	for i in x:
		count += 1
		if x[i] > maxi:
			maxi = x[i]
	if count == maxi:
		print(count - 1)
	else:
		print(min(count, maxi))


def yo():
	from collections import Counter
	t = int(input().strip())
	for i in range(t):
		n = int(input().strip())
		students = list(map(int, input().split()))
		skills = Counter(students)
		max_num = skills.most_common(1)[0][1]
		if max_num <= len(skills) - 1:
			print(max_num)
		elif max_num == len(skills):
			print(max_num - 1)
		else:
			print(len(skills))
yo()


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	c = 1
	b = 0
	count = 0
	flag = 0
	for i in range(n):
		if i < n - 1 and a[i] == a[i + 1]:
			c += 1
		else:
			b += 1
			if c > count:
				count = c
			c = 1
	if count <= b:
		b -= 1
	print(min(b, count))
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	skills = [int(i) for i in input().split()]
	d = {}
	maxim = 0
	for skill in skills:
		if skill in d:
			d[skill] += 1
		else:
			d[skill] = 1
		if d[skill] > maxim:
			maxim = d[skill]
	distinct = len(d)
	if distinct > maxim:
		print(maxim)
	elif distinct == maxim:
		print(maxim - 1)
	elif distinct < maxim:
		print(distinct)


from collections import *
for _ in range(int(input())):
	n = int(input())
	r = list(map(int, input().split()))
	c = {}
	p = 0
	for k in r:
		if k not in c:
			c[k] = 0
		c[k] += 1
		if c[k] > p:
			p = c[k]
	ans = min(len(c) - 1, p)
	for k in c:
		if c[k] == p:
			break
	if c[k] - 1 >= len(c):
		ans += 1
	print(ans)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	l = li()
	cnt = Counter(l)
	ans = 0 if n == 1 else 1
	s = max(cnt.values()) - 1
	le = len(cnt)
	ans = min(le, s)
	if le - 1 == s + 1:
		ans = max(ans, s + 1)
	ans = max(ans, min(s + 1, le - 1))
	ans = max(ans, 0 if n == 1 else 1)
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	else:
		d = {}
		for i in a:
			if i not in d.keys():
				d[i] = 1
			else:
				d[i] += 1
		val = max(d.values())
		k = len(d.keys()) - 1
		a1 = min(k + 1, val - 1)
		a2 = min(k, val)
		print(max(a1, a2))


import sys
from collections import Counter
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def print_list_int(l):
	print(' '.join(map(str, map(int, l))))
t = inp()
for i in range(t):
	n = inp()
	arr = inlt()
	c = Counter(arr)
	most = c.most_common()[0][1]
	dif = len(c.most_common()) - 1
	print(int(max(min(most, dif), min(most - 1, dif + 1))))


t = int(input())
for j in range(t):
	n = int(input())
	diff_set = set()
	odinak_dict = dict()
	a = list(map(int, input().split()))
	for stud in a:
		diff_set.add(stud)
		if stud not in odinak_dict:
			odinak_dict[stud] = 1
		else:
			odinak_dict[stud] += 1
	diff = len(diff_set)
	max_val = 0
	max_key = 0
	for key in odinak_dict:
		if odinak_dict[key] > max_val:
			max_val = odinak_dict[key]
			max_key = key
	if diff > max_val:
		print(max_val)
	elif diff == max_val:
		print(max_val - 1)
	else:
		print(diff)


from operator import itemgetter
import sys
input = sys.stdin.readline

def compress(string):
	string.append('#')
	n = len(string)
	(begin, end, cnt) = (0, 1, 1)
	ans = []
	while end < n:
		if string[begin] == string[end]:
			(end, cnt) = (end + 1, cnt + 1)
		else:
			ans.append((string[begin], cnt))
			(begin, end, cnt) = (end, end + 1, 1)
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	comp = compress(a)
	comp = sorted(comp, key=itemgetter(1))
	same_max = comp[-1][-1]
	kind_max = len(comp)
	if same_max == kind_max:
		print(same_max - 1)
	else:
		print(min(same_max, kind_max))


import string
n = int(input())
for i in range(n):
	count = int(input())
	students = [int(i) for i in input().split()]
	m = dict()
	for student in students:
		if student in m.keys():
			m[student] += 1
		else:
			m[student] = 1
	highest = 0
	keyHighest = 0
	for (key, value) in m.items():
		if value >= highest:
			highest = value
			keyHighest = key
	if len(m) > highest:
		print(highest)
	elif len(m) == highest:
		print(len(m) - 1)
	else:
		print(len(m))


for t in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	freq = {A[0]: 1}
	maxFrq = 1
	maxEle = A[0]
	for i in range(1, n):
		if A[i] not in freq:
			freq[A[i]] = 0
		freq[A[i]] += 1
		if freq[A[i]] > maxFrq:
			maxFrq = freq[A[i]]
			maxEle = A[i]
	if n == 1:
		print(0)
	else:
		uniqueElem = len(freq.keys()) - 1
		diff = uniqueElem - maxFrq
		if diff >= 0:
			print(maxFrq)
		elif diff == -1:
			print(uniqueElem)
		else:
			print(uniqueElem + 1)


def foo(n, a):
	a.sort()
	num_distinct = 1
	for i in range(0, n - 1):
		if a[i] != a[i + 1]:
			num_distinct += 1
	return num_distinct

def bar(n, a):
	list_dickt = []
	dickt = {}
	for el in a:
		if el not in dickt:
			dickt[el] = 1
		else:
			dickt[el] += 1
	list_dickt = list(dickt.items())
	return dickt[sorted(sorted(list_dickt, key=lambda i: i[0]), key=lambda i: i[1])[-1][0]]
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	q = foo(n, a)
	w = bar(n, a)
	if q == w:
		print(q - 1)
	elif q > w:
		print(w)
	else:
		print(q)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr_counter = Counter(arr)
	arr_condensed = set(arr)
	max_individual = len(arr_condensed) - 1
	max_same = arr_counter[arr_counter.most_common(1)[0][0]]
	if max_same >= max_individual + 2:
		max_same -= 1
		max_individual += 1
	print(min(max_individual, max_same))


from collections import Counter
for i in range(int(input())):
	n = int(input())
	l = [int(j) for j in input().split()][:n]
	if n == 1:
		print('0')
	elif n == 2:
		print('1')
	else:
		s = set(l)
		d = dict(Counter(l))
		m = -1
		p = -1
		for (j, k) in d.items():
			if k > p:
				m = j
				p = k
		if p == len(s):
			print(p - 1)
		else:
			print(min(p, len(s)))


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	counts = Counter(A)
	numDistinct = len(counts)
	best = 0
	for (k, v) in counts.items():
		best = max(best, min(numDistinct - 1, v), min(numDistinct, v - 1))
	return best
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


t = int(input())
cases = []
for i in range(t):
	n = int(input())
	cases.append([int(item) for item in input().split()])
for c in cases:
	n = len(c)
	c.sort()
	counts = [0]
	for i in range(c[-1]):
		counts.append(0)
	for x in c:
		counts[x] += 1
	m = 0
	m_i = -1
	for i in range(len(counts)):
		if counts[i] > m:
			m = counts[i]
			m_i = i
	u = len(list(set(c)))
	max_size = n // 2
	if u == m:
		max_size = min(max_size, min(m, u) - 1)
	else:
		max_size = min(max_size, min(m, u))
	print(max_size)


m = int(input())
while m > 0:
	n = int(input())
	a = list(map(int, input().split()))
	db = dict()
	for i in range(n):
		if a[i] not in db:
			db[a[i]] = 1
		else:
			db[a[i]] += 1
	(ans, mx) = (0, 0)
	d = len(db)
	for i in range(n):
		mx = max(mx, db[a[i]])
	if d == mx:
		ans = d - 1
	elif d > mx:
		ans = mx
	else:
		ans = d
	if n == 1:
		ans = 0
	print(ans)
	m -= 1


def process_array(array, size):
	counter_array = [0 for i in range(size + 1)]
	non_zero = 0
	max_num = 1
	for a in array:
		if counter_array[a] == 0:
			non_zero += 1
		else:
			max_num = max(max_num, counter_array[a] + 1)
		counter_array[a] += 1
	return max(min(non_zero - 1, max_num), min(non_zero, max_num - 1))
test_num = int(input())
for i in range(test_num):
	n = int(input())
	array = list(map(int, input().split()))
	test_answer = process_array(array, n)
	print(test_answer)


t = int(input())
from collections import Counter
for qwe in range(t):
	n = int(input())
	listt = list(map(int, input().split()))
	co = Counter(listt)
	sett = set(listt)
	maxx = co.most_common(1)[0][1]
	lenn = len(sett)
	if maxx < lenn:
		print(maxx)
	elif maxx == lenn:
		print(lenn - 1)
	elif maxx > lenn:
		print(lenn)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True
for t in range(ii()):
	n = ii()
	arr = list(mi())
	d = collections.defaultdict(int)
	mx = 1
	for i in arr:
		d[i] += 1
		if d[i] > mx:
			mx = d[i]
	l = len(list(d.keys()))
	if l >= mx:
		print(min(l - 1, mx))
	else:
		print(min(l, mx))


import sys
from collections import Counter
t = int(sys.stdin.readline().rstrip())

def main():
	for _ in range(t):
		n = int(sys.stdin.readline().rstrip())
		(*a,) = map(int, sys.stdin.readline().split())
		if n == 1:
			print(0)
			continue
		elif n == 2:
			print(1)
			continue
		c = Counter(a)
		if len(c) == 1:
			print(1)
			continue
		else:
			m = max(c.values())
			if m - 1 >= len(c):
				print(len(c))
			else:
				print(min(len(c) - 1, m))
main()


import math
from collections import Counter

def most_frequent(List):
	occurence_count = Counter(List)
	return occurence_count.most_common(1)[0][0]
t = int(input())
for g in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	x = most_frequent(a)
	k = 0
	for i in range(0, len(a)):
		if x == a[i]:
			k = k + 1
	y = len(b) - 1
	if k == y:
		print(k)
	elif k < y:
		print(k)
	elif k - y >= 2:
		print(y + 1)
	else:
		print(y)


import sys
from collections import Counter
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	tmp = Counter(map(int, input().split()))
	sortedTmp = tmp.most_common()
	cl = len(sortedTmp)
	if cl == 1:
		if sortedTmp[0][1] > 1:
			print(1)
		else:
			print(0)
		continue
	ans = min(cl - 1, sortedTmp[0][1])
	if sortedTmp[0][1] > cl:
		ans = max(ans, cl)
	print(ans)


dp = [[999999999999] * 100 for i in range(100)]
from math import ceil
from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	c = Counter(nums)
	diff = c.most_common(1)[0][1]
	tot = 0
	for i in c.values():
		if i >= 1:
			tot += 1
	print(max(min(diff, tot - 1), min(diff - 1, tot)))


import sys
import string
from collections import defaultdict

def ag(L):
	d = defaultdict(int)
	for i in L:
		d[i] += 1
	s = d.values()
	return max(s)
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = list(set(l))
	l1 = len(s)
	l2 = ''.join([str(i) for i in l])
	g = ag(l)
	if l1 - 1 >= g:
		print(g)
	else:
		print(min(l1, g - 1))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(int)
	for i in a:
		d[i] += 1
	dc = len(set(a))
	max_same_count = max(d.values())
	if dc > max_same_count:
		print(max_same_count)
	elif dc < max_same_count:
		print(dc)
	else:
		print(dc - 1)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	m = 0
	for i in range(n):
		if l[i] in d:
			d[l[i]] += 1
			m = max(m, d[l[i]])
		else:
			d[l[i]] = 1
			m = max(m, 1)
	n = len(set(l)) - 1
	if n >= m:
		print(m)
	elif n == m - 1:
		print(n)
	else:
		print(n + 1)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	temp = Counter(arr)
	maxc = max(temp.values())
	sett = len(set(arr)) - 1
	if sett >= maxc:
		print(maxc)
	elif sett + 1 == maxc:
		print(sett)
	else:
		print(sett + 1)


import math

def combination(n, k):
	return math.factorial(n) / (math.factorial(k) * math.factorial(n - k))

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	maxc = 0
	maxa = None
	for i in a:
		d[i] = d.get(i, 0) + 1
		if d[i] > maxc:
			maxc = d[i]
			maxa = i
	del d[maxa]
	rd = len(d.items())
	sol = max(min(rd + 1, maxc - 1), min(rd, maxc))
	print(sol)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


t = int(input())
for testcase in range(t):
	n = int(input())
	A = [int(i) for i in input().split(' ')]
	d = {}
	for i in A:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	distinct = len(d) - 1
	same = 1
	for i in d:
		same = max(d[i], same)
	if same > distinct:
		distinct += 1
		same -= 1
	print(min(distinct, same))


a = int(input())
for _ in range(a):
	l = int(input())
	b = list(map(int, input().split()))
	c = [0] * (l + 1)
	for i in range(l):
		c[b[i]] += 1
	q = set(b)
	p = len(q) - 1
	z = max(c)
	if p > z or abs(p - z) == 1 or p == z:
		print(min(p, z))
	else:
		print(p + 1)


t = int(input())
for test in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	s = set(a)
	m = 1
	temp = 1
	for i in range(n - 1):
		m = max(m, temp)
		if a[i] == a[i + 1]:
			temp += 1
			m = max(m, temp)
		else:
			temp = 1
	print(min(m, len(s)) - (m == len(s)))


from sys import stdin

def binary(arr, l, h, d, rep):
	ans = 1
	while l <= h:
		mid = (l + h) // 2
		if mid == rep:
			if d - 1 >= mid and rep >= mid:
				ans = max(ans, mid)
				l = mid + 1
			else:
				h = mid - 1
		elif d >= mid and rep >= mid:
			ans = max(ans, mid)
			l = mid + 1
		else:
			h = mid - 1
	return ans
for t in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = [int(x) for x in stdin.readline().split()]
	if n > 1:
		aux = [0] * (n + 1)
		d = rep = 0
		for i in arr:
			if aux[i] == 0:
				aux[i] += 1
				d += 1
			else:
				aux[i] += 1
			rep = max(rep, aux[i])
		h = min(n // 2, d, rep)
		print(binary(arr, 0, h, d, rep))
	else:
		print(0)


import math

def two_teams_composing(n, a):
	table = {}
	team1 = 0
	team2 = 1
	for i in range(0, n):
		if not a[i] in table:
			table[a[i]] = 1
			team1 += 1
		else:
			table[a[i]] += 1
			team2 = max(team2, table[a[i]])
	if team1 > team2:
		print(team2)
	elif team1 == team2:
		print(team2 - 1)
	else:
		print(team1)
t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	two_teams_composing(n, a)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter
for j in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	d = Counter(arr)
	ma = d.most_common(1)
	m = ma[0][1]
	l = len(d) - 1
	if l >= m:
		print(m)
	elif l + 1 == m:
		print(l)
	else:
		print(l + 1)


t = int(input())
D = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	Dict = {}
	for i in arr:
		if i in Dict:
			Dict[i] += 1
		else:
			Dict[i] = 1
	Dict = dict(sorted(Dict.items(), key=lambda x: -x[1]))
	Max = -1
	for i in Dict:
		Max = max(Max, Dict[i])
		break
	if Max == len(Dict):
		print(len(Dict) - 1)
	elif Max < len(Dict):
		print(Max)
	else:
		print(len(Dict))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = {}
	for i in a:
		x[i] = x.get(i, 0) + 1
	(m, d) = (0, 0)
	for j in x.values():
		if j > m:
			m = j
		if j > 0:
			d += 1
	if n == 1:
		print(0)
	elif m == 1:
		print(1)
	elif d > m:
		print(m)
	elif d == m:
		print(d - 1)
	else:
		print(d)


T = int(input())

def main():
	n = int(input())
	a = list(map(int, input().split()))
	skills = [0] * n
	for i in range(n):
		skills[a[i] - 1] += 1
	maxSkill = max(skills)
	unique = 0
	for i in range(n):
		if skills[i] > 0:
			unique += 1
	unique -= 1
	if maxSkill >= unique + 2:
		return unique + 1
	elif maxSkill == unique + 1:
		return unique
	elif maxSkill <= unique:
		return maxSkill
for _ in range(T):
	print(main())


import math

def process():
	n = int(input())
	st = input()
	b = st.split(' ')
	a = []
	for x in b:
		a.append(int(x))
	myset = set(a)
	mydict = {}
	for x in myset:
		mydict[x] = 0
	for x in a:
		mydict[x] = mydict[x] + 1
	maxi = 0
	argmax = 0
	for x in myset:
		if mydict[x] > maxi:
			maxi = mydict[x]
			argmax = x
	print(max(min(len(myset) - 1, maxi), min(len(myset), maxi - 1)))
t = int(input())
for i in range(0, t):
	process()


from collections import defaultdict
import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(el) for el in input().split()]
	d = defaultdict(int)
	for el in a:
		d[el] += 1
	m = max(d.values())
	if len(d) > m:
		print(m)
	elif len(d) == m or len(d) == m + 1:
		print(m - 1)
	else:
		print(len(d))


t = int(input())
sol = []
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = {}
	r = 0
	for x in a:
		s[x] = s.get(x, 0) + 1
		r = max(r, s[x])
	d = len(s)
	tmp = min(d, r)
	if d == r:
		tmp -= 1
	sol.append(str(tmp))
print('\n'.join(sol))


t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n > 1:
		hashmap = {}
		unique = 0
		common_max = 1
		for num in a:
			if num in hashmap.keys():
				hashmap[num] += 1
				if common_max < hashmap[num]:
					common_max = hashmap[num]
			else:
				hashmap[num] = 1
				unique += 1
		if common_max == unique:
			print(common_max - 1)
		else:
			print(min(unique, common_max, len(a) // 2))
	else:
		print(0)


t = int(input())
for j in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if n == 1:
		print(0)
	else:
		a = sorted(a)
		b = []
		k = 1
		for i in range(1, n):
			if a[i] == a[i - 1]:
				k += 1
			else:
				b.append(k)
				k = 1
		b.append(k)
		q = len(b)
		print(max(min(q - 1, max(b)), min(q, max(b) - 1)))


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(i) for i in input().split()]
	dic = {}
	for i in arr:
		dic[i] = dic.get(i, 0) + 1
	maxi = 0
	for i in dic:
		if dic[i] > maxi:
			maxi = dic[i]
	if len(dic) == maxi:
		print(maxi - 1)
	elif len(dic) > maxi:
		print(maxi)
	elif len(dic) < maxi:
		print(len(dic))


def solve(skills):
	counts = [0] * (max(skills) + 1)
	for skill in skills:
		counts[skill] += 1
	a1 = max(counts)
	a2 = len(list(filter(lambda x: x > 0, counts)))
	if a1 == a2:
		return a2 - 1
	return min(a1, a2)
for i in range(int(input())):
	n = int(input())
	skills = [int(x) for x in input().split()]
	print(solve(skills))


t = int(input())
for _ in range(t):
	n = int(input())
	freq = [0 for i in range(n + 1)]
	a = list(map(int, input().split()))
	for i in range(n):
		freq[a[i]] += 1
	x = max(freq)
	y = len(set(a))
	ans = max(min(x - 1, y), min(y - 1, x))
	print(ans)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	skills = [int(x) for x in input().split()]
	counter = Counter(skills)
	unique = len(counter)
	mostly = max(counter.values())
	if unique < mostly:
		print(unique)
	elif unique == mostly:
		print(mostly - 1)
	elif unique > mostly:
		print(mostly)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	if n == 1:
		print(0)
		continue
	for i in l:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	uni = 0
	same = 0
	max = 0
	for k in sorted(d.values(), reverse=True):
		if k > 1:
			if max < k and max != 0:
				same = k
				max = k
				uni += 1
			elif max < k and max == 0:
				same = k
				max = k
			else:
				uni += 1
		else:
			uni += 1
	if same == 0:
		lar = 1
	elif same > uni + 1:
		uni += 1
		lar = uni
	elif same == uni + 1:
		lar = uni
	else:
		lar = same
	print(lar)


from collections import Counter as cn
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = cn(a)
	fg = 1
	iv = 0
	cv = 0
	b = b.most_common()
	iv = b[0][1]
	for i in b[1:]:
		if cv < iv:
			cv += 1
		else:
			break
	if iv - cv >= 2:
		cv += 1
	print(cv)


for _ in range(int(input())):
	(n, a, x) = (int(input()), map(int, input().split()), {})
	for i in a:
		x[i] = x.get(i, 0) + 1
	(k, w) = (len(x.keys()), max(x.values()))
	if k == w:
		print(k - 1)
	else:
		print(min(k, w))


def teamcomp(n, a):
	if n == 1:
		return 0
	fre = {}
	same = 0
	unique = 0
	for i in a:
		if i in fre:
			fre[i] += 1
			same = max(same, fre[i])
		else:
			fre[i] = 1
			unique += 1
			same = max(same, fre[i])
	if same == unique - 1:
		return same
	else:
		val1 = min(same, unique - 1)
		val2 = min(same - 1, unique)
		return max(val1, val2)
test = int(input())
for i in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	print(teamcomp(n, a))


t = int(input())
for tt in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	max_freq = 0
	max_rep_val = 0
	l.sort()
	if n == 1:
		print(0)
	else:
		for i in l:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			if max_freq < d[i]:
				max_freq = d[i]
				max_rep_val = i
		first = []
		for i in l:
			if i != max_rep_val:
				if i in d:
					first.append(i)
					del d[i]
		if d[max_rep_val] < len(first):
			print(d[max_rep_val])
		elif d[max_rep_val] > len(first):
			if d[max_rep_val] - len(first) > 1:
				print(len(first) + 1)
			else:
				print(len(first))
		elif d[max_rep_val] == len(first):
			print(len(first))


def IntInput():
	return map(int, input().split())

def StrInput():
	return map(int, input().split())

def IntArrayInput():
	return list(map(int, input().split()))

def modInverse(p, a, m):
	return p * pow(a % m, m - 2, m) % m
for _ in range(int(input())):
	n = int(input())
	array = IntArrayInput()
	array.sort()
	dictV = {}
	UniqueCount = 0
	ans = 0
	maxCount = 0
	for i in range(len(array)):
		if array[i] in dictV:
			dictV[array[i]] += 1
		else:
			dictV[array[i]] = 1
			UniqueCount += 1
		if dictV[array[i]] > maxCount:
			maxCount = dictV[array[i]]
	if UniqueCount == maxCount:
		print(UniqueCount - 1)
	elif UniqueCount > maxCount:
		print(maxCount)
	else:
		print(UniqueCount)


t = int(input())
for i in range(t):
	n = int(input())
	q = [int(x) for x in input().split()]
	d = dict()
	for j in q:
		d[j] = d.get(j, 0) + 1
	ans = max(d, key=d.get)
	ans = d[ans]
	if ans == 1 and len(d) == 1:
		print('0')
		continue
	if ans - 1 == len(d) - 1:
		print(ans - 1)
	elif ans > len(d):
		print(len(d))
	elif ans < len(d):
		print(ans)


for _ in range(int(input())):
	w = int(input())
	data = input().split()
	dict = {i: 0 for i in data}
	for i in range(w):
		dict[data[i]] += 1
	req1 = min(len(dict), max(dict.values()) - 1)
	req2 = min(len(dict) - 1, max(dict.values()))
	print(max(req1, req2))


from collections import Counter

def fun():
	t = int(input())
	for k in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		b = dict(Counter(a))
		c = len(b)
		d = max(b.values())
		if d > c:
			print(min(d, c))
		else:
			print(min(d, c - 1))
fun()


import math
testCases = int(input())
for _ in range(testCases):
	n = int(input())
	skills = map(int, input().split())
	skillsMap = dict()
	for skill in skills:
		skillsMap[skill] = skillsMap.get(skill, 0) + 1
	repitedSkills = max(skillsMap.values())
	uniqueSkills = len(skillsMap)
	print(min(uniqueSkills, repitedSkills) - (uniqueSkills == repitedSkills))


from sys import stdin
import math
from collections import Counter
inp = lambda : stdin.readline().strip()
t = int(inp())
for i in range(t):
	n = int(inp())
	arr = [int(x) for x in inp().split()]
	f = Counter(arr).values()
	uniqueSkills = set(arr)
	unique = len(uniqueSkills)
	a = [unique - 1, max(f)]
	x = min(a)
	b = [unique, max(f) - 1]
	y = min(b)
	print(max(x, y))


for _ in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = [0] * (n + 1)
	ans = 0
	ct = 0
	mx = 0
	for i in range(0, n):
		if c[l[i]] == 0:
			ct += 1
		c[l[i]] += 1
		mx = max(mx, c[l[i]])
	if mx <= ct - 1:
		ans = mx
	elif mx > ct:
		ans = ct
	else:
		ans = ct - 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	diff = 0
	for val in arr:
		if val in d:
			d[val] += 1
		else:
			d[val] = 1
	same = max(list(d.values()))
	diff = len(d)
	diff -= 1
	_min = min(same, diff)
	if same >= diff + 2:
		_min += 1
	print(_min)


from math import ceil
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	cnt = {}
	distinct = len(set(a)) - 1
	maxCount = 0
	for i in a:
		cnt[i] = cnt.get(i, 0) + 1
	for i in cnt:
		maxCount = max(cnt[i], maxCount)
	if maxCount == distinct:
		print(distinct)
	elif distinct + 1 <= maxCount - 1:
		print(distinct + 1)
	elif maxCount > distinct:
		print(distinct)
	else:
		print(maxCount)


from collections import Counter
ans = []
for _ in range(int(input())):
	n = int(input())
	stu = sorted(map(int, input().split()))
	if n < 2:
		ans.append(0)
		continue
	diff = len(set(stu))
	same = Counter(stu).most_common(1)[0][1]
	ans.append(max(min(diff, same - 1), min(diff - 1, same)))
print('\n'.join(map(str, ans)))


t = int(input())
for i in range(t):
	n = int(input())
	A = [int(j) for j in input().split()]
	p1 = set()
	p2 = {}
	for i in A:
		p1.add(i)
		if i in p2:
			p2[i] += 1
		else:
			p2[i] = 1
	x = p2[max(p2, key=lambda a: p2[a])]
	y = len(p1)
	if x > y:
		print(y)
	if x == y:
		print(y - 1)
	if x < y:
		print(x)


NUM_ITR = int(input())
for iteration in range(NUM_ITR):
	N = int(input())
	li = [int(x) for x in input().split(' ')]
	dictionary = {}
	for i in li:
		if i in dictionary:
			dictionary[i] += 1
		else:
			dictionary[i] = 1
	MAX = 0
	for i in dictionary:
		if dictionary[i] > MAX:
			MAX = dictionary[i]
	if MAX > len(dictionary) - 1:
		if MAX == len(dictionary):
			print(len(dictionary) - 1)
		else:
			print(str(len(dictionary)))
	else:
		print(MAX)


t = int(input())
testcaseList = []
for testcaseIndex in range(t):
	n = int(input())
	skillList = list(map(int, input().split()))
	testcaseList.append([n, skillList])
for (n, skillList) in testcaseList:
	skillNumDict = {}
	for skill in skillList:
		if skill not in skillNumDict:
			skillNumDict[skill] = 1
		else:
			skillNumDict[skill] += 1
	skillSetNum = len(skillNumDict)
	largestSkillNum = max(skillNumDict.values())
	if n == 1:
		answer = 0
	elif skillSetNum > largestSkillNum:
		answer = largestSkillNum
	elif skillSetNum == largestSkillNum:
		answer = largestSkillNum - 1
	else:
		answer = skillSetNum
	print(answer)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = {}
	for i in range(n):
		m[a[i]] = m.get(a[i], 0) + 1
	d = len(m)
	res = 0
	for (k, v) in m.items():
		res = max(res, max(min(d, v - 1), min(d - 1, v)))
	print(res)


for _ in range(int(input())):
	n = int(input())
	k = [0] * n
	for i in map(int, input().split()):
		k[i - 1] += 1
	mx = max(k)
	diff = n - k.count(0)
	print(max(min(mx - 1, diff), min(mx, diff - 1)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = map(int, input().split(' '))
	count_by_skill = {}
	for skill in a:
		count_by_skill[skill] = 1 if skill not in count_by_skill else count_by_skill[skill] + 1
	maxi = 0
	for (skill, value) in count_by_skill.items():
		maxi = max(maxi, value)
	print(min(maxi, len(count_by_skill)) if maxi != len(count_by_skill) else maxi - 1)


t = int(input())
stu = []
for i in range(t):
	n = int(input())
	stu.append(list(map(int, input().split())))
for i in stu:
	sdic = {}
	for j in i:
		if j not in sdic:
			sdic[j] = 1
		else:
			sdic[j] += 1
	sdic2 = sorted(sdic.items(), key=lambda x: x[1], reverse=True)
	ans = min(sdic2[0][1], len(sdic2) - 1)
	if sdic2[0][1] > len(sdic2):
		ans += 1
	print(ans)


def call():
	n = int(input())
	num = [int(x) for x in input().split()]
	l = [0] * (n + 1)
	for i in num:
		l[i] += 1
	M = max(l)
	diff = n + 1 - l.count(0)
	print(max(min(M - 1, diff), min(M, diff - 1)))
for i in range(int(input())):
	call()


tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = list(map(int, input().split()))
	cnt = [0] * (n + 1)
	n1 = len(set(arr))
	for x in arr:
		cnt[x] += 1
	n2 = max(cnt)
	print(n1 - 1 if n1 == n2 else min(n1, n2))


from math import ceil, gcd, floor, log
from collections import deque, defaultdict
from heapq import heappush as hpush, heappop as hpop, heapify
from functools import lru_cache
import sys

def inpi():
	return int(input())

def inpa():
	return list(map(int, input().split()))

def inp():
	s = input()
	return list(s)

def inpv():
	return map(int, input().split())
l = 'abcdefghijklmnopqrstuvwxyz'
testcase = int(input())
for test in range(testcase):
	n = inpi()
	a = inpa()
	d = defaultdict(int)
	for i in a:
		d[i] += 1
	s = set(a)
	l = len(s) - 1
	m = max(d.values())
	if m > l:
		print(min(m - 1, l + 1))
	else:
		print(min(m, l))


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('0')
	else:
		dp = [0 for _ in range(n)]
		for i in range(n):
			dp[arr[i] - 1] += 1
		distinct = 0
		repeated = 0
		for i in range(n):
			if dp[i] > 0:
				distinct += 1
				if dp[i] > repeated:
					repeated = dp[i]
		print(max(min(repeated - 1, distinct), min(repeated, distinct - 1)))
	t -= 1


from sys import stdin, stdout

def poss(a, mid):
	d = {}
	for i in range(len(a)):
		if a[i] not in d:
			d[a[i]] = 1
		else:
			d[a[i]] += 1
	(same, dis) = ([], [])
	(cd, cs) = (0, 0)
	(mk, mv) = (0, 0)
	for (k, v) in zip(d.keys(), d.values()):
		if v > mv:
			(mv, mk) = (v, k)
	while cs < mid:
		same.append(mk)
		d[mk] -= 1
		if d[mk] == 0:
			break
		cs += 1
	for (k, v) in zip(d.keys(), d.values()):
		if v >= 1:
			dis.append(k)
			cd += 1
			if cd == mid:
				break
	return len(same) == mid and len(dis) == mid
t = int(stdin.readline())
while t > 0:
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	(low, high) = (1, len(a))
	ans = 0
	while low <= high:
		mid = (low + high) // 2
		if poss(a, mid):
			ans = mid
			low = mid + 1
		else:
			high = mid - 1
	stdout.write('{}\n'.format(ans))
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	b = list(set(a))
	c = []
	n1 = len(b)
	d = 1
	for i in range(1, n):
		if a[i] == a[i - 1]:
			d += 1
			if i == n - 1:
				c.append(d)
		else:
			c.append(d)
			d = 1
	if len(c) == 0:
		if n == 1:
			print(0)
		else:
			print(1)
	else:
		grt = max(c)
		if grt > n1:
			print(n1)
		elif n1 > grt:
			print(grt)
		else:
			print(grt - 1)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	number = dict()
	ma = 0
	one = 0
	for j in arr:
		if j in number:
			number[j] += 1
			ma = max(ma, number[j])
			continue
		number[j] = 1
		ma = max(ma, number[j])
		one += 1
	one -= 1
	if n == 1:
		print(0)
		continue
	ans = min(one, ma)
	if one < ma - 1:
		ans += 1
	print(ans)


from collections import Counter
from sys import stdin
input = stdin.readline
for ti in range(int(input().strip())):
	n = int(input().strip())
	c = Counter([int(x) for x in input().strip().split()])
	ans = 0
	cl = len(c)
	for ci in c:
		if c[ci] > cl:
			ans = max(ans, cl)
		else:
			ans = max(ans, min(cl - 1, c[ci]))
	print(ans)


import collections, math, bisect
local = False
if local:
	file = open('input.txt', 'r')
	import time

def inp():
	if local:
		return file.readline().rstrip()
	else:
		return input().rstrip()

def ints():
	return [int(_) for _ in inp().split()]
if local:
	start = time.time()
t = int(inp())
for _ in range(1, t + 1):
	n = int(inp())
	a = ints()
	ans = 0
	counts = collections.Counter(a).most_common()
	diffNums = len(counts)
	for (num, count) in counts:
		if count >= diffNums:
			count -= 1
			ans = max(ans, min(count, diffNums))
		elif diffNums > count:
			diffNums -= 1
			ans = max(ans, min(count, diffNums))
	print(ans)
if local:
	fin = (time.time() - start) * 1000
	print('{:.2f}'.format(fin) + 'ms')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	s = set()
	max = 0
	if n == 1:
		print(0)
	else:
		for i in l:
			if i not in d.keys():
				d[i] = 1
				if 1 > max:
					max = 1
			else:
				d[i] += 1
				if d[i] > max:
					max = d[i]
			s.add(i)
		length = len(s)
		if length == n:
			print(1)
		elif max == len(s):
			print(max - 1)
		else:
			if max == n // 2 + 1:
				max -= 1
			print(min(len(s), max))


import sys
input = sys.stdin.buffer.readline
cnt = {}
tests = int(input())
for ii in range(tests):
	cnt.clear()
	n = int(input())
	a = [int(x) for x in input().split()]
	for i in range(n):
		cnt[a[i]] = 0
	for i in range(n):
		cnt[a[i]] = cnt[a[i]] + 1
	l = 0
	r = n
	ans = 0
	while l <= r:
		mid = (l + r) // 2
		add = 0
		ind = -1
		tot = 0
		for i in cnt:
			if cnt[i] > mid:
				add = 1
				ind = i
				break
			elif cnt[i] == mid:
				add = 0
				ind = i
		if ind == -1:
			r = mid - 1
			continue
		for i in cnt:
			tot += 1
		tot += add - 1
		if tot >= mid:
			ans = max(ans, mid)
			l = mid + 1
		else:
			r = mid - 1
	print(ans)


from collections import Counter as c
for _ in range(int(input())):
	n = int(input())
	arr = map(int, input().split())
	arr = c(arr)
	arr = list(arr.items())
	arr.sort(key=lambda x: x[1])
	n = len(arr)
	ans = max(min(arr[-1][-1], n - 1), min(arr[-1][-1] - 1, n))
	print(ans)


answer = []
for _ in range(int(input())):
	n = int(input())
	cnt = {}
	for i in list(map(int, input().split())):
		cnt[i] = cnt.get(i, 0) + 1
	mx = max(cnt.values())
	diff = len(cnt)
	answer.append(str(max(min(mx - 1, diff), min(mx, diff - 1))))
print('\n'.join(answer))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	counter = dict(Counter(lst))
	a = n // 2
	x = len(counter)
	y = counter[max(counter, key=counter.get)]
	while a != 0:
		if x == a and y > a:
			break
		if y >= a and x > a:
			break
		a -= 1
	print(a)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = [0] * n
	z = 0
	k = 0
	for j in range(n):
		if l1[l[j] - 1] == 0:
			z += 1
		l1[l[j] - 1] += 1
		k = max(k, l1[l[j] - 1])
	if k == z:
		z -= 1
	print(min(z, k))


INT_MAX = 10 ** 18 + 7
x = 10 ** 9 + 7

def INPUT():
	return list((int(i) for i in input().split()))

def LIST_1D_ARRAY(n):
	return [0 for _ in range(n)]

def LIST_2D_ARRAY(m, n):
	return [[0 for _ in range(n)] for _ in range(m)]
for _ in range(int(input())):
	D = {}
	n = int(input())
	A = INPUT()
	for i in range(n):
		if A[i] in D:
			D[A[i]] += 1
		else:
			D[A[i]] = 1
	subsetmaxlenght = 0
	distinct = len(D)
	for ele in D:
		if D[ele] > subsetmaxlenght:
			subsetmaxlenght = D[ele]
	box = subsetmaxlenght
	if distinct > subsetmaxlenght:
		print(subsetmaxlenght)
	elif distinct == subsetmaxlenght:
		print(distinct - 1)
	else:
		print(distinct)


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dd(int)
	for i in a:
		d[i] += 1
	ma = 0
	r = len(d.keys())
	for i in d.keys():
		ma = max(ma, min(d[i] - 1, r), min(d[i], r - 1))
	print(ma)


for _ in range(int(input())):
	n = int(input())
	x = [int(x) for x in input().split()]
	if n <= 1:
		print(0)
		continue
	d = {}
	mx = 0
	for i in range(n):
		if x[i] not in d:
			d[x[i]] = 1
		else:
			d[x[i]] += 1
		mx = max(d[x[i]], mx)
	s = len(d)
	ans = min(mx, s - 1)
	ans = max(ans, min(mx - 1, s))
	print(ans)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	a = len(s)
	c = []
	occurence_count = Counter(l)
	b1 = occurence_count.most_common(1)[0][0]
	b = l.count(b1)
	if a == b:
		print(a - 1)
	elif a > b:
		print(b)
	else:
		print(a)


t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	li = [int(i) for i in l]
	arr = [0 for i in range(n)]
	for i in li:
		arr[i - 1] += 1
	x = max(arr)
	count = 0
	for i in range(n):
		if arr[i] > 0:
			count += 1
	if x > count:
		print(count)
	elif x == count:
		print(x - 1)
	else:
		print(x)


import collections
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = collections.Counter(a)
	print(max(min(max(c.values()) - 1, len(c)), min(max(c.values()), len(c) - 1)))


import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *

class Scanner:

	@staticmethod
	def int():
		return int(sys.stdin.readline().rstrip())

	@staticmethod
	def string():
		return sys.stdin.readline().rstrip()

	@staticmethod
	def map_int():
		return [int(x) for x in Scanner.string().split()]

	@staticmethod
	def string_list(n):
		return [input() for i in range(n)]

	@staticmethod
	def int_list_list(n):
		return [Scanner.map_int() for i in range(n)]

	@staticmethod
	def int_cols_list(n):
		return [int(input()) for i in range(n)]

class Math:

	@staticmethod
	def gcd(a, b):
		if b == 0:
			return a
		return Math.gcd(b, a % b)

	@staticmethod
	def lcm(a, b):
		return a * b // Math.gcd(a, b)

	@staticmethod
	def divisor(n):
		res = []
		i = 1
		for i in range(1, int(n ** 0.5) + 1):
			if n % i == 0:
				res.append(i)
				if i != n // i:
					res.append(n // i)
		return res

	@staticmethod
	def round_up(a, b):
		return -(-a // b)

	@staticmethod
	def is_prime(n):
		if n < 2:
			return False
		if n == 2:
			return True
		if n % 2 == 0:
			return False
		d = int(n ** 0.5) + 1
		for i in range(3, d + 1, 2):
			if n % i == 0:
				return False
		return True

def pop_count(x):
	x = x - (x >> 1 & 6148914691236517205)
	x = (x & 3689348814741910323) + (x >> 2 & 3689348814741910323)
	x = x + (x >> 4) & 1085102592571150095
	x = x + (x >> 8)
	x = x + (x >> 16)
	x = x + (x >> 32)
	return x & 127
MOD = int(1000000000.0) + 7
INF = int(1000000000000000.0)

def solve():
	_ = Scanner.int()
	A = Scanner.map_int()
	D = defaultdict(int)
	for a in A:
		D[a] += 1
	s = len(D)
	t = max(D.values())
	if t == s:
		s -= 1
	print(min(s, t))

def main():
	T = Scanner.int()
	for _ in range(T):
		solve()
main()


from collections import Counter
n = int(input())
for i in range(n):
	lena = int(input())
	arr = list(map(int, input().split()))
	counter = Counter(arr)
	sim = counter.most_common()[0][1]
	dis = len(set(arr)) - 1
	if lena == 1:
		print(0)
	elif sim - dis > 1:
		print(min(sim, dis, lena // 2) + 1)
	else:
		print(min(sim, dis, lena // 2))


import collections

def CountFrequency(arr):
	return collections.Counter(arr)
t = int(input())
for T in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	freq = CountFrequency(arr)
	k = []
	fre = []
	for (key, value) in freq.items():
		k.append(key)
		fre.append(value)
	if len(k) == max(fre):
		print(max(fre) - 1)
	else:
		print(min(len(k), max(fre)))


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(input())
	students = list(map(int, stdin.readline().split()))
	studentCountDict = dict([])
	for skill in students:
		if skill in studentCountDict:
			studentCountDict[skill] += 1
		else:
			studentCountDict[skill] = 1
	numberOfDifferentSkills = len(studentCountDict)
	skillWithMaxCount = max(studentCountDict.values())
	print(max(min(skillWithMaxCount, numberOfDifferentSkills - 1), min(numberOfDifferentSkills, skillWithMaxCount - 1)))


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	if n == 1:
		print('0')
	else:
		for i in range(n):
			if arr[i] not in d:
				d[arr[i]] = 1
			else:
				d[arr[i]] += 1
		dist = len(d)
		max_count = max(d.values())
		if max_count > dist:
			print(dist)
		elif max_count == dist:
			print(dist - 1)
		else:
			print(max_count)


from sys import stdin
from statistics import mode
inp = stdin.readline

def most_frequent(arr):
	Hash = dict()
	for i in range(len(array)):
		if arr[i] in Hash.keys():
			Hash[arr[i]] += 1
		else:
			Hash[arr[i]] = 1
	max_count = 0
	res = -1
	for i in Hash:
		if max_count < Hash[i]:
			res = i
			max_count = Hash[i]
	return max_count
t = int(inp().strip())
for _ in range(t):
	n = int(inp().strip())
	array = [int(x) for x in inp().strip().split()]
	different = set(array)
	mostFrequent = most_frequent(array)
	if mostFrequent == len(different):
		mostFrequent -= 1
	print(min(mostFrequent, n // 2, len(different)))


for _ in range(int(input())):
	n = int(input())
	skills = list(map(int, input().split()))
	freq = {}
	for item in skills:
		if item not in freq:
			freq[item] = 1
		else:
			freq[item] += 1
	a = max(freq.values())
	b = len(freq) - 1
	if len(skills) == 1:
		print(0)
	elif a >= b:
		if a == b or a == b + 1:
			print(b)
		else:
			print(b + 1)
	else:
		print(a)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	e = Counter(a)
	d = max(e.values())
	f = len(e)
	if d == f:
		print(d - 1)
		continue
	print(min(d, f))


from collections import Counter
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	c = dict(Counter(a))
	p = len(c)
	mx = 0
	ans = 0
	for (n, m) in c.items():
		if m > mx:
			mx = m
	p -= 1
	if mx - p > 1:
		ans = p + 1
	else:
		ans = min(p, mx)
	print(ans)


from collections import Counter
for _ in range(0, int(input())):
	n = int(input())
	v = list(map(int, input().split()))
	d = len(set(v))
	s = max(Counter(v).values())
	if s == d:
		print(s - 1)
	else:
		print(min(s, d))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sat = set(arr)
	d = defaultdict(int)
	for i in arr:
		d[i] += 1
	maxrep = 0
	for i in sat:
		maxrep = max(maxrep, d[i])
	lenset = len(sat)
	if lenset == maxrep:
		print(maxrep - 1)
	else:
		print(min(maxrep, lenset))


number_test = int(input())
for k in range(number_test):
	b = input()
	skills = list(map(int, input().split(' ')))
	occurrence = {}
	for skill in skills:
		if skill in occurrence.keys():
			occurrence[skill] += 1
		else:
			occurrence[skill] = 1
	n2 = 0
	lettre = 0
	n1 = len(occurrence)
	for (cle, valeur) in occurrence.items():
		if valeur > n2:
			n2 = valeur
			lettre = cle
	if n2 > n1:
		print(str(n1))
	elif n1 == n2:
		print(str(n1 - 1))
	else:
		print(str(n2))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = sorted(map(int, input().split()))
	c = []
	s = len(set(a))
	c = sorted(Counter(a).values(), reverse=True)
	m = c[0]
	if m > s:
		print(s)
	elif m == s:
		print(s - 1)
	elif s == 1:
		print(0)
	else:
		print(min(m, s))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	b = [1]
	for i in range(1, n):
		if a[i] == a[i - 1]:
			b[-1] += 1
		else:
			b.append(1)
	b.sort()
	if len(b) - 1 == b[-1]:
		print(len(b) - 1)
	elif len(b) - 1 < b[-1]:
		print(min(len(b), b[-1] - 1))
	else:
		print(b[-1])


import collections
test = int(input())
while test:
	test -= 1
	n = int(input())
	ls = list(map(int, input().split()))
	xU = len(set(ls))
	dic = collections.Counter(ls)
	s = dic.values()
	s = max(s)
	if xU > s:
		print(s)
	elif xU < s:
		print(xU)
	else:
		print(xU - 1)


import sys, collections, operator
for i in range(int(sys.stdin.readline())):
	num = int(sys.stdin.readline())
	dick = dick = collections.defaultdict(int)
	members = tuple(sys.stdin.readline().split())
	if num == 3 or num == 2:
		sys.stdout.write('{}\n'.format(1))
		continue
	if num == 1:
		sys.stdout.write('{}\n'.format(0))
		continue
	for mem in members:
		dick[mem] += 1
	same = max(dick.items(), key=operator.itemgetter(1))[1]
	l = len(dick)
	if same > l:
		sys.stdout.write('{}\n'.format(l))
	elif same == l:
		sys.stdout.write('{}\n'.format(l - 1))
	else:
		sys.stdout.write('{}\n'.format(same))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = {}
	cnt1 = set()
	for elem in a:
		cnt1.add(elem)
		cnt[elem] = cnt.get(elem, 0) + 1
	ans1 = max(cnt.items(), key=lambda x: x[1])[1]
	ans2 = len(cnt1) - 1
	if ans1 - 1 > ans2:
		ans = ans2 + 1
	else:
		ans = min(ans1, ans2)
	print(ans)


import collections
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	v = collections.Counter(a)
	(t, len_) = (0, 0)
	for i in v:
		len_ += 1
		if v[i] > t:
			t = v[i]
			val = i
	print(max(min(len_ - 1, t), min(len_, t - 1)))


tests = int(input())
while tests > 0:
	tests -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	dist = 0
	ds = []
	fs = []
	idx = 0
	while idx < n:
		oidx = idx
		dist += 1
		while idx < n and arr[oidx] == arr[idx]:
			idx += 1
		cnt = idx - oidx
		ds.append(arr[oidx])
		fs.append(cnt)
	res = 0
	for i in range(0, dist):
		for fr in range(1, fs[i] + 1):
			dLeft = dist
			if fr == fs[i]:
				dLeft -= 1
			ret = min(dLeft, fr)
			res = max(res, ret)
	print(res)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = Counter(map(int, input().split()))
	x2 = len(set(a))
	x1 = max(a.values())
	if x1 == x2:
		print(x1 - 1)
	else:
		print(min(x1, x2))


for _ in range(int(input())):
	n = int(input())
	team = 0
	ls = list(map(int, input().split()))
	l = len(set(ls))
	di = {}
	for i in range(n):
		if ls[i] in di:
			di[ls[i]] += 1
		else:
			di[ls[i]] = 1
	for x in di:
		for i in range(di[x]):
			if di[x] - i <= n // 2:
				if i > 0:
					if l >= di[x] - i:
						team = max(team, di[x] - i)
						break
				elif l - 1 >= di[x] - i:
					team = max(team, di[x] - i)
					break
	print(team)


from collections import Counter
test_cases = int(input())

def handle():
	input()
	values = input().split(' ')
	if len(values) == 1:
		return 0
	counts = Counter(values)
	if len(counts) == 1:
		return 1
	sorted_values = sorted(counts.values())
	if sorted_values[-1] == len(sorted_values) - 1:
		return len(sorted_values) - 1
	if sorted_values[-1] - 1 >= len(sorted_values):
		return len(sorted_values)
	if len(sorted_values) - 1 >= sorted_values[-1]:
		return sorted_values[-1]
	return max(1, sorted_values[-1] - 1)
for _ in range(test_cases):
	print(handle())


t = int(input())
for _ in range(t):
	n = int(input())
	ar = [int(x) for x in input().split()]
	ar = sorted(ar)
	counts = []
	last = -1
	c = 1
	for val in ar:
		if last == val:
			c += 1
		else:
			counts.append(c)
			c = 1
		last = val
	counts.append(c)
	counts = counts[1:]
	allless1 = min(max(counts) - 1, len(counts))
	allless0 = min(max(counts), len(counts) - 1)
	print(max(allless1, allless0))


n = int(input())
for i in range(n):
	t = int(input())
	m = 0
	q = [0] * (t + 1)
	a = list(map(int, input().split()))
	for i in range(t):
		q[a[i]] += 1
		if q[a[i]] > m:
			m = q[a[i]]
	w = len(set(a))
	if abs(w - m) > 0:
		print(min(m, w))
	else:
		print(m - 1)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	lis = [int(s) for s in input().split()]
	d = dict(Counter(lis))
	co = max(d, key=d.get)
	max_length = d[co]
	total_unique = len(d)
	if total_unique < max_length:
		print(total_unique)
	elif total_unique == max_length:
		print(total_unique - 1)
	else:
		print(max_length)


import sys

def single_solve(n, style):
	count = dict()
	for s in style:
		count[s] = count.get(s, 0) + 1
	max_count = max(count.values())
	diff_values = len(count)
	return f'{max(min(diff_values - 1, max_count), min(diff_values, max_count - 1))}'

def solve(input_path=None):
	if input_path is None:
		f = sys.stdin
	else:
		f = open(input_path, 'r')
	t = int(f.readline())
	golds = []
	for _ in range(t):
		n = int(f.readline())
		style = list(map(int, f.readline().split()))
		golds.append(single_solve(n, style))
	return golds

def main():
	for line in solve():
		print(f'{line}')
main()


t = int(input())
while t:
	n = int(input())
	a = [int(i) for i in input().split()]
	mem = {}
	for ele in a:
		if ele in mem:
			mem[ele] += 1
		else:
			mem[ele] = 1
	distinct = len(mem)
	same = max(mem.values())
	if same == distinct:
		print(same - 1)
	else:
		while same > distinct:
			same -= 1
		while distinct > same:
			distinct -= 1
		print(same)
	t -= 1


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	members = list(map(int, input().split()))
	bucket = [0 for _ in range(n + 1)]
	for i in range(n):
		bucket[members[i]] += 1
	bucket.sort()
	sums = 0
	s = set()
	for i in range(n):
		if bucket[i] != 0:
			s.add(i)
	if bucket[n] - len(s) >= 2:
		s.add(n)
		bucket[n] -= 1
	print(min(len(s), bucket[n]))


t = int(input())
for i in range(t):
	n = int(input())
	an = [int(s) for s in input().split(' ')]
	nbs = {}
	for j in an:
		if j in nbs:
			nbs[j] += 1
		else:
			nbs[j] = 1
	keys = nbs.keys()
	maxi = 0
	for j in keys:
		maxi = max(nbs[j], maxi)
	if maxi == len(keys):
		print(maxi - 1)
	else:
		print(min(maxi, len(keys)))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(int)
	for i in a:
		d[i] += 1
	m = 0
	g = len(d.keys())
	for i in d:
		if d[i] > m:
			m = d[i]
	if g == 1:
		if n == 1:
			print(0)
		else:
			print(1)
	elif m >= g - 1 and g - 1 != 0:
		if m - 1 >= g:
			print(g)
		else:
			print(g - 1)
	else:
		print(m)


import statistics
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	mid1 = len(list(s))
	ele = 0
	max1 = 0
	avg = n // 2
	count2 = 0
	d = statistics.mode(l)
	max1 = l.count(d)
	if max1 > mid1:
		if mid1 > avg:
			print(avg)
		else:
			print(mid1)
	elif max1 - mid1 == 0:
		print(mid1 - 1)
	elif max1 > avg:
		print(avg)
	else:
		print(max1)


from sys import stdin, gettrace
from collections import Counter
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		n = int(input())
		aa = [int(a) for a in input().split()]
		caa = Counter(aa)
		scount = len(caa)
		mc = caa.most_common(1)
		mcc = mc[0][1]
		if mcc < scount:
			print(mcc)
		elif mcc == scount:
			print(mcc - 1)
		else:
			print(scount)
	q = int(input())
	for _ in range(q):
		solve()
main()


for i in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	k = []
	l.sort()
	sch = l[0]
	k.append(1)
	u = 0
	for i in range(1, n):
		if l[i] == sch:
			k[u] += 1
		else:
			sch = l[i]
			u += 1
			k.append(1)
	m = max(k)
	kl = max(min(m, len(k) - 1), min(m - 1, len(k)))
	print(kl)


import sys

def MI():
	return map(int, sys.stdin.readline().strip().split())

def LI():
	return list(map(int, sys.stdin.readline().strip().split()))

def solve(a):
	if len(a) == 1:
		print('0')
		return
	keys = set(a)
	if len(keys) == 1:
		print('1')
		return
	m = {}
	for i in a:
		if i in m:
			m[i] += 1
		else:
			m[i] = 1
	pairs = []
	for i in m.keys():
		if m[i] > 1:
			pairs.append((m[i], i))
	pairs.sort(reverse=True)
	answer = len(a) // 2
	if len(pairs) == 0:
		print('1')
		return
	while answer > 0:
		most_frequent_left = pairs[0][0] - answer
		if most_frequent_left < 0:
			answer -= 1
			continue
		different_numbers_left = len(keys) - 1
		if most_frequent_left > 0:
			different_numbers_left += 1
		if different_numbers_left >= answer:
			break
		answer -= 1
	print(answer)

def main():
	t = int(input())
	for i in range(t):
		int(input())
		a = LI()
		solve(a)
main()


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	ctr = Counter(input().split())
	a = ctr.most_common(1)[0][1]
	b = len(ctr.most_common())
	print(max(min(a, b - 1), min(a - 1, b)))


import sys
import math
from collections import defaultdict

def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		return
	rec = defaultdict(int)
	for i in range(n):
		rec[a[i]] += 1
	dist = len(rec) - 1
	dups = 1
	for k in rec:
		dups = max(dups, rec[k])
	x = None
	if dups <= dist + 1:
		x = min(dups, dist)
	else:
		x = dist + 1
	print(x)
test_cases = int(input())
for _ in range(test_cases):
	solve()


for _ in range(int(input())):
	n = int(input())
	seq = sorted([int(x) for x in input().split()])
	s = set(seq)
	team1 = len(s)
	cur_val = seq[0]
	cnt = 1
	team2 = 0
	for val in seq[1:]:
		if val == cur_val:
			cnt += 1
		else:
			team2 = max(team2, cnt)
			cnt = 1
			cur_val = val
	team2 = max(team2, cnt)
	if n == 1:
		ans = 0
	else:
		ans = min(team1, team2)
		if team1 == team2:
			ans -= 1
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for j in range(t):
	n = int(input())
	nums = [int(i) for i in input().strip().split()]
	counts = {}
	for i in nums:
		if i in counts:
			counts[i] += 1
		else:
			counts[i] = 1
	maxcount = -1
	for i in counts:
		maxcount = max(counts[i], maxcount)
	diff = len(counts) - 1
	if maxcount == 1:
		if n > 1:
			print(1)
		else:
			print(0)
	elif diff >= maxcount:
		print(maxcount)
	elif maxcount - diff == 1:
		print(diff)
	else:
		print(diff + 1)


from collections import Counter
t = int(input())
b = 0
for p in range(t):
	n = int(input())
	my = list(map(int, input().strip().split()))[:n]
	a = Counter(my).values()
	c = len(Counter(my).keys())
	m = max(a)
	r = max(min(c - 1, m), min(c, m - 1))
	print(r)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	for el in a:
		if el not in d:
			d[el] = 1
		else:
			d[el] += 1
	mx = max(d.values())
	cnt = len(d.keys())
	if cnt - 1 >= mx:
		print(mx)
	elif cnt == mx or cnt == mx - 1:
		print(mx - 1)
	else:
		print(cnt)


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(t) for t in input().split()]
	l.sort()
	distincts = 1
	max_sames = 1
	sames = 1
	for (i, num) in enumerate(l):
		if i == 0:
			continue
		if num == l[i - 1]:
			sames += 1
		else:
			max_sames = max(max_sames, sames)
			sames = 1
			distincts += 1
	max_sames = max(max_sames, sames)
	if distincts == max_sames:
		max_sames -= 1
	result = min(distincts, max_sames)
	print(result)


def answer(students):
	_max = {'studentKey': 0, 'duplicatedSkill': 0}
	dic = {}
	for i in range(len(students)):
		key = '_' + str(students[i])
		if dic.get(key) is not None:
			dic[key] += 1
		else:
			dic[key] = 1
		if dic[key] > _max['duplicatedSkill']:
			_max['duplicatedSkill'] = dic[key]
			_max['studentKey'] = key
	leftTeamLength = 0
	for (k, v) in dic.items():
		if k != _max['studentKey']:
			leftTeamLength += 1
	if _max['duplicatedSkill'] - leftTeamLength > 1:
		leftTeamLength += 1
	if leftTeamLength > _max['duplicatedSkill']:
		return _max['duplicatedSkill']
	return leftTeamLength
testCount = int(input())
for _ in range(testCount):
	n = int(input())
	students = list(map(int, input().split()))
	print(answer(students))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	maxd = 0
	for x in a:
		d[x] = d.get(x, 0) + 1
		if d[x] > maxd:
			maxd = d[x]
	print(max(min(len(d) - 1, maxd), min(len(d), maxd - 1)))


from math import floor, ceil
t = int(input())
ans = []
for _ in range(t):
	_ = input()
	perks = map(int, input().split())
	dif = set()
	sim = {}
	for i in perks:
		if not i in dif:
			dif.add(i)
			sim[i] = 1
		else:
			sim[i] += 1
	m = 0
	dif = len(dif)
	for i in sim.keys():
		if sim[i] > m:
			m = sim[i]
	a = min(dif, m)
	if m == dif:
		ans.append(a - 1)
	else:
		ans.append(a)
print(*ans, sep='\n')


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	count = Counter(a)
	l = len(count)
	maxx = max(count.values())
	l -= 1
	if l >= maxx:
		print(maxx)
	elif l < maxx:
		if maxx == l + 1:
			print(l)
		else:
			print(l + 1)


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	x = 0
	y = 0
	z = 0
	for j in range(0, n - 1):
		if a[j] == a[j + 1]:
			x += 1
			z += 1
		elif y <= x:
			y = x
			x = 0
		else:
			x = 0
	if y <= x:
		y = x
	zz = n - z
	if y == 0:
		if n < 2:
			print('0')
		else:
			print('1')
	elif zz > y + 1:
		print(y + 1)
	else:
		print(min(y, zz))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	count = {}
	maxx = 0
	ls = 0
	for i in l:
		if i in count:
			count[i] += 1
		else:
			count[i] = 1
			ls += 1
		if count[i] > maxx:
			maxx = count[i]
	if ls > maxx:
		print(maxx)
	elif ls == maxx:
		print(maxx - 1)
	else:
		print(ls)


import math
t = int(input())
for _ in range(t):
	dict1 = {}
	dict2 = {}
	n = int(input())
	for x in input().split():
		if x not in dict1.keys():
			dict1[x] = 1
			dict2[x] = 0
		else:
			dict2[x] += 1
	dict21 = dict(sorted(dict2.items(), key=lambda x: x[1], reverse=True))
	len1 = len(dict1)
	v = list(dict21.values())
	len2 = max(v)
	ans = 0
	if len1 == len2 + 1:
		ans = len2
	elif len1 >= len2 + 2:
		ans = len2 + 1
	elif len1 <= len2:
		ans = len1
	print(ans)


from collections import Counter
for _ in range(int(input())):
	vn = int(input())
	l = list(map(int, input().split()))
	d = Counter(l)
	mx = -1
	for t in d:
		if d[t] > mx:
			mx = d[t]
	n = len(d) - 1
	if mx > n:
		if mx - n == 1:
			print(n)
		else:
			print(n + 1)
	else:
		print(mx)


import math as mt

def mostFrequent(arr):
	n = len(arr)
	Hash = dict()
	for i in range(n):
		if arr[i] in Hash.keys():
			Hash[arr[i]] += 1
		else:
			Hash[arr[i]] = 1
	max_count = 0
	res = -1
	for i in Hash:
		if max_count < Hash[i]:
			res = i
			max_count = Hash[i]
	return max_count
from collections import Counter

def countDistinct(arr):
	return len(Counter(arr).keys())
t = int(input())
for im in range(t):
	n = int(input())
	skill = list(map(int, input().split()))
	u = countDistinct(skill)
	m = mostFrequent(skill)
	if u == m:
		print(m - 1)
	elif u > m:
		print(m)
	else:
		print(min(u, m))


from collections import Counter
for t in range(int(input())):
	input()
	s = input().split()
	c = Counter(s)
	max_c = max(c.values())
	ans = max(min(max_c, len(c) - 1), min(max_c - 1, len(c)))
	print(ans)


t = int(input())
for i in range(t):
	(n, s, a, m) = (int(input()), input().split(), dict(), 0)
	for i in range(n):
		a[s[i]] = a.get(s[i], 0) + 1
	for i in a:
		m = max(a[i], m)
	print(max(min(len(a) - 1, m), min(len(a), m - 1)))


for _ in range(int(input())):
	n = int(input())
	ais = [0] * n
	for i in list(map(int, input().split())):
		ais[i - 1] += 1
	ais.sort()
	count = 0
	for a in ais:
		count += 1 if a > 0 else 0
	if n == 1:
		print(0)
	elif count == n:
		print(1)
	elif count == ais[-1]:
		print(ais[-1] - 1)
	else:
		print(min(count, ais[-1]))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = map(int, input().split())
	c = Counter(a)
	b_max = c.most_common(1)[0][1]
	a_max = len(c.keys())
	print(max(min(a_max - 1, b_max), min(a_max, b_max - 1)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	max_same = 1
	tmp = 1
	cnt = 0
	for i in range(1, n):
		if a[i] == a[i - 1]:
			cnt += 1
			tmp += 1
		else:
			max_same = max(max_same, tmp)
			tmp = 1
	max_same = max(max_same, tmp)
	for k in range(min(n // 2, max_same), -1, -1):
		if k == max_same:
			if n - 2 * k >= cnt - (k - 1):
				print(k)
				break
		elif n - 2 * k >= cnt - k:
			print(k)
			break


t = int(input())
while t != 0:
	a = int(input())
	b = list(map(int, input().split(' ')))
	c = set(b)
	c = list(c)
	e = []
	count = 0
	w = 0
	if a == 1:
		print(0)
	else:
		b.sort()
		for i in range(0, a - 1):
			if b[i] == b[i + 1]:
				count = count + 1
				w = 1
				if i == a - 2:
					e.append(count + 1)
			elif w == 1:
				count = count + 1
				e.append(count)
				count = 0
				w = 0
		if len(e) == 0:
			print(1)
		else:
			f = max(e)
			g = len(c)
			if f == g:
				print(f - 1)
			elif f <= g - 1:
				print(f)
			else:
				print(g)
	t = t - 1


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for v in a:
		if v in d:
			d[v] += 1
		else:
			d[v] = 1
	l = len(d)
	mx = d[max(d, key=d.get)]
	if l == mx:
		print(l - 1)
	if l > mx:
		print(mx)
	if l < mx:
		print(l)


t = int(input())
from collections import defaultdict
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	m = defaultdict(int)
	for i in a:
		m[i] += 1
	max_key = max(m, key=lambda x: m.get(x))
	x1 = [max_key for i in range(m[max_key])]
	x2 = set([i for i in a if i != max_key])
	l1 = len(x1)
	l2 = len(x2)
	if l2 >= l1:
		print(l1)
		continue
	elif l1 > 1:
		print(min(l1 - 1, l2 + 1))
	else:
		print(min(l1, l2))


t = int(input())
while t:
	n = int(input())
	skills = list(map(int, input().split()))
	unique = set(skills)
	freq = {}
	for el in skills:
		freq[el] = freq.get(el, 0) + 1
	maxfreq = sorted(freq, key=freq.get, reverse=True)
	max_freq_el = maxfreq[0]
	if freq[max_freq_el] == len(unique):
		print(len(unique) - 1)
	else:
		print(min(freq[max_freq_el], len(unique)))
	t = t - 1


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	j = set(l)
	j = len(j)
	dict1 = {}
	for i in range(n):
		dict1[l[i]] = 0
	for i in range(n):
		dict1[l[i]] += 1
	max1 = 0
	for i in range(n):
		max1 = max(max1, dict1[l[i]])
	if j - 1 == max1:
		print(j - 1)
	elif j - 1 > max1:
		print(max1)
	elif j < max1:
		print(j)
	else:
		print(j - 1)


T = int(input())
for t in range(T):
	n = int(input())
	a = [int(i) for i in input().split()]
	skills = {}
	for ai in a:
		if ai not in skills:
			skills[ai] = 1
		else:
			skills[ai] += 1
	differentSkills = len(set(a)) - 1
	last = -1
	lastS = -1
	for j in skills:
		if skills[j] > last:
			last = skills[j]
			lastS = j
	fOpt = min(differentSkills, last)
	sOpt = min(differentSkills + 1, last - 1)
	print(max(fOpt, sOpt))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sam = 0
	diff = set()
	a.sort()
	mx = -1
	for i in range(n):
		if i == 0:
			sam = 1
		elif a[i] == a[i - 1]:
			sam += 1
			mx = max(sam, mx)
		else:
			sam = 1
		diff.add(a[i])
	if n == 1:
		print('0')
	elif mx == -1:
		print('1')
	elif mx == n:
		print('1')
	elif abs(len(diff) - mx) == 0:
		print(mx - 1)
	else:
		print(min(mx, len(diff)))


T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	mp = {}
	li = []
	for a in A:
		if a not in mp:
			mp[a] = 1
			li.append(a)
		else:
			mp[a] += 1
	mx = 0
	for k in mp:
		if mp[k] > mx:
			mx = mp[k]
	if mx > len(li):
		print(len(li))
	elif mx < len(li):
		print(mx)
	else:
		print(mx - 1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	dct = {}
	for i in l:
		dct[i] = 0
	for i in l:
		dct[i] += 1
	d = n // 2
	maxi = 0
	for value in dct.values():
		if maxi < value:
			maxi = value
	if d == 0:
		print(0)
	elif maxi > len(dct):
		print(len(dct))
	elif maxi < len(dct):
		print(maxi)
	else:
		print(len(dct) - 1)


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = set(arr)
	dic = {}
	for i in range(n):
		if arr[i] in dic:
			dic[arr[i]] += 1
		else:
			dic[arr[i]] = 1
	if n == 1:
		print(0)
	else:
		s_m = 0
		for i in dic:
			s_m = max(s_m, dic[i])
		s_s = len(s)
		if s_m < s_s:
			print(s_m)
		elif s_m == s_s:
			print(s_m - 1)
		else:
			print(s_s)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = Counter(arr)
	dist = len(d)
	maxans = 0
	for value in d.values():
		curr = value
		rem = dist - 1
		if curr <= rem + 1:
			ans = min(curr, rem)
		else:
			ans = min(curr, rem) + 1
		maxans = max(maxans, ans)
	print(maxans)


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split()
	if n < 2:
		print(0)
	elif n == 2:
		print(1)
	else:
		h = int(n / 2)
		d = {}
		for o in s:
			if o not in d.keys():
				d[o] = 1
			else:
				d[o] += 1
		print(min(len(d), max(d.values())) - (len(d) == max(d.values())))


for j in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	if n == 1:
		print(0)
	else:
		for i in l:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		m1 = max(d.values())
		m2 = len(d) - 1
		if m1 > m2 + 1:
			print(m2 + 1)
		else:
			print(min(m1, m2))


t = int(input())
res = ''
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	dif = len(set(a))
	count = 1
	max_count = 0
	for i in range(1, n):
		if a[i] == a[i - 1]:
			count += 1
		else:
			count = 1
		if count > max_count:
			max_count = count
	if dif == max_count:
		x = dif - 1
	else:
		x = min(dif, max_count)
	res += str(x) + '\n'
print(res)


t = int(input())
for i in range(t):
	n = int(input())
	A = [int(s) for s in input().split()]
	A.sort()
	skills = [A[0]]
	counts = []
	old = skills[0]
	qty = 1
	for j in range(1, n):
		new = A[j]
		if new == old:
			qty += 1
		else:
			counts.append(qty)
			skills.append(new)
			old = new
			qty = 1
	counts.append(qty)
	if max(counts) == len(skills):
		print(max(counts) - 1)
	else:
		print(min(max(counts), len(skills)))


from sys import stdin, stdout
q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, stdin.readline().split()))
	if n == 1:
		print(0)
	else:
		arr = [0] * n
		for i in l:
			arr[i - 1] = arr[i - 1] + 1
		cnt = 0
		for i in arr:
			if i > 0:
				cnt = cnt + 1
		maxi = 1
		for i in arr:
			if i > 1:
				maxi = max(maxi, max(min(cnt - 1, i), min(cnt, i - 1)))
		print(maxi)


t = int(input())
for i in range(0, t):
	n = int(input())
	students = input()
	students = list(map(int, students.split()))
	studentHash = {}
	biggest = -1
	for j in range(0, n):
		skill = students[j]
		if skill in studentHash:
			studentHash[skill] = studentHash[skill] + 1
		else:
			studentHash[skill] = 1
		if studentHash[skill] > biggest:
			biggest = studentHash[skill]
	if biggest == 1:
		if len(studentHash) == 1:
			print(0)
		else:
			print(1)
	else:
		otherLength = len(studentHash) - 1
		if biggest > otherLength:
			if biggest - otherLength > 1:
				print(otherLength + 1)
			else:
				print(otherLength)
		else:
			print(biggest)


t = int(input())
for _ in range(t):
	n = int(input())
	counts = {}
	cnt = 0
	for i in map(int, input().split()):
		if i not in counts:
			counts[i] = 0
			cnt += 1
		counts[i] += 1
	res = 0
	for i in counts.items():
		res = max(res, min(i[1], cnt - 1), min(i[1] - 1, cnt))
	print(res)


for i in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	b = {}
	for i in range(n):
		b[li[i]] = 0
	for i in range(n):
		b[li[i]] += 1
	diff = []
	same = []
	for i in b:
		diff.append(i)
		same.append(b[i])
	max_same = max(same)
	print(max(min(len(diff) - 1, max_same), min(len(diff), max_same - 1)))


import collections
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	counter = collections.Counter(a)
	hval = max(counter.items(), key=lambda x: x[1])[1]
	otherside = len(counter.items()) - 1
	highest = max(min(hval, otherside), min(hval - 1, otherside + 1))
	print(highest)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	aa = Counter(a)
	l1 = len(aa)
	l2 = max(aa.values())
	if l1 == l2:
		print(l1 - 1)
	else:
		print(min(l1, l2))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	d = defaultdict(int)
	ar = list(map(int, input().split()))
	m = 0
	for i in ar:
		d[i] += 1
		m = max(m, d[i])
	l = len(d)
	if m > l - 1 and m - l < 1:
		print(l - 1)
	elif m > l - 1 and m - l >= 1:
		print(l)
	else:
		print(m)


from collections import *
test = int(input())
for _ in range(test):
	n = int(input())
	arr = Counter(input().split()).values()
	(x, t) = (len(arr), max(arr))
	print(min(x, t) - (x == t))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = Counter(a)
	if n < 2:
		print(0)
	elif len(set(a)) == n:
		print(1)
	else:
		h = d.most_common(1)[0]
		x = len(set(a)) - 1
		ans = max(min(h[1], x), min(h[1] - 1, x + 1))
		print(ans)


x = int(input())
for i in range(x):
	n = int(input())
	if n == 1 or n == 2:
		inp = list(map(int, input().split()))
		print(n - 1)
	else:
		dict = {}
		inp = list(map(int, input().split()))
		for item in inp:
			if item in dict:
				dict[item] += 1
			else:
				dict[item] = 1
		vallist = list(dict.values())
		maxindex = 0
		for i in range(len(vallist)):
			if vallist[i] > vallist[maxindex]:
				maxindex = i
		ans = n // 2 + 1
		same = False
		while same == False:
			ans -= 1
			if vallist[maxindex] >= ans:
				vallist[maxindex] -= ans
				same = True
				break
		unique = False
		ans += 1
		while not unique:
			count = 0
			ans -= 1
			for k in range(len(vallist)):
				if vallist[k] > 0:
					count = count + 1
				if count == ans:
					unique = True
					break
			vallist[maxindex] += 1
		print(ans)


def comander(n, lst):
	cnt = [0] * (n + 1)
	for elem in lst:
		cnt[elem] += 1
	m = max(cnt)
	d = n + 1 - cnt.count(0)
	return max(min(m - 1, d), min(m, d - 1))
for _ in range(int(input())):
	m = int(input())
	a = [int(i) for i in input().split()]
	print(comander(m, a))


t = int(input())
for _ in range(t):
	n = int(input())
	student = list(map(int, input().split()))
	student_num = dict()
	for x in student:
		student_num[x] = student_num.get(x, 0) + 1
	a = max(student_num.values())
	b = len(student_num)
	if b > a:
		print(a)
	elif b == a:
		print(b - 1)
	else:
		print(b)


from collections import defaultdict
a = int(input())
for _ in range(a):
	n = int(input())
	skills = defaultdict(int)
	sk = list(map(int, input().split()))
	for i in range(n):
		skills[sk[i]] += 1
	maxf = max(skills.values())
	diff = len(skills)
	print(max(min(diff - 1, maxf), min(diff, maxf - 1)))


t = int(input())
for k in range(t):
	n = int(input())
	donnees = input().split(' ')
	skills = [0] * n
	for j in range(n):
		skills[j] = int(donnees[j])
	freqskills = [0] * n
	for j in range(n):
		freqskills[skills[j] - 1] += 1
	nbdiff = 0
	max = 0
	for j in range(n):
		if freqskills[j] > 0:
			nbdiff += 1
		if freqskills[j] > max:
			max = freqskills[j]
	if max > nbdiff:
		print(nbdiff)
	if max < nbdiff:
		print(max)
	if max == nbdiff:
		print(max - 1)


from collections import defaultdict

def main():
	t = int(input())
	for _ in range(1, t + 1):
		n = int(input())
		a = [int(i) for i in input().split(' ')]
		skills = defaultdict(lambda : 0)
		for i in range(n):
			skills[a[i]] += 1
		distinct = len(skills)
		similar = max(skills.values())
		if similar == distinct:
			res = similar - 1
		else:
			res = min(similar, distinct)
		print(res)
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	dis = 0
	com = 0
	for i in arr:
		dic[i] = 0
	for i in arr:
		if dic[i] == 0:
			dis += 1
		dic[i] += 1
		if dic[i] > com:
			com = dic[i]
	if n == 1:
		print(0)
		continue
	if com == n or dis == n:
		print(1)
		continue
	print(max(min(com, dis - 1), min(com - 1, dis)))


t = int(input())
for _ in range(t):
	n = int(input())
	str = list(map(int, input().split()))
	str = sorted(str)
	d = 1
	same = 0
	final_same = 0
	for i in range(len(str) - 1):
		if str[i] != str[i + 1]:
			d += 1
			final_same = max(final_same, same)
			same = 0
		else:
			same += 1
	final_same = max(final_same, same)
	if final_same > 0:
		final_same += 1
		d -= 1
	if final_same - 1 > d:
		print(d + 1)
	elif d > 1 and final_same == 0:
		print(1)
	else:
		print(min(final_same, d))


from collections import Counter
t = int(input())
while t != 0:
	n = int(input())
	A = list(map(int, input().split()))
	C = Counter(A)
	if max(C.values()) > len(C.keys()):
		print(len(C.keys()))
	elif max(C.values()) == len(C.keys()):
		print(len(C.keys()) - 1)
	else:
		print(max(C.values()))
	t -= 1


from collections import Counter
cases = int(input().strip())
for case in range(cases):
	n = int(input().strip())
	students = [int(x) for x in input().strip().split(' ')]
	cnt = Counter(students)
	distinct = len(cnt)
	same_range = max(cnt.values())
	if len(students) < 2:
		print(0)
	elif distinct == same_range:
		print(distinct - 1)
	else:
		print(min(distinct, same_range))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = Counter(a)
	x = b.most_common()[0]
	if len(b) != x[1]:
		ans = min(len(b), x[1])
	else:
		ans = len(b) - 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	A.sort()
	if n == 1:
		print(0)
		continue
	S = [0 for i in range(A[n - 1] + 1)]
	for i in range(n):
		S[A[i]] += 1
	P = []
	for i in range(n):
		if S[A[i]] > 1:
			P.append(S[A[i]])
	P.sort()
	if len(P) == 0 or len(set(A)) == len(A):
		print(1)
		continue
	d = max(P)
	li = len(set(A)) - 1
	diff = 0
	if li >= d:
		print(d)
	else:
		diff = d - li
		if diff == 1:
			print(li)
		else:
			print(li + 1)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(x) for x in input().split()]
		count = Counter(a)
		l = len(count)
		maxx = max(count.values())
		l -= 1
		if l >= maxx:
			print(maxx)
		elif l < maxx:
			if maxx == l + 1:
				print(l)
			else:
				print(l + 1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin
import sys
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	dictt = {}
	fmax = 0
	typ = 0
	for i in arr:
		if i not in dictt:
			typ += 1
			dictt[i] = 1
		else:
			dictt[i] += 1
		if dictt[i] > fmax:
			fmax = dictt[i]
	print(max(min(typ, fmax - 1), min(typ - 1, fmax)))


import math
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [0 for i in range(n + 1)]
	b = list(map(int, input().split()))
	for i in b:
		a[i] += 1
	q = max(a)
	k = len(set(b)) - 1
	if k >= q:
		print(q)
	elif k + 1 == q:
		print(k)
	else:
		print(k + 1)


import heapq as hp
import collections
import bisect

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def s_unpack(func=int):
	return sorted(map(func, input().split()))

def range_n():
	return range(int(input()))

def counter(a):
	d = {}
	for x in a:
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	return d

def getint():
	return int(input())

def main():
	for _ in range_n():
		_ = getint()
		a = l_unpack()
		d = counter(a)
		b = sorted(d, key=lambda x: d[x])
		n = len(b)
		ans = 0
		if d[b[-1]] < n:
			ans = d[b[-1]]
		elif d[b[-1]] > n:
			ans = n
		else:
			ans = n - 1
		print(ans)
main()


t = int(input())
while t > 0:
	n = int(input())
	lister = list(map(int, input().split()))
	freq_dict = {}
	for i in range(len(lister)):
		if lister[i] in freq_dict:
			freq_dict[lister[i]] += 1
		else:
			freq_dict[lister[i]] = 1
	maximum = max(freq_dict.values())
	unique = len(freq_dict.keys())
	if unique < maximum:
		print(unique)
	elif unique == maximum:
		print(unique - 1)
	else:
		print(maximum)
	t -= 1


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	line = sys.stdin.readline().split()
	skills = list(map(int, line))
	freq = [0] * n
	max_freq = 0
	skill_fq = 0
	for i in range(n):
		freq[skills[i] - 1] += 1
		if freq[skills[i] - 1] > max_freq:
			max_freq = freq[skills[i] - 1]
			skill_fq = skills[i]
	sum_freqs = 0
	for i in range(n):
		if i != skill_fq - 1:
			sum_freqs += min(1, freq[i])
	if max_freq > sum_freqs + 1:
		print(sum_freqs + 1)
	else:
		print(min(sum_freqs, max_freq))


from sys import stdin, stdout

def read():
	x0 = stdin.readline().rstrip()
	return x0

def __main__():
	t = int(read())
	for t0 in range(t):
		n = int(read())
		na = [int(i) for i in read().split()]
		d = dict()
		for x in na:
			if x in d:
				d.update({x: d.get(x) + 1})
			else:
				d.update({x: 1})
		nmax = 0
		for d0 in d:
			a = d.get(d0)
			if a > nmax:
				nmax = a
		res0 = n // 2
		if len(d) <= nmax - 1:
			res1 = len(d)
		else:
			res1 = min(nmax, len(d) - 1)
		out = str(min(res0, res1)) + '\n'
		stdout.write(out)
__main__()


for _ in range(int(input())):
	n = int(input())
	l = sorted(map(int, input().split()))
	(m1, m2, a, b) = (0, 0, 0, 0)
	for i in l:
		if a != i:
			m2 += 1
			a = i
			b = 1
		else:
			b += 1
		if m1 < b:
			m1 = b
	if m1 > n // 2:
		print(min(m2, n // 2))
	else:
		print(max(min(m1 - 1, m2), min(m1, m2 - 1)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	maxi = 0
	diffnum = 0
	temp = 1
	for i in range(1, n):
		if a[i] != a[i - 1]:
			diffnum += 1
			temp = 1
		else:
			temp += 1
		maxi = max(maxi, temp)
	for i in range(maxi, -1, -1):
		c = 1 if maxi - i > 0 else 0
		if i <= diffnum + c:
			print(i)
			break


t = int(input())
while t:
	n = int(input())
	sk = list(map(int, input().split()))
	out = []
	for i in range(n + 1):
		out.append(0)
	for i in range(n):
		out[sk[i]] += 1
	max_m = max(out)
	diff = n + 1 - out.count(0)
	print(max(min(diff - 1, max_m), min(diff, max_m - 1)))
	t = t - 1


from math import *
from collections import Counter
t = int(input())
output = []

def most_frequent(l):
	dic = {}
	for i in range(len(l)):
		if l[i] in dic:
			dic[l[i]] += 1
		else:
			dic[l[i]] = 1
	m = 0
	ind = -1
	for i in dic:
		if dic[i] > m:
			m = dic[i]
			ind = i
	return ind
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	if n <= 1:
		output.append('0')
		continue
	a = most_frequent(p)
	an = p.count(a)
	dic = {}
	t1 = []
	t2 = []
	dic[a] = 1
	for i in range(0, len(p)):
		if an == 0:
			break
		if p[i] not in dic:
			t1.append(p[i])
			t2.append(a)
			an -= 1
			dic[p[i]] = 1
	if an >= 2:
		t2.append(a)
		t1.append(a)
	output.append(str(len(t1)))
for i in output:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mapu = {}
	for i in range(n):
		mapu[a[i]] = mapu.get(a[i], 0) + 1
	maxx = len(mapu)
	maxy = 0
	for key in mapu:
		maxy = max(maxy, mapu[key])
	if maxx == maxy:
		maxx -= 1
	ans = min(maxx, maxy)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for c in a:
		if c in d:
			d[c] += 1
		else:
			d[c] = 1
	uni = len(list(d.keys()))
	ans = 0
	for c in d:
		num = c
		values = d[c]
		left = uni - 1
		if left > values:
			ans = max(ans, values)
		elif values - left <= 1:
			ans = max(ans, left)
		else:
			ans = max(ans, left + 1)
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	c = d = mc = 1
	for i in range(1, n):
		if a[i] == a[i - 1]:
			c += 1
			if c > mc:
				mc = c
		else:
			d += 1
			c = 1
	if mc == d:
		print(d - 1)
	else:
		print(min(d, mc))


T = int(input())
for t in range(T):
	N = int(input())
	P = list(map(int, input().split()))
	P.sort()
	a = 1
	b = 1
	c = 1
	for n in range(1, N):
		if P[n] != P[n - 1]:
			a = a + 1
			c = 1
		else:
			c = c + 1
			if c > b:
				b = c
	if a != b:
		print(min(a, b))
	else:
		print(b - 1)


def solve(n, ll):
	mp = {}
	for i in ll:
		if i not in mp:
			mp[i] = 1
		else:
			mp[i] += 1
	cnt = -10000000
	for i in mp.values():
		cnt = max(cnt, i)
	if len(mp) == cnt:
		return cnt - 1
	if len(mp) > cnt:
		return cnt
	return len(mp)
t = int(input())
for _ in range(t):
	n = int(input())
	ll = list(map(int, input().split()))
	print(solve(n, ll))


for i in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	d = {}
	for i in x:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	max = 0
	for (key, val) in d.items():
		if val > max:
			max = val
	l = len(set(x))
	if l == len(x) and l > 1:
		print(1)
	elif l == len(x) and l == 1:
		print(l - 1)
	elif l - 1 > max:
		print(max)
	elif l < max:
		print(l)
	else:
		print(l - 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dif = [0] * (n + 1)
	for a_i in a:
		dif[a_i] += 1
	unique = 0
	same = 0
	for i in range(n + 1):
		if dif[i] != 0:
			unique += 1
		if dif[i] > same:
			same = dif[i]
	if unique > same:
		print(same)
	if unique == same:
		same -= 1
		if same < 0:
			same = 0
		print(same)
	if unique < same:
		print(unique)


for lo in range(int(input())):
	n = int(input())
	ls = [int(x) for x in input().split()]
	d = {}
	for i in ls:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	d1 = {}
	x = len(d)
	for i in d:
		if d[i] in d1:
			d1[d[i]] += 1
		else:
			d1[d[i]] = 1
	ans = 0
	if x >= 2:
		for i in d1:
			if x - 1 >= i:
				ans = max(i, ans)
			elif i - (x - 1) >= 2:
				ans = max(x, ans)
			else:
				ans = max(x - 1, ans)
	elif n > 1:
		ans = max(ans, 1)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('0')
	elif n == 2:
		print('1')
	else:
		a.sort()
		d = 1
		ml = 1
		cm = 1
		for i in range(n - 1):
			if a[i] == a[i + 1]:
				cm += 1
			else:
				ml = max(ml, cm)
				cm = 1
				d += 1
		d -= 1
		ml = max(cm, ml)
		if d > ml:
			print(min(n, ml))
		elif ml >= d + 2:
			print(min(n, d + 1))
		else:
			print(min(n, d))


num = int(input())
for i in range(num):
	n = int(input())
	skills = list(map(int, input().split(' ')))
	dict = {}
	for i in skills:
		if i not in dict:
			dict[i] = 1
		else:
			dict[i] += 1
	k = list(dict.keys())
	v = list(dict.values())
	max_common = max(v)
	max_distinct = len(k)
	if n > 1:
		if max_common > max_distinct:
			print(max_distinct)
		elif max_common < max_distinct:
			print(max_common)
		else:
			print(max_common - 1)
	else:
		print(0)


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = defaultdict(lambda : 0)
	for j in arr:
		d[j] += 1
	l = len(d)
	mx = 0
	for (j, v) in d.items():
		if v > mx:
			mx = v
	mn = l - 1
	f = True
	while mx > mn:
		mx -= 1
		if f:
			mn += 1
			f = False
	print(mx)


for t in range(int(input())):
	from collections import Counter
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	ans = 0
	same = max(Counter(a).values())
	diff = len(b) - 1
	if same <= diff:
		ans = same
	elif same - diff != 1:
		ans = diff + 1
	else:
		ans = diff
	print(ans)


def countFreq(arr, n):
	mp = dict()
	for i in range(n):
		if arr[i] in mp.keys():
			mp[arr[i]] += 1
		else:
			mp[arr[i]] = 1
		abc = len(mp) - 1
		lola = 0
	for x in mp:
		if mp[x] > lola:
			lola = mp[x]
	final = min(lola, abc)
	if lola - abc >= 2:
		final += 1
	print(final)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	countFreq(arr, n)


t = int(input())
for ti in range(t):
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	d = dict()
	for ai in a:
		if d.get(ai) == None:
			d[ai] = 0
		d[ai] = d[ai] + 1
	m = 0
	for di in d.keys():
		m = max(m, d[di])
	if m > len(d.keys()):
		print(len(d.keys()))
	elif m == len(d.keys()):
		print(len(d.keys()) - 1)
	else:
		print(m)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	if n == 1:
		print(0)
		continue
	d = defaultdict(int)
	for i in arr:
		d[i] += 1
	mx = 0
	for k in d:
		mx = max(mx, d[k])
	cnt = len(d) - 1
	if cnt < mx - 1:
		print(cnt + 1)
	else:
		print(min(cnt, mx))


def pair_size(ls):
	if len(ls) == 1:
		return 0
	d = {}
	for i in ls:
		if i in d.keys():
			d[i] += 1
		else:
			d[i] = 1
	repeated = max((d[k] for k in d))
	return max(min(len(d) - 1, repeated), min(len(d), repeated - 1))
l = [1]
l = [4, 2, 4, 1, 4, 3, 4]
l = [2, 1, 5, 4, 3]
l = [1, 1, 1, 3]
l = [1, 1, 1, 1, 2, 2]
tests = int(input())
i = 0
results = []
while i < tests:
	length = int(input())
	l = [int(e) for e in input().split()]
	results.append(pair_size(l))
	i += 1
for i in results:
	print(i)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	bl = len(b)
	al = len(a)
	if al == 1:
		print(0)
	elif bl == al:
		print(1)
	else:
		c = {}
		for j in a:
			try:
				c[j] += 1
			except:
				c[j] = 1
		c = max(c.values())
		b = bl - 1
		print(max(min(b, c), min(b + 1, c - 1)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	distinct = 0
	d = {}
	max_freq = 0
	for x in l:
		if x not in d:
			distinct += 1
		d.setdefault(x, 0)
		d[x] += 1
		if d[x] > max_freq:
			max_freq = d[x]
	if n == 1:
		print(0)
	elif distinct > max_freq:
		print(max_freq)
	elif distinct == max_freq:
		print(distinct - 1)
	else:
		print(distinct)


t = int(input())
for testcase in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = {}
	same = 0
	for number in a:
		if number in cnt.keys():
			cnt[number] += 1
		else:
			cnt[number] = 1
		if cnt[number] > same:
			same = cnt[number]
	unique = len(cnt.keys())
	if unique == same:
		print(same - 1)
	else:
		print(min(unique, same))


t = int(input())
for i in range(t):
	n = int(input())
	l = [0] * n
	x = input().split()
	y = 0
	for i in range(n):
		l[int(x[i]) - 1] += 1
		if l[int(x[i]) - 1] == 1:
			y += 1
	z = max(l)
	r = min(y - 1, z)
	if r == y - 1 and z > y:
		r = r + 1
	print(r)


elems = dict()
for q in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	sett = set()
	for i in range(n):
		sett.add(s[i])
		elems[s[i]] = elems.setdefault(s[i], 0) + 1
	maxx = max(elems.values())
	if maxx <= len(sett) - 1:
		print(maxx)
	elif len(sett) >= maxx - 1:
		print(maxx - 1)
	else:
		print(maxx - 1 - (maxx - 1 - len(sett)))
	for i in range(n + 1):
		elems[i] = 0


for _ in ' ' * int(input()):
	a = int(input())
	d = dict()
	for i in map(int, input().split()):
		d[i] = d.get(i, 0) + 1
	print(min(len(d), max(d.values())) - (len(d) == max(d.values())))


for t in range(int(input())):
	n = int(input())
	y = [int(i) for i in input().split()]
	d = {k: 0 for k in set(y)}
	for i in y:
		d[i] += 1
	max_d = max(d.values())
	skill = len(d.keys())
	print(max(min(skill - 1, max_d), min(skill, max_d - 1)))


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	s = set()
	dic = dd(int)
	m = [0, 0]
	for el in map(int, input().split()):
		dic[el] += 1
		if m[0] < dic[el]:
			m = [dic[el], el]
		s.add(el)
	k = len(s) - 1
	if k >= m[0]:
		ans = m[0]
	else:
		ans = k + (1 if m[0] - k > 1 else 0)
	print(ans)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	ac = Counter(a)
	mx = 0
	for k in ac:
		mx = max(ac[k], mx)
	l = len(ac)
	print(max(min(l - 1, mx), min(mx - 1, l)))


import sys
import math
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(sys.stdin.readline())
	a = map(int, sys.stdin.readline().split())
	vis = [0] * (n + 1)
	unique = []
	total_skill = 0
	for ai in a:
		if vis[ai] == 0:
			total_skill += 1
			unique.append(ai)
		vis[ai] += 1
	ans = 0
	for x in unique:
		b_num = vis[x]
		if total_skill > b_num:
			ans = max(ans, b_num)
		elif total_skill == b_num:
			ans = max(ans, total_skill - 1)
		else:
			ans = max(ans, total_skill)
	print(ans)


t = int(input())
ans = []
for _ in range(1, t + 1):
	n = int(input())
	s = list(map(int, input().split()))
	d = {}
	for i in s:
		if i in d:
			d[i] = d[i] + 1
		else:
			d[i] = 1
	distinct = len(set(s))
	maxsame = 0
	p = 0
	for i in d:
		if d[i] > maxsame:
			maxsame = d[i]
			p = i
	if maxsame > distinct:
		ans.append(distinct)
	elif maxsame == distinct:
		ans.append(distinct - 1)
	elif maxsame < distinct:
		ans.append(maxsame)
for i in ans:
	print(i)


from sys import stdin, stdout
input = stdin.buffer.readline
t = int(input())
while t > 0:
	n = int(input())
	pre = []
	for i in range(n + 1):
		pre.append(0)
	a = [int(x) for x in input().split()]
	for i in range(n):
		pre[a[i]] += 1
	m = 0
	for i in range(n + 1):
		if m < pre[i]:
			m = pre[i]
	s = set(a)
	k = len(s)
	if k == m:
		print(k - 1)
	elif k > m:
		print(m)
	else:
		print(k)
	t -= 1


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	st = map(int, input().split())
	c = Counter(st)
	unique = len(c)
	same = c.most_common(1)[0]
	size = min(unique, same[1])
	if unique == same[1]:
		size -= 1
	print(size)


t = int(input())
for i in range(t):
	n = int(input())
	array = [int(s) for s in input().split()]
	amount = {}
	for i in range(len(array)):
		if array[i] not in amount:
			amount[array[i]] = 1
		else:
			amount[array[i]] += 1
	distinct = len(amount.keys())
	max_ = -1
	max_idx = -1
	for (key, value) in amount.items():
		if value > max_:
			max_ = value
			max_idx = key
	if max_ == distinct:
		print(max_ - 1)
	else:
		print(min(max_, distinct))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	freq = dict()
	for i in arr:
		if i in freq.keys():
			freq[i] += 1
		else:
			freq[i] = 1
	freq = {k: v for (k, v) in sorted(freq.items(), key=lambda p: p[1], reverse=True)}
	for k in freq.keys():
		maxsize = freq[k]
		break
	ind = 0
	first = 0
	for k in freq.keys():
		if first == 0:
			first = 1
			continue
		if freq[k] == 1:
			ind += 1
	l = len(freq.keys())
	nonind = l - ind - 1
	if nonind < 0:
		nonind = 0
	borrow = False
	if ind + nonind == 0 and maxsize > 1:
		ind = 1
		maxsize -= 1
		borrow = True
	minpos = min(maxsize, ind + nonind)
	if maxsize > minpos and borrow == False:
		ind += 1
		maxsize -= 1
		minpos = min(maxsize, ind + nonind)
		borrow = True
	if maxsize - (ind + nonind) == 2 and borrow == False:
		minpos += 1
	print(minpos)


t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	students = list(map(int, input().split()))
	skills = {}
	for student in students:
		if skills.get(student) is None:
			skills[student] = 1
		else:
			skills[student] += 1
	max_num = 0
	for skill_lvl in skills:
		if skills[skill_lvl] > max_num:
			max_num = skills[skill_lvl]
	if max_num <= len(skills) - 1:
		print(max_num)
	elif max_num == len(skills):
		print(max_num - 1)
	else:
		print(len(skills))


from sys import stdin, stdout
import math
n = int(stdin.readline())
for i in range(n):
	t = int(stdin.readline())
	arr = [int(x) for x in stdin.readline().split()]
	dic = {}
	for i in range(len(arr)):
		if arr[i] in dic:
			dic[arr[i]] += 1
		else:
			dic[arr[i]] = 1
	length = len(dic)
	dic = {a: b for (a, b) in sorted(dic.items(), key=lambda item: item[1], reverse=True)}
	values = list(dic.values())
	if values[0] < length:
		ans = values[0]
	elif values[0] == length:
		ans = length - 1
	else:
		ans = length
	stdout.write(str(ans) + '\n')


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = map(int, input().split())
	d = defaultdict(int)
	for i in a:
		d[i] += 1
	first_team = len(d.keys())
	second_team = max(d.values())
	if first_team == second_team:
		print(first_team - 1)
	else:
		print(min(first_team, second_team))


from collections import *
I = input
exec(int(I()) * 'I();a=Counter(I().split()).values();x=len(a);y=max(a);print(min(x,y)-(x==y));')


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	for i in l:
		d[i] += 1
	m = len(d.keys())
	k = max(d.values())
	if k > m:
		print(min(m, k))
	elif k <= m:
		print(min(m - 1, k))


import math
for x in range(int(input())):
	n = int(input())
	s = input()
	array = []
	for i in s.split():
		array.append(int(i))
	if len(array) < 2:
		print(0)
	else:
		unique = len(set(array))
		dic = {}
		for i in array:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
		m = max(dic.values())
		ma = len(array) // 2
		print(max(min(unique - 1, m), min(m - 1, unique)))


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	raj = dict()
	for i in arr:
		if i in raj:
			raj[i] += 1
		else:
			raj[i] = 1
	uni = len(raj)
	ans = 0
	for i in raj:
		x = raj[i]
		if x > 1:
			x -= 1
			ans = max(ans, min(uni, x))
			ans = max(ans, min(uni - 1, x + 1))
	if n > 1:
		ans = max(1, ans)
	print(ans)


from collections import Counter

def fun(n, u, m):
	if n == 1:
		return 0
	if u - 1 == m:
		return m
	if u - 1 > m:
		return m
	if u - 1 < m:
		return min(u, m - 1)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = dict(Counter(a))
	u = len(c)
	m = max(c.values())
	print(fun(n, u, m))


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	same = []
	distinct = []
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	max_num = max(arr)
	counting = [0] * (max_num + 1)
	mapping = [0] * (max_num + 1)
	for ele in arr:
		counting[ele] += 1
	same_num = counting.index(max(counting))
	for i in range(len(arr)):
		if arr[i] == same_num:
			same.append(arr[i])
			continue
		if mapping[arr[i]] == 0:
			mapping[arr[i]] = 1
			distinct.append(arr[i])
	if len(same) - 1 >= len(distinct) + 1 and mapping[same_num] == 0:
		same.pop()
		distinct.append(0)
	if len(same) >= len(distinct):
		print(len(distinct))
	elif len(same) < len(distinct):
		print(len(same))


t = int(input())
while t:
	n = int(input())
	stud = list(map(int, input().split()))
	store = {}
	dist = 0
	same = 1
	for s in stud:
		if s in store:
			store[s] += 1
			same = max(same, store[s])
		else:
			store[s] = 1
			dist += 1
	if n == 1:
		print('0')
	elif dist <= same:
		if dist == same:
			print(dist - 1)
		else:
			print(dist)
	else:
		print(same)
	t -= 1


import sys
import math
mod = 998244353

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in range(n):
		d[a[i]] = d.get(a[i], 0) + 1
	s = set(a)
	n1 = len(s)
	maxi = 0
	for i in d:
		if d[i] > n1:
			maxi = max(maxi, n1)
		if d[i] == n1:
			maxi = max(maxi, d[i] - 1)
		if d[i] < n1:
			maxi = max(maxi, d[i])
	print(maxi)


from collections import defaultdict
for _ in range(int(input())):
	d = defaultdict(lambda : 0)
	n = int(input())
	a = list(map(int, input().split()))
	for i in a:
		d[i] = d[i] + 1
	m = 0
	c = []
	for i in d.values():
		c.append(i)
	m = max(c)
	l = len(d)
	if l > m:
		print(m)
	elif l < m:
		print(l)
	else:
		print(l - 1)


t = int(input())
for i in range(t):
	n = int(input())
	st = list(map(int, input().split()))
	d = {}
	for elem in st:
		if elem not in d.keys():
			d[elem] = 1
		else:
			d[elem] += 1
	first = len(d)
	m = 0
	for elem in list(d.values()):
		m = max(m, elem)
	if m > first:
		print(first)
	elif m == first:
		print(m - 1)
	else:
		print(m)


import collections
t = int(input())
for ii in range(t):
	a = int(input())
	arr = [int(i) for i in input().split()]
	d = collections.Counter(arr)
	best = max(d, key=d.get)
	temp = d[best]
	arr = d.keys()
	arr = [i for i in arr if i != best]
	one = [best] * temp
	if temp > len(arr):
		arr.append(best)
		temp -= 1
	print(min(len(arr), temp))


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	a1 = set(a)
	d1 = {a[i]: 0 for i in range(0, n)}
	for i in range(0, n):
		d1[a[i]] += 1
	d2 = {}
	for i in range(0, n):
		if d1[a[i]] > 1:
			d2[a[i]] = d1[a[i]]
	if n == 1:
		print('0')
	elif len(d2) == 0:
		print('1')
	else:
		max_same = max(d2.values())
		if max_same == len(a1):
			print(max_same - 1)
		else:
			print(min(max_same, len(a1)))
	t -= 1


import sys
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	val = 0
	key = 0
	res = dict().fromkeys(set(arr), 0)
	for i in arr:
		res[i] += 1
	for i in res:
		if val < res[i]:
			val = max(val, res[i])
			key = i
	a = []
	b = []
	for i in arr:
		if i == key:
			a.append(i)
	for i in set(arr):
		if i != key:
			b.append(i)
	if len(a) > len(b):
		if len(a) == len(b) + 1:
			print(len(b))
		else:
			print(len(b) + 1)
	else:
		print(min(len(a), len(b)))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	lis = Counter(a)
	dist = len(lis.values())
	val = max(lis.values())
	if dist == val:
		print(dist - 1)
	else:
		print(min(dist, val))


T = int(input())
while T:
	D = {}
	n = int(input())
	A = list(map(int, input().split()))
	for i in A:
		if i in D:
			D[i] = D[i] + 1
		else:
			D[i] = 1
	x = len(D.keys())
	y = max(D.values())
	if x == y:
		z = x - 1
	else:
		z = min(x, y)
	print(z)
	T = T - 1


class CountSet(dict):

	def __getitem__(self, item):
		if item in self:
			return super().__getitem__(item)
		else:
			return 0

def main():
	n = int(input())
	d = CountSet()
	mv = 0
	for a in map(int, input().split()):
		d[a] += 1
		if d[a] > d[mv]:
			mv = a
	e = min(len(d.keys()) - 1, d[mv])
	if d[mv] > e:
		print(min(len(d.keys()), d[mv] - 1))
	else:
		print(e)
t = int(input())
for _ in range(t):
	main()


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	(u, s) = (0, 0)
	k = lst[0]
	u = 1
	z = 0
	for j in range(1, n):
		if lst[j] == k:
			z += 1
		else:
			k = lst[j]
			u += 1
			if z > s:
				s = z
			z = 0
	if z > s:
		s = z
	if len(lst) == 1:
		print(0)
	elif s == 0:
		print(1)
	else:
		if u <= s:
			print(u)
		if u > s:
			if u - s > 1:
				s += 1
			print(s)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	d = dict()
	for i in lis:
		d[i] = d.get(i, 0) + 1
	l = len(d.keys()) - 1
	m = max(d.values())
	if m <= l:
		print(m)
	elif m >= l + 2:
		print(l + 1)
	else:
		print(l)


for _ in range(int(input())):
	_ = input()
	students = list(map(int, input().split()))
	skills = {}
	for x in students:
		skills[x] = skills.setdefault(x, 0) + 1
	mx = max(skills.values())
	if mx < len(skills):
		print(mx)
	elif mx == len(skills):
		print(mx - 1)
	else:
		print(len(skills))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(d, maxcnt, diff) = (dict(), 0, 0)
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
			diff += 1
		maxcnt = max(maxcnt, d[i])
	print(max(min(diff - 1, maxcnt), min(maxcnt - 1, diff)))


from collections import Counter
t = int(input())
for wer in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	c = Counter(li)
	key = list(c.keys())
	val = list(c.values())
	lis = []
	if len(val) == 1 and max(val) == 1:
		ma = 0
	elif len(val) > 1 and max(val) == 1:
		ma = 1
	else:
		for i in range(len(key)):
			lis.append([val[i], key[i]])
		ma = 0
		lis.sort(reverse=True)
		for x in lis:
			if x[0] >= len(lis) - 1:
				if x[0] > len(lis):
					ma = len(lis)
					break
				else:
					ma = len(lis) - 1
					break
			else:
				ma = x[0]
				break
	print(ma)


import sys
from collections import Counter
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	student = list(map(int, sys.stdin.readline().split()))
	cnt = Counter(student).most_common()
	for ii in range(len(cnt)):
		cnt[ii] = list(cnt[ii])
	ans = cnt[0][1]
	while ans > 1:
		if ans <= len(cnt) - 1:
			break
		else:
			if ans - 1 == len(cnt):
				ans -= 1
				break
			ans -= 1
	if n == 1:
		print(0)
	else:
		print(ans)


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	cnt = [0] * (n + 1)
	for i in l:
		cnt[i] += 1
	d = len(set(l))
	m = max(cnt)
	check = 0
	if m == d:
		check = 1
	print(min(m, d) - check)
	t -= 1


from collections import Counter
t = int(input())
for i in range(t):
	second_team = []
	skill = -1
	n = int(input())
	l = Counter([int(i) for i in input().split()])
	first_team = len(list(l.keys()))
	second_team = max(list(l.values())) - 1
	if sum(l.values()) == 1:
		print(0)
	elif first_team <= second_team:
		print(first_team)
	elif first_team - second_team > 1:
		print(second_team + 1)
	elif first_team - second_team == 1:
		print(second_team)


from collections import Counter
c = Counter()
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = Counter(l)
	a = len(c)
	b = c.most_common(1)[0][1]
	if b - 1 >= a:
		print(a)
	elif a - 1 >= b:
		print(b)
	else:
		print(a - 1)


def TwoTeams():
	casosDeTestes = int(input())
	while casosDeTestes:
		frequencia = {}
		totalPessoas = int(input())
		arr = list(map(int, input().split()))
		casosDeTestes -= 1
		valoresIguais = 0
		valoresDiferentes = 0
		for integrante in arr:
			if frequencia.get(integrante) == None:
				frequencia[integrante] = 1
				valoresDiferentes += 1
				valoresIguais = max(valoresIguais, frequencia[integrante])
			else:
				frequencia[integrante] += 1
				valoresIguais = max(valoresIguais, frequencia[integrante])
		if valoresIguais == valoresDiferentes - 1:
			print(valoresIguais)
		else:
			valor1 = min(valoresIguais, valoresDiferentes - 1)
			valor2 = min(valoresIguais - 1, valoresDiferentes)
			print(max(valor1, valor2))
TwoTeams()


t = int(input())
while t:
	n = int(input())
	l1 = list(map(int, input().split()))
	d1 = {}
	for i in l1:
		if i in d1:
			d1[i] = d1[i] + 1
		else:
			d1[i] = 1
	a = len(d1.keys())
	b = max(d1.values())
	if a == b:
		print(a - 1)
	elif a < b:
		print(a)
	else:
		print(b)
	t = t - 1


import statistics
from statistics import mode

def most_common(l):
	return mode(l)
for i in range(int(input())):
	n = int(input())
	k = 0
	l = list(map(int, input().split()))
	s = set(l)
	u = most_common(l)
	k = l.count(u)
	f = len(s) - 1
	if f + 2 <= k:
		print(f + 1)
	else:
		print(min(f, k))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	n = n // 2
	d = {}
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] = d[i] + 1
	m = len(d)
	a = max(d, key=d.get)
	if d[a] == m:
		m = m - 1
	print(min(n, m, d[a]))


numcases = int(input())
for i in range(numcases):
	numplayer = int(input())
	player = list(map(int, input().split()))
	player.sort()
	same = 1
	maxsame = 0
	different = 1
	for i in range(numplayer - 1):
		if player[i] == player[i + 1]:
			same = same + 1
		if player[i] != player[i + 1]:
			same = 1
			different = different + 1
		if same > maxsame:
			maxsame = same
	different = different - 1
	if maxsame > different + 1:
		print(different + 1)
	if maxsame == different + 1:
		print(different)
	if maxsame <= different:
		print(maxsame)


t = int(input())
for _ in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	count1 = [0] * (n + 1)
	for x in array:
		count1[x] += 1
	a = max(count1)
	b = len(set(array)) - 1
	if b == 0:
		print(1)
		continue
	if a - b > 1:
		b += 1
	print(min(a, b))


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	a.sort()
	c = 1
	diff = 1
	maxx = 0
	prea = a[0]
	for i in range(1, len(a)):
		if prea == a[i]:
			c = c + 1
			maxx = max(maxx, c)
		else:
			prea = a[i]
			diff = diff + 1
			maxx = max(maxx, c)
			c = 1
	d = max(min(maxx - 1, diff), min(diff - 1, maxx))
	print(d)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = Counter(map(int, input().split()))
	diff = len(a)
	max_same = a.most_common()[0][1]
	if diff == max_same:
		print(diff - 1)
	else:
		print(min(diff, max_same))


t = int(input())
for q in range(t):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	d = {}
	m = 0
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
		if d[i] > m:
			m = d[i]
	s = set(a)
	ll = len(s)
	if m < ll:
		print(m)
	elif m == ll:
		print(m - 1)
	else:
		print(ll)


from collections import Counter
q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	ans = 0
	for e in c:
		ans = max(ans, min(c[e], len(c) - 1), min(c[e] - 1, len(c)))
	print(ans)


def solve():
	n = int(input())
	if n == 1:
		l = [int(i) for i in input().split()]
		print(0)
	elif n == 2:
		l = [int(i) for i in input().split()]
		print(1)
	else:
		l = [int(i) for i in input().split()]
		freq = dict()
		for i in l:
			if i in freq:
				freq[i] += 1
			else:
				freq[i] = 1
		pairs = [(freq[num], num) for num in freq]
		pairs = sorted(pairs)
		max_possible = n // 2
		ones = 0
		for (i, j) in pairs:
			if i == 1:
				ones += 1
		ones = max(ones, len(freq.keys()) - 1)
		max_same = pairs[-1][0]
		sep = min(max_possible, max_same, ones)
		donateone = min(max_possible, max_same - 1, ones + 1)
		ans = max(sep, donateone)
		print(ans)
t = int(input())
for i in range(t):
	solve()


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = dict(Counter(l))
	k = len(c.keys())
	m = max(c.values())
	if k == m:
		print(k - 1)
	else:
		print(min(k, m))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
for i1 in range(int(input())):
	d = dict()
	l = []
	l1 = []
	n = int(input())
	m = 0
	ind = 0
	l = list(map(int, input().split()))
	for i in range(n):
		if l[i] not in d:
			d.update({l[i]: 1})
		else:
			d[l[i]] += 1
		if d[l[i]] > m:
			ind = l[i]
		m = max(d[l[i]], m)
	ans = 0
	for j in d:
		if j != ind:
			ans += 1
	if ans < m - 1:
		ans = ans + 1
		m -= 1
	print(min(ans, m))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	if n == 1:
		print(0)
		continue
	for i in range(n):
		d[a[i]] = d.get(a[i], 0) + 1
	c = set(a)
	count = 0
	for (i, j) in d.items():
		count = max(count, j - 1)
	if count == 0:
		print(1)
	elif len(c) - 2 >= count:
		print(count + 1)
	else:
		print(min(count, len(c)))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	cnts = Counter(A)
	dist = len(cnts)
	(ma, mc) = cnts.most_common(1)[0]
	if dist == mc:
		print(dist - 1)
	else:
		print(min(dist, mc))


from collections import Counter
testcase = int(input())
for i in range(testcase):
	students = int(input())
	array = [int(x) for x in input().split()]
	A = len(Counter(array).keys())
	B = max(Counter(array).values())
	print(max(min(A - 1, B), min(A, B - 1)))


from collections import *
t = int(input(''))
while t != 0:
	t -= 1
	li = []
	n = int(input(''))
	a = [int(a) for a in input('').split()]
	e = Counter(a).values()
	s = set(a)
	(o, p) = (len(s), max(e))
	print(min(o, p) - (o == p))


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	m = {}
	for i in range(len(arr)):
		if m.get(arr[i]):
			m[arr[i]] += 1
		else:
			m[arr[i]] = 1
	mx = -1
	mk = 0
	for k in m.keys():
		if m[k] > mx:
			mx = m[k]
			mk = k
	print(max(min(len(m.keys()) - 1, m[mk]), min(len(m.keys()), m[mk] - 1)))


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
from collections import Counter
for _ in range(I()[0]):
	(n,) = I()
	a = I()
	d = Counter(a)
	maxx = max(d.values())
	if len(d) < maxx:
		print(len(d))
	elif len(d) == maxx:
		print(len(d) - 1)
	else:
		print(maxx)


for _ in range(int(input())):
	input()
	arr = [int(i) for i in input().split()]
	dic = {}
	for x in arr:
		if x in dic:
			dic[x] += 1
		else:
			dic[x] = 1
	t2 = max(list(dic.values()))
	t1 = len(list(dic.keys()))
	if t1 == t2:
		print(t1 - 1)
	else:
		print(min([t1, t2]))


q = int(input())
for q in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	count = [0] * len(a)
	for i in a:
		count[i - 1] += 1
	cnt = 0
	for (i, v) in enumerate(count):
		if v >= 1:
			cnt += 1
	count = sorted(count, reverse=True)
	ans = 0
	for i in count:
		ans = max(min(cnt - 1, i), min(cnt, i - 1), ans)
	print(ans)


from math import floor
T = int(input())
for test in range(T):
	N = int(input())
	data = list(map(int, input().split()))
	stock = {}
	for elt in data:
		if not elt in stock:
			stock[elt] = 1
		else:
			stock[elt] += 1
	idem1 = max(stock.values())
	diff1 = len(stock) - 1
	idem2 = idem1 - 1
	diff2 = diff1 + 1
	print(max(min(diff1, idem1), min(diff2, idem2)))


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	hashmap = {}
	for i in arr:
		if hashmap.get(i):
			hashmap[i] += 1
		else:
			hashmap[i] = 1
	if n < 2:
		print(0)
		continue
	ans = 0
	for i in hashmap:
		if hashmap[i] > ans:
			ans = hashmap[i]
	if ans > len(hashmap):
		print(len(hashmap))
	elif ans == len(hashmap):
		print(len(hashmap) - 1)
	else:
		print(ans)


from collections import defaultdict
t = int(input())
for tt in range(t):
	n = int(input())
	d = defaultdict(int)
	cur_max = 0
	for i in input().split():
		i = int(i)
		d[i] += 1
		cur_max = max(cur_max, d[i])
	unique = len(d)
	if unique > cur_max:
		print(cur_max)
	elif unique < cur_max:
		print(unique)
	else:
		print(cur_max - 1)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cnt = defaultdict(int)
	for a in arr:
		cnt[a] += 1
	ans = 0
	if len(cnt) == 1:
		val: int
		if n > 1:
			print(1)
		else:
			print(0)
		continue
	for val in cnt:
		if cnt[val] != 1:
			ans = max(ans, max(min(cnt[val] - 1, len(cnt)), min(cnt[val], len(cnt) - 1)))
		else:
			ans = max(ans, 1)
	print(ans)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = [0] * n
	for j in range(n):
		a[l[j] - 1] += 1
	z = a.count(0)
	u = n - z
	m = max(a)
	if u - 1 >= m:
		print(m)
	elif u >= m - 1:
		print(m - 1)
	else:
		print(u)


def min(a, b):
	if a < b:
		return a
	else:
		return b

class hashmap(object):

	def __init__(self):
		self.hash = {}

	def push(self, obj):
		self.hash.update({obj: 1})

	def addition(self, obj):
		self.hash[obj] += 1
t = int(input())
for i in range(0, t):
	n = int(input())
	l = input()
	l = l.split()
	x = hashmap()
	for j in range(0, n):
		if l[j] in x.hash:
			x.addition(l[j])
		else:
			x.push(l[j])
	num = list(x.hash.values())
	size_distinct = len(num)
	highest_num = max(num)
	if highest_num > size_distinct:
		print(size_distinct)
	else:
		print(min(highest_num, size_distinct - 1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(_) for _ in input().split()]
	ans = n // 2
	lis = [0] * (n + 1)
	for i in a:
		lis[i] += 1
	ans = n // 2
	(ft, st) = (0, 0)
	ft = max(lis)
	for i in lis:
		if i > 0:
			st += 1
	if ft > st:
		ft -= 1
	else:
		st -= 1
	print(min(ans, min(ft, st)))


t = int(input())
list_of_tests = []
for i in range(t):
	a = int(input())
	b = input().split(' ')
	b = list(map(int, b))
	b.sort()
	list_of_tests.append([a, b])
for lists in list_of_tests:
	l = lists[0]
	lis = lists[1]
	if l == 1:
		print(0)
		continue
	if l == 2:
		print(1)
		continue
	new_list = []
	s = 1
	for i in range(1, l):
		if lis[i - 1] == lis[i]:
			s += 1
			if i == l - 1:
				new_list.append(s)
		else:
			new_list.append(s)
			s = 1
			if i == l - 1:
				new_list.append(s)
	new_list.sort()
	m = new_list[-1]
	s = len(new_list)
	if m == s - 1:
		print(m)
	elif m < s - 1:
		print(m)
	elif m == s:
		print(s - 1)
	else:
		print(s)


from collections import defaultdict
test_cases = int(input())
for _ in range(test_cases):
	l = int(input())
	students_dict = defaultdict(int)
	for v in input().split():
		students_dict[int(v)] += 1
	most_occurances = max(students_dict.values())
	unique_skills = len(students_dict)
	if unique_skills == most_occurances:
		print(most_occurances - 1)
	else:
		print(min(unique_skills, most_occurances))


from collections import Counter as c
for _ in range(int(input())):
	n = int(input())
	d = c(input().split())
	(a, b) = (max(d.values()), len(d) - 1)
	if a <= b:
		print(a)
	elif a - 1 > b:
		print(b + 1)
	else:
		print(b)


import os
import sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		line = input().split()
		arr = []
		for i in range(n):
			arr.append(int(line[i]))
		if len(arr) == 1:
			print('0')
			continue
		d = {}
		for i in arr:
			d[i] = d.get(i, 0) + 1
		ele = max(d.values())
		ans1 = min(len(d) - 1, ele)
		ans2 = min(len(d), ele - 1)
		ans = max(ans1, ans2)
		if ans < 1:
			print('1')
		else:
			print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def sting(n, s):
	s = list(s.split())
	d = {}
	for i in s:
		if i in d:
			d[i] = d.get(i) + 1
		else:
			d[i] = 1
	l = len(d)
	j = max(list(d.values()))
	if j > l:
		if l <= n // 2:
			return l
		else:
			return n // 2
	if j == l:
		return l - 1
	elif j <= n // 2:
		return j
	else:
		return n // 2
t = int(input())
while t > 0:
	n = int(input())
	s = input()
	print(sting(n, s))
	t -= 1


for t in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	di = dict()
	for i in num:
		if i in di.keys():
			di[i] += 1
		else:
			di[i] = 1
	s = list(set(num))
	ma = s[0]
	for i in s:
		if di[i] > di[ma]:
			ma = i
	a = di[ma]
	b = len(s) - 1
	if a > b + 1:
		a -= 1
		b += 1
	print(min(a, b))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = Counter(a)
	mx = 1 if len(d) >= 2 else 0
	c = len(d) - 1
	for (x, y) in d.items():
		if y > 1:
			if y >= c:
				mx = max(mx, min(c, y))
				if c + 1 <= y - 1:
					mx = max(mx, c + 1)
			else:
				mx = max(mx, y)
	print(mx)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	k = len(set(A))
	A = Counter(A)
	t = max(A.values())
	if t == k:
		print(t - 1)
	elif t < k:
		print(t)
	else:
		print(min(t, k))


from collections import Counter
for qq in range(int(input())):
	n = int(input())
	(a, b) = zip(*Counter(map(int, input().split())).items())
	mx = max(b)
	s = len(b) - 1
	print(max(min(mx, s), min(mx - 1, s + 1)))


from collections import Counter

def solve(a):
	n = len(a)
	if n == 1:
		print(0)
	elif len(set(a)) == n:
		print(1)
	else:
		aa = list(set(a))
		d = dict(Counter(a))
		f = 0
		for i in range(len(aa)):
			if d[aa[i]] >= 1:
				x = max(min(d[aa[i]], len(aa) - 1), min(d[aa[i]] - 1, len(aa)))
				if x > f:
					f = x
		print(f)
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	solve(a)


def count_distinct(arr, n):
	arr.sort()
	(res, i) = (0, 0)
	while i < n:
		while i < n - 1 and arr[i] == arr[i + 1]:
			i += 1
		res += 1
		i += 1
	return res

def mostFrequent(arr, n):
	arr.sort()
	max_count = 1
	res = arr[0]
	curr = 1
	for i in range(1, n):
		if arr[i] == arr[i - 1]:
			curr += 1
		else:
			if curr > max_count:
				max_count = curr
				res = arr[i - 1]
			curr = 1
	if curr > max_count:
		max_count = curr
		res = arr[n - 1]
	return max_count
for t in range(int(input())):
	n = int(input())
	F = [int(i) for i in input().split()]
	a = count_distinct(F, len(F))
	b = mostFrequent(F, len(F))
	if a == b:
		print(b - 1)
	elif a <= b:
		print(a)
	else:
		print(b)


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	d = {}
	for e in lst:
		if e in d:
			d[e] += 1
		else:
			d[e] = 1
	m = max([v for v in d.values()])
	s = len(d)
	print(max(min(m, s - 1), min(s, m - 1)))


from collections import Counter
res = []
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	res.append((n, a))
for (n, a) in res:
	if n < 2:
		print(0)
	elif n <= 3:
		print(1)
	else:
		c = [x for x in zip(Counter(a).values(), Counter(a).keys())]
		c.sort(reverse=True)
		if c[0][0] > len(c):
			print(len(c))
		elif c[0][0] == len(c):
			print(c[0][0] - 1)
		else:
			print(c[0][0])


t = int(input())
for i in range(0, t):
	n = int(input())
	a = [int(b) for b in input().split()]
	ans = {}
	for i in range(len(a)):
		if a[i] in ans:
			ans[a[i]] += 1
		else:
			ans[a[i]] = 1
	x = 0
	l = len(ans)
	for i in range(n):
		if ans[a[i]] > x:
			x = ans[a[i]]
	if l == x:
		print(l - 1)
	elif l < x:
		print(l)
	elif l > x:
		print(x)


from collections import defaultdict
t = int(input())

def getMin(a, b):
	if a == b:
		return a - 1
	return min(a, b)
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	d = defaultdict(list)
	lst2 = []
	for val in lst:
		d[val].append(val)
	s = set(lst)
	for (a, b) in d.items():
		lst2.append(len(b))
	lst2.sort(reverse=True)
	print(getMin(len(s), lst2[0]))


T = int(input())
for _ in range(T):
	n = int(input())
	L = list(map(int, input().split()))
	nd = 0
	ns = 0
	A = [0 for i in range(n + 1)]
	for j in range(n):
		i = L[j]
		A[i] = A[i] + 1
		if A[i] == 1:
			nd = nd + 1
		if ns < A[i]:
			ns = A[i]
	print(max(min(ns - 1, nd), min(ns, nd - 1)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n < 2:
		print(0)
		continue
	d = {}
	for i in l:
		t = d.get(i)
		if t == None:
			d[i] = 1
		else:
			d[i] = t + 1
	dis = len(d)
	ma = 0
	for i in d:
		if d[i] > ma:
			ma = d[i]
	if ma == dis - 1:
		print(ma)
	elif ma - 1 == dis:
		print(dis)
	elif dis == ma:
		print(dis - 1)
	elif dis < ma:
		print(dis)
	else:
		print(ma)


def solve(n, studentSkills):
	if n == 1:
		return 0
	skillsSet = set(studentSkills)
	skill2Freq = {}
	freq2Skill = {}
	for skill in studentSkills:
		try:
			skill2Freq[skill] += 1
		except:
			skill2Freq[skill] = 1
	for (k, v) in skill2Freq.items():
		try:
			freq2Skill[v] += [k]
		except:
			freq2Skill[v] = [k]
	maxSameTeam = max(freq2Skill.keys())
	for teamSize in range(maxSameTeam, 0, -1):
		remaining = n - teamSize
		if remaining >= teamSize:
			if teamSize == maxSameTeam:
				if len(skillsSet) - 1 >= teamSize:
					return teamSize
			elif len(skillsSet) >= teamSize:
				return teamSize
		else:
			continue
	return 0
numTestCase = int(input())
for t in range(numTestCase):
	n = int(input())
	studentSkills = list(map(int, input().split()))
	print(solve(n, studentSkills))


from collections import Counter
for _ in range(int(input())):
	ans = 0
	n = int(input())
	ac = Counter(list(map(int, input().split())))
	kl = len(ac.keys())
	if kl == 1:
		if n == 1:
			print(0)
		else:
			print(1)
		continue
	for (k, v) in ac.most_common():
		if v <= kl - 1:
			ans = max(ans, v)
		elif v - 1 >= kl:
			ans = max(ans, kl)
		else:
			ans = max(ans, kl - 1)
	print(ans)


from collections import Counter
t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	d = Counter(arr)
	l = len(set(arr))
	f = 0
	for i in d:
		if d[i] > f:
			f = d[i]
	if f > l:
		print(l)
	elif f == l:
		print(l - 1)
	else:
		print(f)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	dic = {}
	for i in l:
		if i in dic:
			dic[i] = dic[i] + 1
		else:
			dic[i] = 1
	m = max(dic.values())
	x = len(set(l))
	if x != m:
		print(min(x, m))
	else:
		print(m - 1)


from collections import Counter

def calc_max_size(nums):
	n = len(nums)
	freq = Counter(nums)
	s = set(nums)
	l = len(s)
	max_freq = max(freq.values())
	if max_freq <= l - 1:
		return max_freq
	elif max_freq - (l - 1) == 1:
		return l - 1
	elif max_freq - (l - 1) == 2:
		return max_freq - 1
	else:
		return l
for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		print(calc_max_size(nums))


t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		d = {}
		for i in a:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		m = 0
		val = None
		for i in d.keys():
			if m < d[i]:
				m = d[i]
				val = i
		if m >= len(d.keys()) + 1:
			print(len(d.keys()))
		elif m <= len(d.keys()) - 1:
			print(m)
		else:
			print(len(d.keys()) - 1)


import sys
input = sys.stdin.readline
from math import *
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	count = {}
	for i in a:
		count[i] = 0
	for i in a:
		count[i] += 1
	p = []
	for i in count:
		p.append(count[i])
	print(max(min(len(set(a)) - 1, max(p)), min(len(set(a)), max(p) - 1)))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

def solve():
	n = N()
	ar = lis()
	c = counter(ar)
	d = len(c.keys())
	m = max(c.values())
	if d > m:
		print(m)
	else:
		print(min(d, m - 1))
testcase(int(inp()))


def testcase(l, n):
	val = {}
	for i in range(n):
		if l[i] not in val:
			val[l[i]] = 1
		else:
			val[l[i]] += 1
	m = max(val.values())
	print(max(min(len(val) - 1, m), min(len(val), m - 1)))
n = int(input())
for _ in range(n):
	k = int(input())
	l = list(map(int, input().split()))
	testcase(l, k)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	s = set()
	unique = 0
	maxfreq = 1
	freq = {}
	for i in a:
		if i not in s:
			s.add(i)
			unique += 1
		if i not in freq:
			freq[i] = 1
		else:
			freq[i] += 1
			if freq[i] > maxfreq:
				maxfreq = freq[i]
	if maxfreq == 1 and n != 1:
		print(1)
	elif maxfreq < unique:
		print(maxfreq)
	elif maxfreq == unique:
		print(unique - 1)
	else:
		print(unique)
	t -= 1


t = int(input())
while t != 0:
	n = int(input())
	skill = list(map(int, input().split()))
	freq = dict()
	uniqe = dict()
	maxx = 1
	for s in skill:
		if s not in uniqe:
			uniqe[s] = 1
		if s not in freq:
			freq[s] = 1
		else:
			freq[s] += 1
			maxx = max(freq[s], maxx)
	if len(uniqe) == maxx:
		maxx -= 1
	print(min(maxx, len(uniqe)))
	t -= 1


from sys import stdout
import io
import os
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	c = [0 for i in range(n + 1)]
	for i in range(n):
		c[a[i]] += 1
	ma = max(c)
	if len(b) == ma:
		print(ma - 1)
	else:
		print(min(len(b), ma))


t = int(input())
for _ in range(t):
	n = int(input())
	d = dict()
	for i in map(int, input().split()):
		d[i] = d.get(i, 0) + 1
	if n == 1:
		print(0)
		continue
	dis = len(d.keys())
	maxrep = max(d.values())
	if maxrep == 1:
		print(1)
	elif maxrep < dis:
		print(maxrep)
	else:
		print(min(maxrep - 1, dis))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = [0] * (n + 1)
	m = 0
	count = 0
	for i in a:
		f[i] += 1
		m = max(m, f[i])
		if f[i] == 1:
			count += 1
	print(max(min(m, count - 1), min(m - 1, count)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	uniq = 0
	dic = {}
	for i in a:
		if dic.get(i, 0) == 0:
			uniq += 1
			dic[i] = 1
		else:
			dic[i] += 1
	same = max(dic.values())
	if uniq == same:
		print(same - 1)
	else:
		print(min(uniq, same))


import math
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	d = {}
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	b = []
	for (k, v) in d.items():
		b.append(v)
	b.sort()
	maxcnt = max(b)
	ans = max(min(len(b) - 1, maxcnt), min(maxcnt - 1, len(b)))
	print(ans)


from collections import Counter
tst = int(input())
t = 1
while t <= tst:
	n = int(input())
	arr = list((int(x) for x in input().split()))
	x = Counter(arr)
	y = dict(x)
	k = x.values()
	p = x.keys()
	mx1 = 0
	for i in k:
		p = i
		p -= 1
		if i < len(y):
			mx1 = max(mx1, i)
		if p < len(y):
			mx1 = max(mx1, p)
		if p >= len(y):
			mx1 = max(mx1, len(y))
	if mx1 == 0 and len(y) > 1:
		mx1 = 1
	print(mx1)
	t += 1


from collections import Counter

def Most_Common(l):
	data = Counter(l)
	return data.most_common(1)[0][0]
for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	nmc = x.count(Most_Common(x))
	s = set(x)
	nd = len(s)
	res = 0
	if n == 1:
		res = 0
	elif nd == n:
		res = 1
	elif nmc == nd:
		res = nd - 1
	else:
		res = min(nd, nmc)
	print(res)


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(1)
		continue
	d = dict()
	for num in a:
		if num in d:
			d[num] += 1
		else:
			d[num] = 1
	ctu = 0
	dups = []
	for num in d:
		if d[num] == 1:
			ctu += 1
		else:
			dups.append(d[num])
	if len(dups) == 0:
		print(1)
		continue
	cancontrib = len(dups) - 1
	ctu += cancontrib
	ans = max(dups)
	if ans < ctu:
		print(ans)
	elif ans == ctu:
		print(ans)
	else:
		print(min(ctu + 1, ans - 1))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	different = len(set(a))
	most = Counter(a).most_common(1)[0][1]
	print(max(min(different - 1, most), min(different, most - 1)))


from collections import defaultdict
t = int(input())
ans = []
for i in range(0, t):
	n = int(input())
	A = list(map(int, input().split()))
	dic = defaultdict(int)
	for j in range(0, n):
		dic[A[j]] += 1
	talent = len(dic.keys())
	m = max((dic[key] for key in dic.keys()))
	if talent > m:
		ans.append(m)
	elif m > talent:
		ans.append(talent)
	else:
		ans.append(m - 1)
for i in range(0, t):
	print(ans[i])


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter()
	for i in a:
		c[i] += 1
	g1 = max(c.values())
	g2 = len(c.keys())
	print(min(g1, g2) - (g1 == g2))


def maxTeams(n, al):
	dic_of_skills = {}
	for num in al:
		if num not in dic_of_skills:
			dic_of_skills[num] = 0
		dic_of_skills[num] += 1
	maxOcc = 0
	for key in dic_of_skills:
		maxOcc = max(dic_of_skills[key], maxOcc)
	ans = 0
	for i in range(1, n):
		if maxOcc == i:
			if i <= len(dic_of_skills) - 1:
				ans = i
		elif maxOcc > i and i <= len(dic_of_skills):
			ans = i
	print(ans)
t = int(input())
for i in range(t):
	n = int(input())
	al = list(map(int, input().split()))
	maxTeams(n, al)


from statistics import mode
T = int(input())
for _ in range(T):
	n = int(input())
	arr = [int(x) for x in input().split()]
	d = {}
	for i in range(len(arr)):
		if arr[i] in d:
			d[arr[i]] += 1
		else:
			d[arr[i]] = 1
	temp = 0
	l = len(d)
	for i in range(n):
		if d[arr[i]] > temp:
			temp = d[arr[i]]
	if l == temp:
		print(l - 1)
	elif l < temp:
		print(l)
	elif l > temp:
		print(temp)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		d[i] = 0
	for i in a:
		d[i] += 1
	x = len(d)
	y = max(d.values())
	if x == y:
		print(x - 1)
	else:
		print(min(x, y))


import sys
from collections import Counter
input = sys.stdin.buffer.readline

def I():
	return list(map(int, input().split()))

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
for __ in range(int(input())):
	n = int(input())
	a = I()
	if n == 1:
		print(0)
		continue
	c = Counter(a)
	comm = c.most_common(1)
	l = len(c.keys())
	x = 1
	ele = comm[0]
	if l - 1 >= ele[1]:
		print(ele[1])
	elif l >= ele[1] - 1:
		print(ele[1] - 1)
	else:
		print(l)


import math
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = dict()
	for i in range(n):
		d[a[i]] = d.get(a[i], 0) + 1
	mostFrequest = max(d.values())
	amountNumbers = len(d)
	if mostFrequest != amountNumbers:
		print(min(mostFrequest, amountNumbers))
	else:
		print(mostFrequest - 1)


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def getStr():
	return input().strip()
t = getInt()
for _ in range(t):
	n = getInt()
	a = getList()
	d = {}
	m1 = 0
	m2 = 0
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 0
			m1 += 1
		d[a[i]] += 1
		m2 = max(m2, d[a[i]])
	if m1 > m2:
		res = m2
	if m1 == m2:
		res = m2 - 1
	if m1 < m2:
		res = m1
	print(res)


t = int(input())
r = []

def most_frequent(List):
	dict = {}
	(count, itm) = (0, '')
	b = 1
	for item in reversed(List):
		dict[item] = dict.get(item, 0) + 1
		if dict[item] >= count:
			(count, itm) = (dict[item], item)
	return (count, len(dict))
for ooo in range(t):
	n = int(input())
	a = [int(aaa) for aaa in input().split()]
	if n == 1:
		r.append(0)
	else:
		(f, w) = most_frequent(a)
		if f > w:
			r.append(w)
		elif f == w:
			r.append(f - 1)
		else:
			r.append(f)
for i in r:
	print(i)


t = int(input())
for u in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	b = set(a)
	m_c = 1
	c = 1
	for i in range(n - 1):
		if a[i] == a[i + 1]:
			c += 1
			if m_c < c:
				m_c = c
		else:
			c = 1
	print(max(min(len(b) - 1, m_c), min(len(b), m_c - 1)))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	counter = Counter(arr)
	st = set(arr)
	mx = 0
	for v in counter:
		same_count = counter[v]
		unique_others_count = len(st) - 1
		mx = max(mx, min(unique_others_count, same_count))
		mx = max(mx, min(unique_others_count + 1, same_count - 1))
	print(mx)


import math
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	q = 0
	p = 1
	c = 1
	if n == 1:
		print(0)
	else:
		a.sort()
		for i in range(1, n):
			if a[i] == a[i - 1]:
				c += 1
			else:
				c = 1
				p += 1
			if c > q:
				q = c
		if p == q:
			print(q - 1)
		else:
			print(min(p, q))


import sys
from functools import lru_cache
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def outln(var):
	sys.stdout.write(str(var) + '\n')

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	dp = c(arr)
	b = max(dp.values())
	n = len(dp.keys())
	if b <= n:
		answer = min(b, n - 1)
	else:
		answer = n
	outln(answer)


from bisect import bisect_left as lower_bound, bisect_right as upper_bound
from sys import stdin, stdout
from collections import defaultdict

def valid(x, ma):
	(s, cnt) = (-1, -1)
	for num in ma:
		if ma[num] > cnt:
			s = num
			cnt = ma[num]
	if cnt < x:
		return False
	d = 0
	for num in ma:
		c = ma[num] - (x if num == s else 0)
		if c > 0:
			d += 1
	return d >= x

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		ma = {}
		for num in arr:
			ma[num] = ma.get(num, 0) + 1
		(lo, hi) = (0, n)
		ret = -1
		while lo <= hi:
			mid = (lo + hi) // 2
			if valid(mid, ma):
				ret = max(ret, mid)
				lo = mid + 1
			else:
				hi = mid - 1
		print(ret)
main()


from collections import Counter
t = int(input())
while t > 0:
	n = int(input())
	s = list(map(int, input().split()))
	dic = Counter(s)
	ele = dic.most_common(1)[0][0]
	b = dic[ele]
	a = len(dic) - 1
	if b >= a + 2:
		print(a + 1)
	elif a <= b <= a + 1:
		print(a)
	else:
		print(b)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	x = len(d)
	same = 0
	for i in d.values():
		if i > same:
			same = i
	un = x - 1
	if len(a) == 1:
		print(0)
	elif same == 1:
		print(1)
	elif same <= un:
		print(same)
	elif same - un >= 2:
		print(un + 1)
	else:
		print(un)


from collections import Counter
for _ in range(int(input())):
	int(input())
	a = list(map(int, input().split()))
	if len(Counter(a).keys()) == max(Counter(a).values()):
		print(len(Counter(a).keys()) - 1)
	else:
		print(min(len(Counter(a).keys()), max(Counter(a).values())))


for _ in [0] * int(input()):
	input()
	a = b = c = l = 0
	for x in sorted(map(int, input().split())):
		if x != l:
			a += 1
			c = 0
		c += 1
		b = max(b, c)
		l = x
	print(max(min(a, b - 1), min(a - 1, b)))


import sys
import math
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(c) for c in input().split()]
	dic = {}
	for i in arr:
		dic[i] = dic.get(i, 0) + 1
	x1 = len(dic.keys())
	x2 = max(dic.values())
	if x1 > x2:
		print(x2)
	elif x1 == x2:
		print(x1 - 1)
	else:
		print(x1)


t = int(input())
for ig in range(t):
	n = int(input())
	lst = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	else:
		d = {}
		for i in lst:
			d[i] = 0
		for i in lst:
			d[i] += 1
		l = len(d.keys())
		m = max(d.values())
		if m == 1:
			print(1)
		elif m > l:
			print(l)
		elif m == l:
			print(l - 1)
		elif m < l:
			print(m)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	skills = []
	for i in s.split():
		skills.append(int(i))
	skills.sort()
	l = len(skills)
	d = {}
	for j in skills:
		if j in d.keys():
			d[j] += 1
		else:
			d[j] = 1
	mx = max(d.values())
	lk = len(d.keys())
	if lk == 1 and mx == 1:
		print('0')
	elif lk == mx:
		print(mx - 1)
	else:
		print(min(lk, mx))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	count = Counter(a)
	keys = list(count.keys())
	values = list(count.values())
	l = len(keys)
	if l == n:
		print(1)
		continue
	maxx = -1
	for i in values:
		minn = max(min(l - 1, i), min(l, i - 1))
		maxx = max(minn, maxx)
	print(maxx)


for _ in range(int(input())):
	n = int(input())
	counts = [0] * (n + 1)
	singles = 0
	idx = []
	for num in input().split():
		num = int(num)
		if counts[num] == 0:
			singles += 1
			idx.append(num)
		counts[num] += 1
	max_size = 0
	singles -= 1
	for i in idx:
		count = counts[i]
		if max_size > count:
			continue
		cur_size = min(count, singles)
		alt_size = min(count - 1, singles + 1)
		max_size = max(max_size, max(alt_size, cur_size))
	print(max_size)


T = int(input())
for t in range(T):
	N = int(input())
	S = list(map(int, input().split()))
	S.sort()
	a = 1
	b = 0
	c = 1
	for n in range(N - 1):
		if S[n] != S[n + 1]:
			a += 1
			c = 1
		else:
			c += 1
		if c > b:
			b = c
	if a > b:
		print(b)
	if a == b:
		print(a - 1)
	if a < b:
		print(a)


def most_frequent(List):
	dict = {}
	(count, itm) = (0, '')
	for item in reversed(List):
		dict[item] = dict.get(item, 0) + 1
		if dict[item] >= count:
			(count, itm) = (dict[item], item)
	return itm
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	sets = set(arr)
	x = most_frequent(arr)
	num = arr.count(x)
	if num == len(sets):
		print(num - 1)
	else:
		print(min(len(sets), num))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = Counter(l).most_common()
	s = len(d) - 1
	a = d[0][1]
	if s == 0:
		if a == 1:
			print(0)
		else:
			print(1)
	elif s == a:
		print(a)
	elif s > a:
		print(a)
	elif a == s + 1:
		print(s)
	else:
		print(s + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	x = 1
	count = 1
	maxcount = 1
	different = 1
	while x < len(l):
		if l[x] == l[x - 1]:
			count += 1
			if maxcount < count:
				maxcount = count
		else:
			count = 1
			different += 1
		x += 1
	if maxcount > different:
		print(different)
	elif maxcount == different:
		print(maxcount - 1)
	else:
		print(maxcount)


import sys
input = sys.stdin.readline
from collections import defaultdict
al = defaultdict(int)
a = int(input())
for i in range(a):
	x = int(input())
	count = 0
	z = list(map(int, input().split()))
	ans = [0 for i in range(max(z) + 1)]
	for i in range(len(z)):
		if ans[z[i]] == 0:
			count += 1
		ans[z[i]] += 1
	maxa = 0
	for i in range(1, len(ans)):
		if ans[i] == 0:
			continue
		if ans[i] == count - 1:
			maxa = max(ans[i], maxa)
		elif ans[i] < count - 1:
			maxa = max(ans[i], maxa)
		elif ans[i] == count:
			maxa = max(ans[i] - 1, maxa)
		elif ans[i] > count:
			maxa = max(count, maxa)
	print(maxa)


T = int(input())
while T:
	T -= 1
	n = int(input())
	A = []
	di = {}
	for x in input().split():
		a = int(x)
		A.append(a)
		if a not in di:
			di[a] = 1
		else:
			di[a] += 1
	if n == 0 or n == 1:
		print(0)
		continue
	distinct = len(di.keys())
	di = sorted(di.items(), key=lambda item: item[1], reverse=True)
	for (k, v) in di:
		if v > distinct - 1:
			if v > distinct:
				print(distinct)
			else:
				print(distinct - 1)
		else:
			print(v)
		break


t = int(input())
for tt in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	x = max(d.values())
	y = len(d)
	if x == y:
		print(y - 1)
	else:
		print(min(x, y))


def main():
	len_test_cases = int(input())
	for i in range(len_test_cases):
		numstudents = int(input())
		case = input().split()
		if len(case) == 1:
			print(0)
		else:
			countofvars = [0] * (numstudents + 1)
			for i in case:
				countofvars[int(i)] += 1
			max_elem = max(countofvars)
			diff = numstudents + 1 - countofvars.count(0)
			if max_elem > diff:
				print(diff)
			elif max_elem == diff:
				print(diff - 1)
			else:
				print(max_elem)
main()


testCases = int(input())
answers = []
for testCase in range(testCases):
	ignoreInput = input()
	teammates = input().split()
	teammates.sort(reverse=True)
	powers = {}
	maxTarget = len(teammates) // 2
	for mate in teammates:
		if mate in powers:
			powers[mate] += 1
		else:
			powers[mate] = 1
	powersLen = len(powers)
	new_powers = sorted(powers.items(), key=lambda x: x[1], reverse=True)
	if len(teammates) == 1:
		answers.append(maxTarget)
	else:
		for i in range(maxTarget):
			if powersLen == maxTarget:
				isValid = False
				for (power, count) in new_powers:
					if count > maxTarget:
						isValid = True
						break
					else:
						break
				if isValid:
					answers.append(maxTarget)
					break
				maxTarget -= 1
			elif powersLen > maxTarget:
				isValid = False
				for (power, count) in new_powers:
					if count >= maxTarget:
						isValid = True
						break
					else:
						break
				if isValid:
					answers.append(maxTarget)
					break
				maxTarget -= 1
			else:
				maxTarget -= 1
for answer in answers:
	print(answer)


def answer(n, a):
	c = [0] * (n + 1)
	a.sort()
	for i in range(n):
		c[a[i]] += 1
	mx2 = max(c)
	mx1 = n - (c.count(0) - 1)
	if mx2 > mx1:
		mxx = mx1
	elif mx1 > mx2:
		mxx = mx2
	else:
		mxx = mx1 - 1
	return mxx

def main():
	t = int(input())
	a = [0] * t
	n = [0] * t
	for j in range(t):
		n[j] = int(input())
		a[j] = [int(i) for i in input().split()]
	for j in range(t):
		print(answer(n[j], a[j]))
main()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for a in l:
		if a in d:
			d[a] += 1
		else:
			d[a] = 1
	x = max(d.values())
	y = len(d)
	if x > y:
		print(y)
	elif x == y:
		print(x - 1)
	else:
		print(x)


for _ in range(int(input())):
	n = int(input())
	li = [int(i) for i in input().split()]
	d = {}
	for i in li:
		d[i] = d.get(i, 0) + 1
	a = max(d.values())
	c = 0
	for i in d:
		c += 1
	c -= 1
	if a == c:
		print(a)
	elif a > c:
		if a - c >= 2:
			print(c + 1)
		else:
			print(c)
	else:
		print(a)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	size = list(map(int, input().split()))
	temp = list(set(size))
	x = Counter(size)
	count = max(x.values())
	if count == len(temp):
		ans = len(temp) - 1
	else:
		ans = min(count, len(temp))
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split()
	for j in range(n):
		s[j] = int(s[j])
	p = []
	for j in range(n):
		p.append(0)
	for j in range(n):
		p[s[j] - 1] += 1
	x = n - p.count(0)
	y = max(p)
	z = n // 2
	if x == y and z >= x:
		print(x - 1)
		continue
	if z <= y and z <= x:
		print(z)
	elif x <= y:
		print(x)
	else:
		print(y)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	q = 0
	max1 = 0
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	for i in d:
		if d[i] > 1:
			q += 1
			max1 = max(max1, d[i])
	if max1 == 0:
		max1 = 1
	if len(d) < max1:
		print(len(d))
	elif len(d) == max1:
		print(len(d) - 1)
	else:
		print(max1)


from sys import stdin, exit
from collections import Counter
input = stdin.readline

def i():
	return input()

def ii():
	return int(input())

def iis():
	return map(int, input().split())

def liis():
	return list(map(int, input().split()))

def print_array(a):
	print(' '.join(map(str, a)))
t = ii()
for _ in range(t):
	n = ii()
	a = liis()
	count = Counter(a)
	maxi = 0
	seti = set(a)
	for (key, value) in count.items():
		maxi = max(maxi, value)
	print(min(maxi, max(len(seti) - 1, min(len(seti), maxi - 1))))


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	dic = {}
	count = 0
	for i in range(n):
		if ar[i] in dic:
			dic[ar[i]] += 1
		else:
			dic[ar[i]] = 1
			count += 1
	val = 0
	for i in dic:
		if dic[i] > 1:
			x = min(dic[i] - 1, count)
			val = max(val, x)
			x = min(dic[i], count - 1)
			val = max(val, x)
		elif dic[i] == 1:
			x = min(dic[i], count - 1)
			val = max(val, x)
	print(val)


from collections import Counter
T = int(input())
for _ in range(T):
	n = int(input())
	sks = [int(x) for x in input().split()]
	c = Counter(sks)
	s = set(sks)
	take_all = min(len(s) - 1, max(c.values()))
	take_one_less = min(len(s), max(c.values()) - 1)
	print(max(take_all, take_one_less))


import sys
import math
from collections import defaultdict
mod = 1000000007
T = 1
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	d = defaultdict(int)
	m = 0
	for i in l:
		d[i] += 1
		if d[i] > m:
			m = d[i]
	k = len(d)
	print(max(min(m - 1, k), min(m, k - 1)))


t = int(input())
for _ in range(t):
	n = int(input())
	students = list(map(int, input().split()))
	unique = set(students)
	d = {}
	if n == 1:
		print(0)
	else:
		for s in students:
			d[s] = d.get(s, 0) + 1
		common = max(d, key=lambda key: d[key])
		if d[common] == len(unique):
			print(d[common] - 1)
		else:
			print(min(d[common], len(unique)))


from statistics import *
for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		s = len(list(set(l)))
		zz = mode(l)
		m = l.count(zz)
		x = min(s - 1, m)
		if m - 1 > x:
			print(x + 1)
		else:
			print(x)


from itertools import groupby
t = int(input())
a = []
for i in range(t):
	n = int(input())
	a.append(list(map(int, input().split()[:n])))

def dif(m, k):
	if m - k == 1:
		return k
	return k + 1

def chk(k):
	if len(k) == 1:
		return 0
	elif len(set(k)) == len(k) or len(set(k)) == 1:
		return 1
	k.sort()
	n = [sum((1 for _ in group)) for (_, group) in groupby(k)]
	n.sort(reverse=True)
	m = n[0]
	n = n[1:]
	if m <= len(n):
		return m
	elif m > len(n):
		return dif(m, len(n))
for i in a:
	print(chk(i))


t = int(input())
for _ in range(t):
	n = int(input())
	skills = list(map(int, input().split()))
	dictskills = dict()
	if n == 1:
		print(0)
		continue
	for skill in skills:
		if skill in dictskills:
			dictskills[skill] += 1
		else:
			dictskills[skill] = 1
	numberOfSkills = len(dictskills.keys())
	maxStudentsWithASkill = max(dictskills.values())
	if numberOfSkills == maxStudentsWithASkill:
		print(numberOfSkills - 1)
	else:
		print(min(numberOfSkills, maxStudentsWithASkill))


def answer(t):
	for i in range(t):
		n = input()
		a = map(int, input().split())
		d = {}
		for i in a:
			if d.get(i, 0):
				d[i] += 1
			else:
				d[i] = 1
		l = len(d)
		ma = max(d.values())
		if l - 1 >= ma:
			print(ma)
		elif l == ma:
			print(ma - 1)
		elif l == ma - 1:
			print(l)
		else:
			print(l)
answer(int(input()))


import collections as cc
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	l = I()
	f = cc.Counter(l)
	x = len(f)
	y = max(f.values())
	print(min(x, y) - (x == y))


for t in range(int(input())):
	n = input()
	a = list(map(int, input().split(' ')))
	dic = {}
	for i in a:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	x = len(dic)
	ans = 0
	max = 0
	for i in dic:
		if x - 1 == dic[i]:
			if min(x - 1, dic[i]) > ans:
				ans = min(x - 1, dic[i])
		elif x - 1 > dic[i]:
			if min(x - 1, dic[i]) > ans:
				ans = min(x - 1, dic[i])
		elif ans < min(x, dic[i] - 1):
			ans = min(x, dic[i] - 1)
	print(ans)


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = Counter(a)
	m = len(list(cnt.keys()))
	ans = 0
	for v in cnt.values():
		ans = max(ans, min(v, m - 1), min(v - 1, m))
	print(ans)


import sys
from collections import Counter
from heapq import heapify, heappop, heappush
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline
MAX_INT = 2 ** 62 - 1
MOD = 10 ** 9 + 7
test_cases = int(input())
for _ in range(test_cases):
	n = int(input())
	studens = list(map(int, input().split()))
	skills = Counter(studens)
	team1 = len(skills)
	team2 = max(skills.values())
	print(max(min(team1, team2 - 1), min(team1 - 1, team2)))


t = int(input())
j = 0
while j < t:
	n = int(input())
	array = sorted(list(map(int, input().split())))
	maxi = 0
	count = 0
	add = 0
	for i in range(0, n - 1):
		if array[i] == array[i + 1]:
			count += 1
		else:
			add += 1
			maxi = max(maxi, count + 1)
			count = 0
	maxi = max(count + 1, maxi)
	if add >= maxi:
		print(maxi)
	elif maxi - add == 1:
		print(add)
	else:
		print(add + 1)
	j += 1


t = int(input())
skills = []
count = {}
n = 1
summ = 0
different = 0
same = 0
countNr = 0
for i in range(t):
	skillList = []
	skillNr = int(input())
	countNr += skillNr
	if countNr > 2 * 100000:
		exit()
	skillList = input()
	skillList = list(map(int, str.split(skillList)))
	skills += [skillList]
	skillList = []
for i in range(t):
	for character in skills[i]:
		count.setdefault(character, 0)
		count[character] = count[character] + 1
	listval = list(count.values())
	maxsame = max(listval)
	if maxsame > len(listval):
		print(len(listval))
	elif maxsame < len(listval):
		print(maxsame)
	elif maxsame == len(listval):
		print(maxsame - 1)
	listval = []
	count = {}


def team():
	n = int(input())
	arr = list(map(int, input().split()))
	s = set(arr)
	if n == 1:
		return 0
	if len(s) == n:
		return 1
	d = {}
	for element in arr:
		if element in d:
			d[element] += 1
		else:
			d[element] = 1
	unique = len(s)
	max_key = 1
	for element in d:
		if d[element] > max_key:
			max_key = d[element]
	if unique == max_key:
		return unique - 1
	res = min(max_key, unique)
	return res
t = int(input())
for loop in range(t):
	print(team())


def main():
	t = int(input())
	for tt in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		k = set(a)
		d = {}
		for i in range(0, len(a)):
			d[a[i]] = d.get(a[i], 0) + 1
		p = max(d.values())
		if p == len(k):
			print(p - 1)
		else:
			print(min(p, len(k)))
main()


t = int(input())
for loop in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	t2m = 0
	for i in range(n):
		if a[i] not in dic:
			dic[a[i]] = 1
		else:
			dic[a[i]] += 1
		t2m = max(t2m, dic[a[i]])
	t1m = len(dic)
	if t1m > t2m:
		print(t2m)
	elif t1m < t2m:
		print(t1m)
	else:
		print(t1m - 1)


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
import string

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
alph = string.ascii_lowercase
for _ in range(ii()):
	n = ii()
	d = li()
	c = Counter(d)
	a = len(c) - 1
	b = max(c.values())
	if a < b:
		a += 1
		b -= 1
	print(min(a, b))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	if len(s) == n and n > 1:
		print(1)
	else:
		sp = 0
		pc = sorted(l)
		xx = 0
		for i in range(n - 1):
			xx += 1
			if i == n - 2:
				if pc[i] == pc[i + 1]:
					xx += 1
					if xx > sp:
						sp = xx
				if pc[i] != pc[i + 1]:
					if xx > sp:
						sp = xx
					if 1 > sp:
						cp = 1
			if pc[i] != pc[i + 1] and i < n - 2:
				if xx > sp:
					sp = xx
				xx = 0
		if sp < len(s):
			ans = sp
		elif len(s) < sp:
			ans = len(s)
		elif sp == len(s):
			ans = len(s) - 1
		print(ans)


from itertools import cycle, islice
from collections import defaultdict
from collections import Counter
import heapq
import copy
import sys
stream = sys.stdin
t = int(stream.readline())
while stream:
	n = stream.readline()
	if not n:
		break
	n = int(n)
	data = stream.readline().strip().split()
	cnts = Counter(data)
	n_unique = int(len(cnts))
	most_pop = int(cnts.most_common(1)[0][1])
	print(max(min(n_unique - 1, most_pop), min(n_unique, most_pop - 1)))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 0:
		print(0)
	else:
		counter = Counter(l)
		most_common = counter.most_common(1)
		if len(counter) - 1 >= most_common[0][1]:
			print(most_common[0][1])
		else:
			same = most_common[0][1]
			diff = len(counter) - 1
			if same - 1 >= diff + 1:
				print(diff + 1)
			else:
				print(same - 1)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = list(Counter(l).values())
	t = max(c)
	ans = min(t, len(c) - 1)
	(b, a) = (t, len(c) - 1)
	print(max(min(b, a), min(a + 1, b - 1)))


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
testcases = int(input())
for case in range(testcases):
	n = int(input())
	l = [int(zax) for zax in input().split()]
	l.sort()
	map = {}
	j = -1
	distinct = 0
	for i in range(n):
		val = l[i]
		if val not in map:
			map[val] = 1
		else:
			map[val] += 1
		if val != j:
			j = val
			distinct += 1
	best = 0
	for i in map:
		freq = map[i]
		using_all = min(freq, distinct - 1)
		leaving_one_out = min(freq - 1, distinct)
		best = max(best, using_all, leaving_one_out)
	print(best)


t = int(input())
for q in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	used = [0] * (n + 1)
	for i in range(n):
		used[a[i]] += 1
	c = 0
	for i in range(n + 1):
		if used[i] >= 1:
			c += 1
	ans = 0
	for i in range(n + 1):
		ans = max(ans, min(c - 1, used[i]))
		ans = max(ans, min(c, used[i] - 1))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	s1 = len(s)
	m = max(a)
	h = [0] * (m + 1)
	for i in a:
		h[i] += 1
	s2 = max(h)
	if s1 == s2:
		print(s1 - 1)
	else:
		print(min(s1, s2))


from collections import *
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		d = Counter(l)
		f = 0
		ans = list(d.values())
		for i in ans:
			if i > 1:
				f = 1
		if f == 0:
			print(1)
		else:
			a1 = len(ans)
			m1 = max(ans)
			if m1 == a1:
				print(m1 - 1)
			elif m1 > a1:
				print(a1)
			else:
				print(m1)


for i in range(int(input())):
	a = int(input())
	dic = {}
	q = set()
	for i in input().split():
		q.add(i)
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	m = dic[max(dic, key=dic.get)]
	if abs(len(q) - m) == 0:
		print(m - 1)
	else:
		print(min(m, len(q)))


import sys
input = sys.stdin.buffer.readline
for i in range(int(input())):
	students = int(input())
	skill = list(map(int, input().split()))
	skill.sort()
	current_same = 1
	max_same = 1
	for j in range(1, students):
		if skill[j] == skill[j - 1]:
			current_same += 1
		else:
			current_same = 1
		if current_same > max_same:
			max_same = current_same
	item = list(set(skill))
	y = len(item)
	if y > max_same:
		print(max_same)
	elif y == max_same:
		print(y - 1)
	else:
		print(y)


for t in range(int(input())):
	n = int(input())
	skill = list(map(int, input().split()))
	r = set(skill)
	r = list(r)
	maxuniquelength = len(r)
	dict = {}
	for i in range(len(skill)):
		if skill[i] not in dict:
			dict[skill[i]] = 1
		else:
			dict[skill[i]] += 1
	frequency = dict.values()
	l = max(frequency)
	if n == 1:
		print(0)
	elif l == 1:
		print(1)
	elif l < maxuniquelength:
		print(l)
	elif l == maxuniquelength:
		print(l - 1)
	elif l > maxuniquelength:
		print(maxuniquelength)


def solve():
	N = int(input())
	arr = [int(i) for i in input().split()]
	cnt = {}
	for item in arr:
		if item not in cnt:
			cnt[item] = 0
		cnt[item] += 1
	cnt_arr = []
	for (k, v) in cnt.items():
		cnt_arr.append((v, k))
	cnt_arr.sort()
	calen = len(cnt_arr)
	for l in range(calen - 1, -1, -1):
		if l == calen - 1:
			if cnt_arr[calen - 1][0] > l + 1:
				return l + 1
		elif cnt_arr[calen - 1][0] >= l + 1:
			return l + 1
	return 0
T = int(input())
for t in range(T):
	print(solve())


def solve(n, l):
	temp = set(l)
	d = len(temp)
	dic = {}
	for i in range(n):
		if l[i] not in dic:
			dic[l[i]] = 1
		else:
			dic[l[i]] += 1
	s = max(dic.values())
	if s == d:
		print(d - 1)
	else:
		print(min(d, s))
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	solve(n, l)


for _ in range(int(input())):
	n = int(input())
	skill = [0] * (n + 1)
	l = list(map(int, input().split()))
	unique = len(set(l))
	for i in l:
		skill[i] += 1
	max_occur = max(skill)
	size = n // 2
	if max_occur == unique:
		print(min(max_occur - 1, size))
	else:
		print(min(max_occur, size, unique))


test = int(input())
for t in range(test):
	n = int(input())
	L = list(map(int, input().split()))
	d = {}
	for x in L:
		d[x] = d.get(x, 0) + 1
	keymax = max(d, key=d.get)
	tot = len(d.keys())
	if tot < d[keymax]:
		print(tot)
	else:
		print(min(tot - 1, d[keymax]))


t = int(input())
for i10 in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	s = 1
	s1 = 1
	k = 1
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			s += 1
		if a[i] == a[i + 1]:
			k += 1
		else:
			k = 1
		if k > s1:
			s1 = k
	if s1 <= n // 2 and s1 <= s:
		s -= 1
	print(min(n // 2, s, s1))


def convertArrayToInt(A):
	for i in range(0, len(A), 1):
		A[i] = int(A[i])
	return A
testCases = int(input())

def maxTeamSize(A):
	hashMap = {}
	distinctElements = len(set(A))
	for num in A:
		if num in hashMap:
			hashMap[num] += 1
		else:
			hashMap[num] = 1
	maxFreq = max(hashMap.values())
	if distinctElements == maxFreq:
		return maxFreq - 1
	else:
		return min(distinctElements, maxFreq)
while testCases:
	n = int(input())
	X = input().split()
	X = convertArrayToInt(X)
	print(maxTeamSize(X))
	testCases -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	m = 0
	for i in range(n):
		d[a[i]] = d.get(a[i], 0) + 1
		if d[a[i]] > m:
			m = d[a[i]]
	b = len(d) - 1
	if m <= b:
		print(m)
	elif m == b + 1:
		print(b)
	else:
		print(b + 1)


from pprint import pprint
import sys
input = sys.stdin.readline
import collections
q = int(input())
for _ in range(q):
	s = int(input())
	dat = list(map(int, input().split()))
	c = collections.Counter(dat)
	mc = c.most_common(1)[0][0]
	typenum = len(c) - 1
	mcnum = c[mc]
	res = min(mcnum, typenum)
	if mcnum >= typenum + 2:
		res += 1
	print(res)


import sys
import collections
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		c = collections.Counter(A)
		x = len(c) - 1
		y = c.most_common()[0][1]
		z = min(x, y)
		if y - z >= 2:
			print(z + 1)
		else:
			print(z)
main()


import sys
t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	maxi = []
	c = 0
	prec = 0
	for y in range(n):
		if c == 0:
			maxi.append([])
			c = 1
			p = 0
			maxi[0].append(c)
			maxi[0].append(a[y])
		elif a[y] == prec:
			maxi[p][0] += 1
		else:
			maxi.append([])
			p += 1
			c = 1
			maxi[p].append(c)
			maxi[p].append(a[y])
		prec = a[y]
	maxi.sort()
	if len(maxi) - 1 < maxi[-1][0]:
		if maxi[-1][0] - (len(maxi) - 1) >= 2:
			r = len(maxi)
		else:
			r = len(maxi) - 1
	else:
		r = maxi[-1][0]
	print(r)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k1 = min(a)
	k2 = max(a)
	x = [0] * (k2 - k1 + 1)
	for i in range(n):
		x[a[i] - k1] += 1
	res = max(x)
	ind = x.index(res)
	k1 = 0
	for i in range(len(x)):
		if i != ind and x[i] >= 1:
			k1 += 1
	if res - 1 >= k1 + 1:
		print(k1 + 1)
	else:
		print(min(res, k1))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	mp = Counter(a)
	same = max(mp.values())
	distinct = len(mp) - 1
	if same == 1 and distinct > 0:
		print(1)
	elif same == 1 and distinct == 0:
		print(0)
	elif distinct >= same:
		print(same)
	elif same > distinct:
		distinct = distinct + 1
		same = same - 1
		print(min(distinct, same))


t = int(input())
for i in range(t):
	n = int(input())
	distinct = 0
	a = list(map(int, input().split()))
	ofeach = [0] * n
	for j in a:
		ofeach[j - 1] += 1
	m = max(ofeach)
	for j in ofeach:
		if j != 0:
			distinct += 1
	if distinct - 1 >= m:
		print(m)
	else:
		print(min(m - 1, distinct))


from collections import Counter
import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
t = int(sys.stdin.readline().rstrip())
l3 = []
for x in range(t):
	n = int(sys.stdin.readline().rstrip())
	a = list(map(int, sys.stdin.readline().rstrip().split()))
	if n == 1:
		l3.append(0)
	else:
		d = Counter(a)
		l1 = min(max(d.values()), len(d) - 1)
		l2 = min(max(d.values()) - 1, len(d))
		l3.append(max(l1, l2))
for x in l3:
	print(x)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	from collections import defaultdict
	if n <= 1:
		print(0)
		continue
	store = defaultdict(int)
	max_count = 0
	for num in arr:
		store[num] += 1
		if store[num] > max_count:
			max_count = store[num]
	accp = len(store) - 1
	if max_count >= accp + 2:
		ans = accp + 1
	else:
		ans = min(max_count, accp)
	print(ans)


from collections import defaultdict

def foo(n, l):
	d = defaultdict(int)
	for each in l:
		d[each] += 1
	x = y = 0
	for (k, value) in d.items():
		x = max(x, value)
		y += 1
	print(max(min(y - 1, x), min(y, x - 1)))
test = int(input())
for each in range(test):
	n = int(input())
	l = (int(x) for x in input().split())
	foo(n, l)


t = int(input())
lst = list()
for l in range(t):
	same = dict()
	t = list()
	n = int(input())
	x = input()
	t = x.split()
	for item in t:
		same[item] = same.get(item, 0) + 1
	t2 = sorted(same.values(), reverse=True)
	if t2[0] - len(same) >= 1:
		lst.append(len(same))
	else:
		lst.append(min(len(same) - 1, t2[0]))
for i in lst:
	print(int(i))


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	a.sort()
	b = []
	f = []
	m = 0
	if n == 1:
		x = 0
	else:
		b.append(a[0])
		k = 1
		for i in range(1, n):
			if a[i] == a[i - 1]:
				k += 1
			else:
				f.append(k)
				k = 1
				b.append(a[i])
		f.append(k)
		m = max(f)
		l = len(f) - 1
		if m == l:
			x = m
		elif m == l + 1:
			x = l
		elif m > l + 1:
			x = l + 1
		elif m < l:
			x = m
	print(x)


from collections import Counter

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	c = Counter(l)
	size = len(c)
	for i in c.values():
		mx = max(min(size - 1, i), min(size, i - 1))
		ans = max(ans, mx)
	print(ans)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
while t > 0:
	n = int(input())
	a = [int(ele) for ele in input().split()]
	maxi_same = 0
	count = {}
	for ele in a:
		if ele in count.keys():
			count[ele] += 1
		else:
			count[ele] = 1
	for key in count.keys():
		if count[key] > maxi_same:
			maxi_key = key
			maxi_same = count[key]
	maxi_diff = 0
	for key in count.keys():
		if key != maxi_key:
			maxi_diff += 1
	if maxi_diff >= maxi_same:
		print(maxi_same)
	elif maxi_same - maxi_diff > 1:
		print(maxi_diff + 1)
	else:
		print(maxi_diff)
	t -= 1


from sys import stdin, stdout
import string
T = int(stdin.readline())
for i in range(T):
	n = int(stdin.readline())
	nums = [x for x in stdin.readline().split()]
	dict = {}
	max = 0
	for num in nums:
		if dict.get(num) == None:
			dict[num] = 1
			if max == 0:
				max = 1
		else:
			dict[num] += 1
			if dict[num] > max:
				max = dict[num]
	if max == len(dict):
		res = max - 1
	elif max > len(dict):
		res = len(dict)
	else:
		res = max
	stdout.write(str(res) + '\n')


from collections import Counter
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	occ = dict(Counter(a))
	fir = len(occ.keys())
	sec = max(occ.values())
	print(fir - 1) if fir == sec else print(min(fir, sec))


t = int(input())
for i in range(t):
	n = int(input())
	numbers = [int(i) for i in input().split()]
	dice = {}
	m = 0
	for i in numbers:
		if i in dice:
			dice[i] += 1
		else:
			dice[i] = 1
		m = max(m, dice[i])
	tam = len(dice) - 1
	if tam < m:
		m -= 1
		tam += 1
	print(min(m, tam))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 2:
		print(0)
		continue
	maxx = n // 2
	d = Counter(a)
	(modekey, mode) = d.most_common()[0]
	maxx = mode
	rem = list(d.keys())
	if len(rem) >= maxx + 1:
		print(maxx)
	elif len(rem) == maxx:
		print(len(rem) - 1)
	elif len(rem) <= maxx - 1:
		print(len(rem))


for _ in range(int(input())):
	input()
	s = [int(i) for i in input().split()]
	d = {}
	for i in s:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	l = len(d)
	v = max(d.values())
	if l == v:
		print(l - 1)
	elif l > v:
		print(v)
	else:
		print(l)


T = int(input())
for t in range(T):
	N = int(input())
	P = list(map(int, input().split()))
	P.sort()
	b = 1
	B = []
	ris = 0
	for n in range(1, N):
		if P[n] != P[n - 1]:
			if b > 1:
				B.append(b)
				b = 1
		else:
			b = b + 1
	if b > 1:
		B.append(b)
	B.sort(reverse=True)
	s = N - sum(B)
	if len(B) > 0:
		if B[0] <= s + len(B) - 1:
			print(min(B[0], s + len(B) - 1))
		else:
			print(min(B[0] - 1, s + len(B)))
	elif len(P) > 1:
		print(1)
	else:
		print(0)


import math
tc = int(input())
for _ in range(tc):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	z = set(a)
	m = 0
	for i in d:
		if d[i] > m:
			m = d[i]
			mk = i
	if len(z) > m:
		print(m)
	else:
		print(min(len(z), m - 1))


for _ in range(int(input())):
	n = int(input())
	skills = list(map(int, input().split()))
	count = {}
	for player in skills:
		if player in count:
			count[player] += 1
		else:
			count[player] = 1
	unique = len(count) - 1
	ans = -1
	for key in skills:
		ans = max(ans, min(count[key], unique), min(count[key] - 1, unique + 1))
	print(ans)


from collections import Counter as cc
I = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(n,) = I()
	l = I()
	an = 0
	d = cc(l)
	k = len(d)
	for i in d:
		an = max([an, min(k - 1, d[i]), min(d[i] - 1, k)])
	print(an)


t = int(input(''))
for _ in range(t):
	n = int(input(''))
	arr = list(map(int, input().split()))
	cnt = [0] * (n + 1)
	for i in arr:
		cnt[i] += 1
	boro = max(cnt)
	alada = n - cnt.count(0) + 1
	sol = max(min(boro - 1, alada), min(alada - 1, boro))
	print(sol)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	Map = {}
	m = 0
	for i in a:
		if i not in Map:
			Map[i] = 0
		Map[i] += 1
		m = max(m, Map[i])
	l = len(s)
	if m > l:
		print(l)
	elif m == l:
		print(l - 1)
	else:
		print(m)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = [0] * (n + 1)
	n1 = len(set(a))
	for x in a:
		cnt[x] += 1
	n2 = max(cnt)
	print(n1 - 1 if n1 == n2 else min(n1, n2))


test_cases = int(input())
for z in range(test_cases):
	n = int(input())
	stdnt = map(int, input().split())
	stdnt = list(stdnt)
	stdnt.sort()
	count1 = 1
	count2 = 1
	count3 = 1
	for i in range(len(stdnt) - 1):
		if stdnt[i] == stdnt[i + 1]:
			count1 += 1
		else:
			if count1 > count2:
				count2 = count1
			count1 = 1
		if stdnt[i] != stdnt[i + 1]:
			count3 += 1
	if count1 < count2:
		count1 = count2
	if count1 == count3:
		print(count3 - 1)
	elif count1 < count3:
		print(count1)
	if count1 > count3:
		print(count3)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = Counter(a)
	diff = len(list(d.keys()))
	(modekey, mode) = d.most_common()[0]
	print(max(min(mode, diff - 1), min(mode - 1, diff)))


import collections
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = collections.Counter(a)
	b = c.most_common()[0][1]
	ans = 0
	if b > 1:
		if len(set(a)) > b:
			ans = min(b, len(set(a)) - 1)
		else:
			ans = min(b - 1, len(set(a)))
		print(ans)
	else:
		if n > 1:
			ans = 1
		else:
			ans = 0
		print(ans)


import collections
t = int(input())
while t > 0:
	n = int(input())
	ai = list(map(int, input().strip().split()))
	f2 = dict(collections.Counter(ai))
	f2 = f2[max(f2, key=f2.get)]
	f1 = len(set(ai)) - 1
	if f2 > f1:
		f2 -= 1
		f1 += 1
	print(min(f1, f2))
	t -= 1


def solve():
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	diff = len(set(l))
	for i in l:
		d[i] = d.get(i, 0) + 1
	x = list(d.values())
	maxcnt = max(x)
	print(max(min(diff - 1, maxcnt), min(diff, maxcnt - 1)))
for _ in range(int(input())):
	solve()


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	skills = Counter(a)
	unique = set(a)
	m = max(skills.values())
	x = len(unique)
	if m == x:
		print(m - 1)
	else:
		print(min(m, x))


from collections import defaultdict

def solve(n, arr):
	same = defaultdict(int)
	diff = set(arr)
	maxkey = ns = 0
	for ai in arr:
		same[ai] += 1
		if ns < same[ai]:
			(maxkey, ns) = (ai, same[ai])
	diff.remove(maxkey)
	nd = len(diff)
	if ns > nd and nd == 0:
		return 1
	elif ns - nd == 1:
		return nd
	elif ns > nd:
		return nd + 1
	elif ns <= nd:
		return ns
t = int(input())
for _ in range(t):
	n = int(input())
	skills = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	print(solve(n, skills))


def freq_list(li):
	d = {}
	for i in li:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	freq = []
	for i in d.values():
		freq.append(i)
	return freq

def freq_of(n, li):
	d = {}
	for i in li:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	return d.get(n)

def freq_max(li):
	f = freq_list(li)
	maxi = 0
	for i in range(len(f)):
		maxi = max(maxi, f[i])
	return maxi

def num_dup(li):
	f = freq_list(li)
	n = 0
	for i in range(len(f)):
		if f[i] > 1:
			n += 1
	return n
for _ in range(int(input())):
	m = int(input())
	l = list(map(int, input().split()))
	maxi = 0
	maxo = freq_max(l)
	no_dup = num_dup(l)
	l_set = list(set(l))
	non_dup = len(l_set) - 1
	if non_dup > maxo:
		print(maxo)
	elif maxo > non_dup:
		print(min(non_dup + 1, maxo - 1))
	else:
		print(non_dup)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	players = input().split()
	maxCount1 = players.count(Counter(players).most_common(1)[0][0])
	maxCount2 = len(set(players))
	if maxCount1 == maxCount2:
		print(maxCount1 - 1)
	else:
		print(min(maxCount1, maxCount2))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		c = Counter(arr)
		c = c.most_common()
		z = c[0][1]
		if z > len(c):
			print(len(c))
		elif z == len(c):
			print(z - 1)
		else:
			print(z)


t = int(input())
for i in range(t):
	n = int(input())
	li = [int(x) for x in input().split()]
	di = {}
	for i in li:
		if i not in di:
			di[i] = 1
		else:
			di[i] += 1
	a = di[max(di, key=di.get)]
	b = len(di)
	x = max(min(a, b - 1), min(a - 1, b))
	print(x)


t = int(input())
for _ in range(t):
	n = int(input())
	al = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if al[i] in d.keys():
			d[al[i]] += 1
		else:
			d[al[i]] = 1
	ma = -100000000000000
	diff = len(d.keys())
	for i in d.keys():
		if d[i] > ma:
			ma = d[i]
	print(max(min(ma - 1, diff), min(ma, diff - 1)))


t = int(input())
for _ in range(t):
	n = int(input())
	d = {}
	a = [int(s) for s in input().split()]
	m = 0
	c = 0
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 1
		else:
			d[a[i]] += 1
		if m < d[a[i]]:
			m = d[a[i]]
	l = len(list(set(a)))
	if m == 1 and n > 1:
		print(1)
	else:
		print(max(min(l, m - 1), min(l - 1, m)))


for _ in range(int(input())):
	n = int(input())
	A = []
	D = {}
	m = [0, '']
	for i in input().split():
		try:
			D[i] += 1
			if D[i] > m[0]:
				m = [D[i], i]
		except:
			D[i] = 1
			A += [i]
	if m[1] in A:
		A.remove(m[1])
	A = set(A)
	l = len(A)
	if m[0] > l + 1:
		l += 1
	if n == 1:
		print(0)
	elif l == n:
		print(1)
	else:
		print(min(l, m[0]))


n = int(input())
for j in range(n):
	a = int(input())
	mx = 0
	c = [int(x) for x in input().split()]
	fl = False
	d = {}
	for i in c:
		d[i] = d.get(i, 0) + 1
		if mx < d[i]:
			mx = d[i]
	if mx > len(d):
		print(len(d))
	elif mx == len(d):
		print(mx - 1)
	else:
		print(mx)


from collections import Counter

def f(data):
	c = Counter(data)
	mc = c.most_common(1)[0][0]
	maxcnt = c[mc]
	diff = len(set(data))
	return max(min(maxcnt, diff - 1), min(maxcnt - 1, diff))
T = int(input())
for _ in range(T):
	n = int(input())
	data = list(map(int, input().split()))
	print(f(data))


from collections import *
for i in range(int(input())):
	n = int(input())
	lt = []
	l = list(map(int, input().split()))
	m = max(Counter(l).values())
	a = len(set(l))
	if a == m:
		a = a - 1
	print(min(a, m))


from collections import Counter
t = int(input())
while t:
	n = int(input())
	li1 = list(map(int, input().split()))
	if n == 1:
		print(0)
		t -= 1
		continue
	occurence_count = Counter(li1)
	max_tm_elm = occurence_count.most_common(1)[0][0]
	x = li1.count(max_tm_elm)
	if x == 1:
		max_tm_elm = 0
		x = 0
	unique_li1_set = set(li1)
	unique_li1_list = list(unique_li1_set)
	if max_tm_elm in unique_li1_list:
		unique_li1_list.remove(max_tm_elm)
	y = len(unique_li1_list)
	if x == 0:
		print(1)
	elif x == y:
		print(x)
	elif abs(x - y) == 1:
		print(min(x, y))
	elif y - x > 1:
		print(x)
	elif max_tm_elm not in unique_li1_list:
		print(min(x, y) + 1)
	else:
		print(min(x, y))
	t -= 1


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	c = 1
	d = []
	a.sort()
	if len(a) == 1:
		e = 0
	elif a == list(b):
		e = 1
	else:
		for i in range(n - 1):
			if a[i] == a[i + 1]:
				c += 1
			elif c == 1:
				pass
			else:
				d.append(c)
				c = 1
		if c > 1:
			d.append(c)
		e = max(min(len(b) - 1, max(d)), min(len(b), max(d) - 1))
	print(e)


for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	f = 0
	s = set()
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
		f = max(d[i], f)
		s.add(i)
	c = 0
	for i in d:
		if d[i] == 1:
			c += 1
	z = len(s) - 1
	if f > z:
		if f - 2 >= z:
			print(z + 1)
		else:
			print(z)
	else:
		print(f)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l2 = [0 for i in range(n + 1)]
	if n == 1:
		print(0)
		continue
	else:
		d = dict()
		m = 0
		x = 0
		for i in l:
			l2[i] += 1
			m = max(m, l2[i])
		for i in l2:
			if i > 0:
				x += 1
		x = x - 1
		if m - x >= 2:
			print(x + 1)
		else:
			print(min(x, m))


from collections import Counter
import sys
input = sys.stdin.buffer.readline

def list_input():
	return list(map(int, input().split()))

def mult_input():
	return map(int, input().split())
for nt in range(int(input())):
	n = int(input())
	l = list_input()
	c = Counter(l)
	m = 0
	for i in c:
		m = max(m, c[i])
	print(max(min(m, len(c) - 1), min(m - 1, len(c))))


t = int(input())
for test in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = {}
	for i in a:
		c[i] = c.get(i, 0) + 1
	dis = len(list(c.keys()))
	max_same = 0
	for i in list(c.keys()):
		max_same = max(max_same, c[i] - 1)
	if dis == max_same:
		print(dis)
	elif dis > max_same + 1:
		print(max_same + 1)
	elif dis == max_same + 1:
		print(max_same)
	else:
		print(dis)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	z = set(arr)
	f = {}
	for i in arr:
		if i in f:
			f[i] += 1
		else:
			f[i] = 1
	(msize, mel) = (-1, -1)
	for i in f:
		if f[i] > msize:
			mel = i
			msize = f[i]
	a1 = min(msize - 1, len(z))
	a2 = min(msize, len(z) - 1)
	print(max(a1, a2))


for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	d = {}
	for i in range(n):
		if arr[i] not in d:
			d[arr[i]] = 1
		else:
			d[arr[i]] += 1
	m = max(d.keys(), key=lambda k: d[k])
	unique = len(d) - 1
	if unique == d[m]:
		print(unique)
	elif unique <= d[m] - 2:
		print(unique + 1)
	elif unique < d[m]:
		print(unique)
	else:
		print(d[m])


T = int(input())
results = []
for case in range(T):
	n = int(input())
	students = [int(i) for i in input().split()]
	skill_dict = {}
	for student in students:
		if student not in skill_dict.keys():
			skill_dict[student] = 1
		else:
			skill_dict[student] += 1
	max_same = max(skill_dict.values())
	distinct = len(skill_dict.keys()) - 1
	dif = max_same - distinct
	result = 0
	if dif > 1:
		result = distinct + 1
	else:
		result = min(max_same, distinct)
	results.append(result)
for result in results:
	print(result)


from itertools import groupby
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	d = 0
	max = 0
	for (i, j) in groupby(l):
		d += 1
		z = len(list(j))
		if z > max:
			max = z
	if d > max:
		print(max)
	elif max > d:
		print(d)
	else:
		print(d - 1)


for tin in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = list(set(a))
	mapp = {}
	for _ in range(len(s)):
		mapp[s[_]] = 0
	for _ in range(len(a)):
		mapp[a[_]] += 1
	b = []
	for f in mapp.keys():
		b.append(mapp[f])
	b.sort()
	ans = 0
	if b[-1] > 1:
		if b[-1] == len(b) - 1:
			ans = b[-1]
		else:
			ans = max(min(b[-1] - 1, len(b)), min(b[-1], len(b) - 1))
	else:
		ans = min(b[-1], len(b) - 1)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split(' ')))
	numSet = {}
	d = 0
	s = 1
	for num in nums:
		if num not in numSet:
			d += 1
			numSet[num] = 1
		else:
			numSet[num] += 1
			if numSet[num] > s:
				s = numSet[num]
	print(min(d, s, max(d, s) - 1))


from collections import Counter

def teams(n, skills):
	x = len(set(skills))
	y = Counter(skills).most_common(1)[0][1]
	if x == y:
		return x - 1
	return min(x, y)
t = int(input())
for i in range(t):
	n = int(input())
	skills = list(map(int, input().split()))
	print(teams(n, skills))


from collections import Counter
for _ in range(int(input())):
	n = map(int, input().split())
	arr = list(map(int, input().split()))
	cnt = list(Counter(arr).values())
	mx = max(cnt)
	print(max(min(len(cnt) - 1, mx), min(len(cnt), mx - 1)))


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_ls():
	return list(map(int, sys.stdin.readline().strip().split()))

def main():
	for _ in range(int(input())):
		n = int(input())
		ls = get_ls()
		d = dict()
		for x in ls:
			if x not in d:
				d[x] = 1
			else:
				d[x] += 1
		mx = max(d.values())
		s = set(ls)
		len_s = len(s) - 1
		if mx >= len_s + 2:
			len_s += 1
		print(min(mx, len_s))
main()


import collections
t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split(' ')))
	if arr == [1]:
		print(0)
		t -= 1
		continue
	var = []
	x = collections.Counter(arr)
	for (key, value) in x.items():
		var.append(value)
	print(max(min(len(var), max(var) - 1), min(max(var), len(var) - 1)))
	t -= 1


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	team = input().split()
	a = dict(Counter(team))
	team_rep = []
	all_values = a.values()
	max_value = max(all_values)
	team_set_length = len(set(team))
	length = len(team)
	if max_value > team_set_length:
		print(min(max_value, team_set_length))
	else:
		print(min(max_value, team_set_length - 1))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict(Counter(a))
	print(min(max(d.values()), len(d)) if max(d.values()) > len(d) else min(max(d.values()), len(d) - 1))


t = int(input())
for i in range(0, t):
	n = int(input())
	students = input()
	students = list(map(int, students.split()))
	repeated = {}
	nonRepeated = {}
	biggest = {'skill': -1, 'amount': -1}
	for j in range(0, n):
		skill = students[j]
		if skill in nonRepeated:
			repeated[skill] = 2
			if repeated[skill] > biggest['amount']:
				biggest['amount'] = repeated[skill]
				biggest['skill'] = skill
			del nonRepeated[skill]
		elif skill in repeated:
			repeated[skill] = repeated[skill] + 1
			if repeated[skill] > biggest['amount']:
				biggest['amount'] = repeated[skill]
				biggest['skill'] = skill
		else:
			nonRepeated[skill] = 1
	nonRepeatedLength = len(nonRepeated) + len(repeated) - 1
	if len(repeated) == 0:
		if len(nonRepeated) <= 1:
			print(0)
		else:
			print(1)
	elif biggest['amount'] == nonRepeatedLength:
		print(nonRepeatedLength)
	elif biggest['amount'] > nonRepeatedLength:
		if biggest['amount'] - nonRepeatedLength > 1:
			print(nonRepeatedLength + 1)
		else:
			print(nonRepeatedLength)
	else:
		print(biggest['amount'])


t = int(input())
res = []
for i in range(t):
	n = int(input())
	a = map(int, input().split())
	if n == 1:
		res.append(0)
	else:
		d = {}
		for i in a:
			if d.get(i, 0) == 0:
				d[i] = 1
			else:
				d[i] += 1
		u = len(d.keys())
		m = 0
		for i in d.keys():
			if d[i] > m:
				m = d[i]
		if u == m:
			res.append(u - 1)
		else:
			res.append(min(u, m))
for i in res:
	print(i)


t = int(input())
for test in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	from collections import Counter
	c = Counter(arr)
	c = c.values()
	cot = []
	for i in c:
		cot.append(i)
	high = max(cot)
	lng = len(cot)
	if n <= 1:
		print(0)
		continue
	if high == 1:
		print(1)
	elif high > lng:
		print(lng)
	elif high == lng:
		print(high - 1)
	else:
		print(high)


import sys
import math
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = []
	for j in range(n + 1):
		cnt.append(0)
	for j in range(n):
		cnt[a[j]] += 1
	(cntmx, cntct) = (0, 0)
	for j in range(n):
		if cnt[j + 1] != 0:
			cntct += 1
		if cnt[j + 1] > cntmx:
			cntmx = cnt[j + 1]
	print(max(min(cntmx, cntct - 1), min(cntmx - 1, cntct)))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	skills = list(map(int, input().split()))
	cnt = Counter(skills)
	s = set(skills)
	count = 0
	dup = []
	for var in s:
		if cnt[var] == 1:
			count += 1
		else:
			dup.append(cnt[var])
	l = len(dup)
	if l == 0:
		if count > 1:
			print(1)
		else:
			print(0)
		continue
	l2 = max(dup)
	if count > l2:
		print(l2)
		continue
	m1 = min(l - 1, l2 - count)
	m2 = l2 - count - m1
	if m2 >= 2:
		m1 = m1 + 1
	print(count + m1)


for _ in range(int(input())):
	l = [list(map(int, input().split())) for i in '..'][1]
	d = dict()
	for s in l:
		d[s] = d.get(s, 0) + 1
	t1 = len(d) - 1
	t2 = max((v for v in d.values()))
	if t2 > t1:
		t2 -= 1
		t1 += 1
	print(min(t1, t2))


T = int(input())
for t in range(T):
	M = int(input())
	L = list(map(int, input().strip().split()))
	L.sort()
	A = 1
	B = 0
	C = 1
	for d in range(M - 1):
		if L[d] != L[d + 1]:
			A = A + 1
			C = 1
		else:
			C = C + 1
		if C > B:
			B = C
	if A > B:
		print(B)
	if A == B:
		print(B - 1)
	if A < B:
		print(A)


t = int(input())
for _ in range(t):
	length = int(input())
	if length == 1:
		input()
		print(0)
		continue
	students = [int(i) for i in input().split()]
	students_dict = {}
	for i in students:
		if i in students_dict:
			students_dict[i] += 1
		else:
			students_dict[i] = 1
	n_common = students_dict[max(students_dict, key=lambda k: students_dict[k])]
	n_different = len(set(students_dict)) - 1
	if n_common - n_different >= 2:
		print(n_different + 1)
	else:
		print(min(n_common, n_different))


T = int(input())
for t in range(T):
	n = int(input())
	A = [int(i) for i in input().split()]
	A_dict_count = dict()
	for i in A:
		if i in A_dict_count:
			A_dict_count[i] += 1
		else:
			A_dict_count[i] = 1
	dict_len = len(A_dict_count)
	max_size = 0
	for i in A_dict_count:
		max_size = max([max_size, min(dict_len - 1, A_dict_count[i]), min(dict_len, A_dict_count[i] - 1)])
	print(max_size)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	x = Counter(arr).most_common()
	a = x[0][1]
	b = len(set([i for i in arr if i != x[0][0]]))
	if a > b:
		a -= 1
		b += 1
	res = min(a, b)
	print(res)


from sys import stdin

def solve(tc):
	n = stdin.readline().strip()
	skills = list(map(int, stdin.readline().split()))
	hm = dict()
	for skill in skills:
		if skill in hm:
			hm[skill] += 1
		else:
			hm[skill] = 1
	maxi = max(hm.values())
	sz = len(hm) - 1
	ans = min(maxi, sz)
	if maxi > 1:
		ans = max(ans, min(maxi - 1, sz + 1))
	print(ans)
LOCAL_TEST = not __debug__
if LOCAL_TEST:
	infile = __file__.split('.')[0] + '-test.in'
	stdin = open(infile, 'r')
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


import collections
tests = int(input())
anws = []
for i in range(tests):
	numStudents = int(input())
	skillSets = [int(item) for item in input().split(' ')]
	if numStudents < 2:
		print(0)
	else:
		query = collections.Counter(skillSets)
		mostKey = max(query, key=query.get)
		most = query[mostKey]
		others = 0
		for (item, value) in query.items():
			if item != mostKey:
				others += 1
		if others < most - 1:
			print(others + 1)
		else:
			print(min(most, others))


import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
from collections import Counter

def go():
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	m = len(c)
	best = -1
	for cnt in c.values():
		best = max(best, min(cnt, m - 1))
		best = max(best, min(cnt - 1, m))
	return best
t = int(input())
ans = []
for _ in range(t):
	ans.append(str(go()))
print('\n'.join(ans))


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = [int(s) for s in input().split()]
	d = {}
	for i in a:
		try:
			d[i] += 1
		except KeyError:
			d[i] = 1
	max_size = d[a[0]]
	ele_in_sec_arr = a[0]
	for i in d.keys():
		if d[i] > max_size:
			max_size = d[i]
			ele_in_sec_arr = i
	count = 0
	for i in d.keys():
		if i is not ele_in_sec_arr:
			count = count + 1
	if count == max_size:
		print(max_size)
	elif count == max_size - 1:
		print(count)
	elif count <= max_size - 2:
		print(count + 1)
	elif count > max_size:
		print(max_size)


import collections

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	c = collections.Counter(l)
	max_same = max(c.values())
	max_different = len(c) - 1
	return min(max_same, max_different) + 1 if max_same > max_different + 1 and n > 1 else min(max_same, max_different)
for __ in range(int(input())):
	print(solve())


test = int(input())
for i in range(test):
	n = int(input())
	dt = {}
	lt = list(map(int, input().split(' ')))
	if n == 1:
		print(0)
		continue
	for j in lt:
		if j in dt:
			dt[j] += 1
		else:
			dt[j] = 1
	a = max(dt, key=dt.get)
	s = set(lt)
	b = dt[a]
	s.remove(a)
	if len(s) >= b:
		print(b)
	elif len(s) + 1 > b - 1:
		print(len(s))
	else:
		print(len(s) + 1)


from collections import defaultdict as ddict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ad = ddict(int)
	for it in a:
		ad[it] += 1
	dist = len(ad)
	same = max(ad.values())
	if n != 1:
		ans1 = min(same, dist - 1)
		ans2 = min(same - 1, dist)
		ans = max(ans1, ans2)
	else:
		ans = 0
	print(ans)


from collections import Counter
from sys import stdin

def main():
	input = lambda : stdin.readline()[:-1]
	T = int(input())
	for _ in [0] * T:
		N = int(input())
		A = map(int, input().split())
		team = Counter(A).most_common()
		if team[0][1] - (len(team) - 1) > 1:
			ans = len(team)
		else:
			ans = min(team[0][1], len(team) - 1)
		print(ans)
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	rem = [0] * (n + 1)
	dis = 0
	for j in range(n):
		rem[l[j]] += 1
		if rem[l[j]] == 1:
			dis += 1
	m = max(rem)
	print(max(min(m - 1, dis), min(m, dis - 1)))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = Counter(l)
	x = c[l[0]]
	y = l[0]
	for i in c:
		if c[i] > x:
			x = c[i]
			y = i
	print(max(min(len(c), x - 1), min(len(c) - 1, x)))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b1 = dict(Counter(a))
	b = len(b1)
	x = max(b1.values())
	y = min(x, b)
	if y == x and y == b:
		print(y - 1)
	else:
		print(y)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	a.sort()
	c = []
	u = []
	num = 0
	mx = 0
	j = 0
	for i in range(n - 1):
		if a[i] == a[i + 1]:
			j += 1
			if j == 1:
				j += 1
		else:
			mx = max(j, mx)
			j = 0
	mx = max(j, mx)
	l = len(list(set(a)))
	if mx != 0:
		l -= 1
	if mx == 0 and l > 1:
		print(1)
		continue
	if min(len(b), len(c)) == n // 2:
		print(n // 2)
	else:
		print(max(min(l, mx), min(l + 1, mx - 1)))


def main():
	t = int(input())
	for i in range(0, t):
		solve()

def solve():
	n = int(input())
	lista = list(map(int, input().split()))
	lista.sort()
	anterior = lista[0]
	(maior, atual, diferentes) = (1, 1, 1)
	for i in range(1, n):
		if lista[i] == anterior:
			atual += 1
		else:
			maior = max(maior, atual)
			diferentes += 1
			atual = 1
			anterior = lista[i]
	maior = max(maior, atual)
	if diferentes > maior:
		print(maior)
	elif maior > diferentes:
		print(diferentes)
	else:
		print(maior - 1)
main()


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	d = []
	dem = 0
	for i in range(0, n + 1):
		d.append(i)
		d[i] = 0
	n //= 2
	a = list(map(int, input().split()))
	for i in a:
		d[i] += 1
		if d[i] == 1:
			dem += 1
	ma = max(d)
	if dem != ma:
		print(min(ma, dem, n))
	else:
		print(min(dem - 1, n))


def solve(n, a):
	count = {}
	for i in a:
		if i in count:
			count[i] += 1
		else:
			count[i] = 1
	max_occur = None
	for j in count:
		if not max_occur:
			max_occur = j
		if count[j] > count[max_occur]:
			max_occur = j
	others = len(count)
	max_occurance = count[max_occur]
	return max(min(max_occurance - 1, others), min(max_occurance, others - 1))

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = input()
		a = [int(i) for i in a.split()]
		ans = solve(n, a)
		print(ans)
main()


from sys import stdin
from collections import Counter
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = Counter(a)
	m1 = c.most_common(1)[0][1]
	m2 = len(c.keys())
	print(max(min(m1 - 1, m2), min(m1, m2 - 1)))


import collections
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	cnt = collections.Counter(arr)
	variety = len(cnt.keys())
	largest = max(cnt.values())
	print(max(min(variety - 1, largest), min(variety, largest - 1)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	freq = [0] * (n + 1)
	for el in a:
		freq[el] += 1
	a = set(a)
	(mx, l) = (0, len(a))
	for el in a:
		mx = max(mx, min(freq[el], l - 1))
		mx = max(mx, min(l, freq[el] - 1))
	print(mx)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = list(set(a))
	nL = len(s)
	r = {}
	for i in a:
		r[i] = r.get(i, 0) + 1
	x = 0
	for i in s:
		if nL - 1 < r[i]:
			x = max(x, min(r[i] - 1, nL))
		else:
			x = max(x, min(r[i], nL - 1))
	print(x)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = dict()
	for t in a:
		if t not in x:
			x[t] = 0
		x[t] += 1
	w = max(x.values())
	a = set(a)
	ke = max(x.keys(), key=lambda k: x[k])
	a.remove(ke)
	z = len(a)
	if len(a) + 1 <= w - 1:
		z = len(a) + 1
	print(min(z, w))


def most_frequent(List):
	occurence_count = Counter(List)
	return occurence_count.most_common(1)[0][0]
from collections import Counter
t = int(input())
for i in range(t):
	distinct_list = {}
	list = []
	n = int(input())
	list = input().split()
	distinct_list = Counter(list).keys()
	distinct = len(distinct_list)
	most = most_frequent(list)
	max = list.count(most)
	if max > 1 and max == distinct:
		max = max - 1
	if n == 1:
		ans = 0
	else:
		ans = min(int(distinct), int(max))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	d = [0] * (n + 1)
	for i in l:
		d[i] = d[i] + 1
	st = len(s)
	m = max(d)
	ans1 = min(st, m - 1)
	ans2 = min(st - 1, m)
	ans = max(ans1, ans2)
	print(ans)


import math
import random

def encryption(s):
	s = s.replace(' ', '')
	tamanho = len(s)
	raiz = math.sqrt(tamanho)
	rows = math.floor(raiz)
	columns = math.ceil(raiz)
	matrizPalavras = []
	counter = 0
	sFinal = ''
	if rows * columns < tamanho:
		rows += 1
	for i in range(rows):
		sBuffer = ''
		for j in range(columns):
			if counter < len(s):
				sBuffer += s[counter]
			counter += 1
			if counter >= len(s):
				sBuffer += ' '
		matrizPalavras.append(sBuffer)
	for i in range(columns):
		sBuffer = ''
		for j in range(rows):
			if matrizPalavras[j][i] != ' ':
				sBuffer += matrizPalavras[j][i]
		sFinal += sBuffer + ' '
	return sFinal

def nightAtMuseum():
	alfabeto = ['o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
	rotacoes = 0
	ponteiroF = 12
	ponteiroA = 12
	string = input()
	for c in string:
		charPF = ''
		charPA = ''
		while charPF != c and charPA != c:
			if ponteiroF == len(alfabeto):
				ponteiroF = 0
			if ponteiroA == -1:
				ponteiroA = 25
			charPF = alfabeto[ponteiroF]
			charPA = alfabeto[ponteiroA]
			if charPF != c and charPA != c:
				ponteiroF += 1
				ponteiroA -= 1
				rotacoes += 1
		ponteiroA = alfabeto.index(c)
		ponteiroF = alfabeto.index(c)
	print(rotacoes)

def constructString():
	t = int(input())
	for i in range(t):
		n = input()
		n = n.split(' ')
		tamanho = int(n[0])
		substring = int(n[1])
		diferentes = int(n[2])
		stringFinal = ''
		counterString = 0
		counter = 0
		counterDiferentes = 0
		while counter != substring:
			if counterDiferentes != diferentes:
				c = random.randint(97, 122)
				while chr(c) in stringFinal:
					c = random.randint(97, 122)
				stringFinal += chr(c)
				counterDiferentes += 1
			else:
				c = random.choice(stringFinal)
				stringFinal += c
			counter += 1
			counterString += 1
		for j in range(tamanho - substring):
			counterDiferentes = 0
			listaDiferentes = []
			for char in stringFinal[j + 1:len(stringFinal)]:
				if char not in listaDiferentes:
					listaDiferentes.append(char)
					counterDiferentes += 1
			if counterDiferentes < diferentes or substring == 1:
				c = random.randint(97, 122)
				while chr(c) in listaDiferentes:
					c = random.randint(97, 122)
				stringFinal += chr(c)
				listaDiferentes.append(c)
			else:
				c = random.choice(listaDiferentes)
				stringFinal += c
		print(stringFinal)

def twoTeams():
	t = int(input())
	for i in range(t):
		input()
		ns = input()
		ns = ns.split(' ')
		x = 0
		t1 = set()
		t2 = {}
		mN = 1
		for j in range(len(ns)):
			n = int(ns[j])
			flag = 0
			if n in t2.keys():
				num = t2.get(n)
				if num + 1 > mN:
					mN = num + 1
				t2.update({n: num + 1})
				flag = 1
			if flag == 0:
				t2.update({n: 1})
			if n not in t1:
				t1.add(n)
		if len(t1) - 1 >= mN:
			x = mN
		elif len(t1) >= mN - 1:
			x = mN - 1
		elif mN >= len(t1):
			x = len(t1)
		elif mN >= len(t1) - 1:
			x = len(t1) - 1
		print(x)
twoTeams()


t = int(input())
res = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	dup = []
	uni = []
	if n <= 1:
		res.append(0)
		continue
	for el in a:
		if el not in d:
			d[el] = 1
		else:
			d[el] += 1
	for el in a:
		if d[el] == 1:
			uni.append(el)
		elif d[el] > 1 and el not in dup:
			dup.append(el)
	(m, mk) = (0, 0)
	if len(dup) == 0:
		res.append(1)
		continue
	for el in dup:
		if d[el] > mk:
			m = el
			mk = d[el]
	for el in dup:
		if el != m and el not in uni:
			uni.append(el)
	rteam = [m] * mk
	if len(rteam) - len(uni) >= 2:
		uni.append(rteam[0])
		del rteam[0]
	res.append(min(len(rteam), len(uni)))
for el in res:
	print(el)


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	count = [0] * (n + 1)
	if len(arr) == 1:
		print(0)
	else:
		for i in arr:
			count[i] += 1
		maxx = 1
		lenn = len(set(arr))
		equal = max(count)
		if equal == 1:
			print(1)
		else:
			print(max(min(equal, lenn - 1), min(equal - 1, lenn)))
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(1)
		continue
	elif n == 3:
		print(1)
		continue
	sample_dict = dict()
	max_key = 0
	max_value = 0
	for j in a:
		sample_dict[j] = sample_dict.get(j, 0) + 1
		if sample_dict[j] > max_value:
			(max_key, max_value) = (j, sample_dict[j])
	if len(sample_dict) > max_value:
		print(max_value)
	elif len(sample_dict) == max_value:
		print(max_value - 1)
	elif len(sample_dict) < max_value:
		print(len(sample_dict))


from collections import Counter
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	counter = Counter(a)
	x = len(counter)
	y = counter.most_common(1)[0][1]
	if x == y:
		y -= 1
	print(min(x, y))


def f(len1):
	d = {}
	been = set()
	for j in range(n):
		if l[j] in been:
			d[l[j]] += 1
		else:
			been.add(l[j])
			d[l[j]] = 1
	val = d.values()
	ma = max(val)
	s = set(l)
	if ma - len1 > 0:
		if len(s) >= len1:
			return True
		return False
	if ma - len1 == 0:
		if len(s) - 1 >= ma:
			return True
		return False
	return False
t = int(input())
for i in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	le = 0
	ra = 10 ** 10
	while ra - le > 1:
		m = (le + ra) // 2
		if f(m):
			le = m
		else:
			ra = m
	print(le)


from collections import Counter
for test in range(int(input())):
	n = int(input())
	lt = list(map(int, input().split()))
	nelt = list(dict.fromkeys(lt))
	mx = Counter(lt)
	mx = mx.most_common(1)[0][0]
	mx = lt.count(mx)
	c = len(nelt)
	if c < mx:
		print(c)
	else:
		print(min(c - 1, mx))


import sys
t = int(sys.stdin.readline())
for query in range(t):
	n = int(sys.stdin.readline())
	a = sys.stdin.readline().strip().split()
	nums = {}
	for x in a:
		try:
			nums[x] += 1
		except:
			nums[x] = 1
	mx = 0
	for num in nums:
		mx = max(mx, nums[num])
	dif = len(list(nums)) - 1
	ans = 1
	if mx > dif:
		mx -= 1
		dif += 1
	ans = min(mx, dif)
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	counter = Counter(arr)
	unique = [val for val in counter.values() if val == 1]
	non_unique = [val for val in counter.values() if val > 1]
	if n == 1:
		print(0)
		continue
	if not non_unique:
		print(1)
	else:
		(l, m) = (len(unique), max(non_unique))
		l += len(non_unique) - 1
		if l == m:
			print(l)
		elif l > m:
			print(m)
		elif m > l:
			print(min((l + 1, m - 1)))


import sys
input = sys.stdin.buffer.readline
from collections import Counter

def main():
	n = int(input())
	a = list(map(int, input().split()))
	fre = Counter(a).values()
	if n == 1:
		return 0
	else:
		(mx, ln) = (max(fre), len(fre))
		x = min(mx, ln)
		if x <= n // 2:
			if mx == ln:
				return mx - 1
			else:
				return x
		else:
			return n // 2
t = int(input())
while t:
	t -= 1
	print(main())


import math
from collections import Counter
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
I = lambda : int(input())
IN = lambda : input()

def s(a):
	print(' '.join(list(map(str, a))))
for i in range(I()):
	n = I()
	a = L()
	b = set(a)
	l = len(b)
	D = Counter(a)
	Keymax = max(D, key=D.get)
	x = D[Keymax]
	if l == x:
		print(l - 1)
	elif l > x:
		print(x)
	elif x > l:
		print(l)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
for i in range(ii()):
	n = ii()
	d = collections.Counter(li())
	v = sorted(d.values())
	k = len(d.keys())
	if n == 1:
		print(0)
	elif v[-1] > k:
		print(k)
	elif v[-1] < k:
		print(v[-1])
	elif v[-1] == k:
		print(k - 1)


from collections import defaultdict
for ii in range(int(input())):
	n = int(input())
	maxx = n // 2
	arr = list(map(int, input().split()))
	m = defaultdict(int)
	for i in arr:
		m[i] += 1
	alag = len(m.keys())
	same = max(m.values())
	print(max(min(alag - 1, same), min(alag, same - 1)))


t = int(input())
for i in range(t):
	n = int(input())
	lst1 = list(map(int, input().split()))
	a = list(set(lst1))
	a = sorted(a)
	dict1 = {}
	for i in range(n):
		if lst1[i] in dict1:
			dict1[lst1[i]] += 1
		else:
			dict1[lst1[i]] = 1
	i = len(a) - 1
	n = len(a)
	max1 = 0
	while i >= 0:
		if dict1[a[i]] > 1:
			max1 = max(max1, min(dict1[a[i]] - 1, n))
			max1 = max(max1, min(dict1[a[i]], n - 1))
		else:
			max1 = max(max1, min(dict1[a[i]], n - 1))
		i -= 1
	print(max1)


T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	ma = 1
	dic = {}
	for i in arr:
		if i in dic.keys():
			dic[i] += 1
			if ma < dic[i]:
				ma = dic[i]
		else:
			dic[i] = 1
	if len(dic) < ma:
		print(len(dic))
	elif len(dic) == ma:
		print(max(len(dic) - 1, 0))
	else:
		print(ma)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	d = (n + 1) * [0]
	for i in a:
		d[i] += 1
	beshi = max(d)
	l = len(set(a))
	if beshi < l - 1:
		print(beshi)
	elif beshi - l + 1 >= 2:
		print(l)
	else:
		print(l - 1)
	t = t - 1


import collections

def solve(n, skills):
	if n <= 1:
		return 0
	elif n == 2:
		return 1
	counts = collections.Counter(skills)
	distinctElementsCount = 0
	sameElements = 0
	maxRepeatingElemCount = max(counts.values())
	totalSum = sum([1 for k in counts.keys()]) - 1
	if totalSum == maxRepeatingElemCount:
		return totalSum
	elif totalSum < maxRepeatingElemCount:
		return min(totalSum + 1, maxRepeatingElemCount - 1)
	else:
		return maxRepeatingElemCount
t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	skills = list(map(int, input().strip().split()))
	print(solve(n, skills))


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	dupa = list(a)
	sa = set(dupa)
	da = {}
	for x in sa:
		da[x] = 0
	for x in dupa:
		da[x] += 1
	vals = []
	for x in da.values():
		vals.append(x)
	if n == 1:
		print(0)
	elif set(vals) == set([1]):
		print(1)
	elif len(sa) - 1 >= max(vals):
		print(max(vals))
	elif len(sa) == max(vals):
		print(len(sa) - 1)
	elif len(sa) + 1 == max(vals):
		print(len(sa))
	else:
		print(len(sa))


t = int(input())
while t > 0:
	t = t - 1
	dct = {}
	n = int(input())
	lst = list(map(int, input().split(' ')))
	if n == 1:
		print(0)
		continue
	for i in lst:
		if i in dct:
			dct[i] += 1
		else:
			dct[i] = 1
	a = max(dct, key=dct.get)
	b = dct[a]
	st = set(lst)
	bst = st.copy()
	st.remove(a)
	if len(st) >= b:
		print(b)
	elif len(st) + 1 <= b - 1:
		print(len(st) + 1)
	else:
		print(len(st))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	dic = {}
	for c in l:
		if c in dic:
			dic[c] += 1
		else:
			dic[c] = 1
	ct1 = len(dic.keys()) - 1
	ct2 = max(dic.values())
	ans = 0
	if ct2 == ct1:
		ans = ct1
	elif ct2 > ct1:
		ans = min(ct2 - 1, ct1 + 1)
	else:
		ans = ct2
	print(ans)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007
for t in range(I()):
	n = I()
	arr = L()
	dict1 = {}
	(mx, nk) = (0, 0)
	for i in arr:
		if i in dict1.keys():
			dict1[i] += 1
		else:
			dict1[i] = 1
			nk += 1
		mx = max(mx, dict1[i])
	if mx > nk:
		print(nk)
	elif mx == nk:
		print(nk - 1)
	else:
		print(mx)


t = int(input())
for i in range(t):
	n = int(input())
	flag = 0
	skills = list(map(int, input().split()))
	skill_count = {a: 0 for a in set(skills)}
	crit_1 = n // 2
	crit_2 = len(set(skills))
	for elm in skills:
		skill_count[elm] += 1
	crit_3 = max(skill_count.values())
	if crit_3 == crit_1:
		if crit_3 > crit_2:
			print(crit_2)
		elif crit_3 == crit_2:
			print(crit_3 - 1)
		else:
			print(crit_3)
	elif crit_3 > crit_1:
		print(min(crit_2, crit_1))
	elif crit_3 > crit_2:
		print(crit_2)
	elif crit_3 == crit_2:
		print(crit_3 - 1)
	else:
		print(crit_3)


from collections import Counter

def solve():
	_ = int(input())
	l = list(map(int, input().split()))
	counts = Counter(l)
	if len(counts) == 1:
		(k, v) = counts.most_common()[0]
		return 0 if v == 1 else 1
	ans = 0
	rem_size = len(counts) - 1
	for k in counts:
		v = counts[k]
		if rem_size >= v:
			ans = max(ans, v)
		else:
			size_diff = v - rem_size
			ans = max(ans, rem_size + (size_diff > 1))
	return ans

def main():
	t = int(input())
	for _ in range(t):
		ret = solve()
		print(ret)
main()


from collections import Counter, defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(lambda : 0)
	for i in l:
		d[i] += 1
	x = max(d.values())
	y = len(d) - 1
	if x == y:
		print(x)
	elif x < y:
		print(x)
	elif x - y >= 2:
		print(min(x - 1, y + 1))
	else:
		print(min(x, y))


t = int(input())
for _ in range(t):
	t1 = int(input())
	s1 = list(map(int, input().split()))
	d = {}
	n = 0
	for i in range(t1):
		if s1[i] in d.keys():
			d[s1[i]] += 1
		else:
			d[s1[i]] = 1
			n = n + 1
	m1 = max(d.values())
	if n == 1 and m1 > 1:
		print(1)
	elif n == 1 and m1 == 1:
		print(0)
	elif m1 > 1:
		m2 = min(n, m1 - 1)
		m3 = min(n - 1, m1)
		print(max(m2, m3))
	else:
		print(1)


t = int(input())
for _ in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	lis.sort()
	tots = []
	tots.append([1, lis[0]])
	for i in range(1, n):
		if lis[i] == tots[-1][1]:
			tots[-1][0] += 1
		else:
			tots.append([1, lis[i]])
	tots.sort(reverse=True)
	l = len(tots)
	ans = 0
	if tots[0][0] > l:
		ans = l
	elif l > tots[0][0]:
		ans = tots[0][0]
	else:
		ans = l - 1
	print(ans)


for case in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ha = {}
	cnt = 0
	for x in a:
		if not x in ha.keys():
			cnt += 1
			ha[x] = 1
		else:
			ha[x] += 1
	ans = 0
	for k in ha.values():
		if cnt > k:
			ans = max(ans, k)
		else:
			ans = max(ans, min(cnt, k - 1))
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ndis = len(set(l))
	d = Counter(l)
	nsame = max(d.values())
	if nsame == ndis:
		print(nsame - 1)
	else:
		print(min(nsame, ndis))


import sys
from collections import Counter
input = sys.stdin.buffer.readline
for _ in [0] * int(input()):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	l = len(c)
	r = max(c.values())
	if l == r:
		r -= 1
	print(min(l, r))


import sys
input = sys.stdin.readline
t = int(input())
for r 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
	maxsize = n // 2
	maxcount = -1
	element = 0
	for i in d:
		if d[i] > maxcount:
			maxcount = d[i]
			element = i
	ans1 = min(maxsize, maxcount)
	d[element] -= ans1
	counter = 0
	for i in d:
		if d[i] > 0:
			counter += 1
	finalans = min(ans1, counter)
	d[element] += ans1 - 1
	counter2 = 0
	for i in d:
		if d[i] > 0:
			counter2 += 1
	finalans2 = min(ans1 - 1, counter2)
	print(max(finalans, finalans2))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	c = 0
	ans = 0
	max_now = 0
	unique = 0
	for i in a:
		if i not in d:
			d[i] = 1
			c += 1
			max_now = max(max_now, d[i])
		else:
			d[i] += 1
			max_now = max(max_now, d[i])
	for i in range(0, max_now):
		if c - 1 >= max_now - i > 0:
			ans = max(ans, max_now - i)
		elif c >= max_now - i - 1 > 0:
			ans = max(ans, max_now - i - 1)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().strip().split()]
	dic = {}
	for e in a:
		if e not in dic:
			dic[e] = 0
		dic[e] += 1
	mx = 0
	for d in dic:
		if dic[d] > mx:
			mx = dic[d]
			sm = d
	dis = len(dic) - 1
	if dis >= mx:
		res = mx
	elif dis + 1 <= mx - 1:
		res = dis + 1
	else:
		res = dis
	print(res)


t = int(input())
for i in range(t):
	count = {}
	l = int(input())
	s = list(map(int, input().split()))
	for j in s:
		if j in count.keys():
			count[j] += 1
		else:
			count[j] = 1
	num_distinct = len(count.keys())
	num_same = max(count.values())
	if num_distinct < num_same:
		print(num_distinct)
	elif num_distinct == num_same:
		print(num_distinct - 1)
	else:
		print(num_same)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	cnt = 0
	max = 0
	for i in range(n):
		if l[i] not in d:
			d[l[i]] = 1
			cnt += 1
		else:
			d[l[i]] += 1
		if d[l[i]] > max:
			max = d[l[i]]
	if max > cnt:
		print(cnt)
	elif max == cnt:
		print(max - 1)
	else:
		print(max)


import collections
T = int(input())
for t in range(T):
	n = int(input())
	s = list(map(int, input().split()))
	ds = len(set(s))
	ss = collections.Counter(s).most_common()[0][1]
	if ss >= ds:
		ss -= 1
	print(min(ss, ds))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		s = dict()
		for elem in a:
			s[elem] = 1 + s.get(elem, 0)
		one = 0
		maxx = 0
		for elem in s:
			s[elem] -= 1
			one += 1
			maxx = max(maxx, s[elem])
		if maxx >= one:
			print(one)
		else:
			print(min(maxx + 1, one - 1))


def main():
	t = int(input())
	while t > 0:
		n = int(input())
		l = list(map(int, input().split()))
		d = {}
		for x in sorted(l):
			if x not in d:
				d[x] = 1
			else:
				d[x] += 1
		max_val = 0
		val = -1
		for key in d.keys():
			if d[key] > max_val:
				max_val = d[key]
				val = key
		A = max_val * [val]
		B = []
		for key in d.keys():
			if key != val:
				B.append(key)
				d[key] -= 1
		if len(A) > len(B):
			if val not in B:
				B.append(val)
				A.pop()
		print(min(len(A), len(B)))
		t -= 1
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	x = n // 2
	d = {}
	for i in a:
		if i not in d:
			c += 1
			d[i] = 1
		else:
			d[i] += 1
	m = 0
	for i in d.keys():
		if m < d[i]:
			m = d[i]
	ans = min(m, c - 1)
	if ans < m - 1 and x >= ans + 1:
		ans += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = dict()
	for i in arr:
		d[i] = 0
	for i in arr:
		if i in d:
			d[i] += 1
	c1 = max(d.values())
	l = len(d)
	if n == 1:
		print(0)
	elif c1 == l:
		print(l - 1)
	else:
		print(min(c1, l))


from collections import defaultdict
from collections import OrderedDict
t = int(input())
for i in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	d = defaultdict(int)
	for i in li:
		d[i] += 1
	x = max(d.values())
	dup = list(OrderedDict.fromkeys(li))
	y = len(dup)
	if y == x:
		print(x - 1)
	elif y < x:
		print(y)
	else:
		print(x)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	d = dict()
	(m, o) = (0, 0)
	for x in arr:
		if x not in d.keys():
			d[x] = 1
			o += 1
		else:
			d[x] += 1
		m = max(m, d[x])
	res = max(min(o, m - 1), min(o - 1, m))
	print(res)


for i in range(int(input())):
	n = int(input())
	(l, dic) = ([int(num) for num in input().split()], {})
	for i in l:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	if max(dic.values()) == len(dic):
		print(len(dic) - 1)
	else:
		print(min(len(dic), max(dic.values())))


from collections import Counter
t = int(input())
for _ in range(0, t):
	n = int(input())
	aa = [int(i) for i in input().split()]
	c = Counter(aa)
	max_repeated = max(c.values())
	distinct = len(c)
	k1 = min(distinct, max_repeated - 1)
	k2 = min(distinct - 1, max_repeated)
	print(max(k1, k2))


T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	s = set()
	a = [0] * (n + 1)
	for num in arr:
		s.add(num)
		a[num] += 1
	u = len(s)
	m = max(a)
	if n <= 1:
		print(0)
	elif m <= 1:
		print(1)
	elif u > m:
		print(m)
	elif m == u:
		print(u - 1)
	else:
		print(u)


for _ in range(int(input())):
	(n, ans) = (int(input()), 0)
	arr = list(map(int, input().split()))
	if n == 1:
		print(ans)
	else:
		lo = list(set(arr))
		d = dict()
		for i in arr:
			if i not in d.keys():
				d[i] = 1
			else:
				d[i] += 1
		(rep, dis) = (max(d.values()), len(lo))
		if rep < dis:
			print(min(rep, n // 2))
		else:
			print(min(rep - 1, dis, n // 2))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	similar = 0
	largest_similar = 0
	different = 0
	previous = 0
	for ls in l:
		if previous == ls:
			similar += 1
		else:
			different += 1
			similar = 1
		if similar > largest_similar:
			largest_similar = similar
		previous = ls
	if largest_similar > different:
		print(different)
	elif different > largest_similar:
		print(largest_similar)
	else:
		print(largest_similar - 1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = [0] * (n + 1)
	u = 0
	for j in range(n):
		l1[l[j]] += 1
		if l1[l[j]] == 1:
			u += 1
	m = max(l1)
	print(max(min(m - 1, u), min(m, u - 1)))


t = int(input())
for ts in range(t):
	l = int(input())
	a = [int(i) for i in input().split()]
	f = {}
	for skill in a:
		f[skill] = f.get(skill, 0) + 1
	u = len(list(f.keys()))
	s = max(list(f.values()))
	if u == s:
		print(u - 1)
	else:
		print(min(u, s))


from sys import stdin
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, stdin.readline().split()))
	count = {}
	dict = {}
	for e in l:
		if e not in dict:
			dict[e] = 1
		else:
			dict[e] += 1
	a = 0
	for e in dict.values():
		if e > a:
			a = e
	if a <= len(dict) - 1:
		print(a)
	else:
		while True:
			a -= 1
			if a <= len(dict):
				print(a)
				break
			if a <= 0:
				print(0)
				break


for _ in range(int(input())):
	N = int(input())
	team = list(map(int, input().split()))
	dic = {}
	(kind, MAX) = (0, 0)
	for t in team:
		if t not in dic:
			kind += 1
			dic[t] = 1
		else:
			dic[t] += 1
		if dic[t] > MAX:
			MAX = dic[t]
	if kind == MAX:
		print(kind - 1)
	else:
		print(min(kind, MAX))


import collections
t = int(input())

def solve(n, a):
	ht = collections.defaultdict(int)
	for s in a:
		ht[s] += 1
	unique = len(ht)
	max_same = max(ht.values())
	return max(min(unique, max_same - 1), min(unique - 1, max_same))
for _ in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	soln = solve(n, arr)
	print(soln)


from sys import stdin, stdout
from collections import Counter

def write(_s):
	stdout.write(_s)

def writeint(_s):
	stdout.write(str(_s))

def writelist(_l):
	stdout.write(' '.join(_l))

def read():
	return stdin.readline().strip().split(' ')

def readint():
	return int(stdin.readline().strip())

def readints():
	return map(int, stdin.readline().strip().split(' '))
t = readint()
while t:
	n = readint()
	a = list(readints())
	counter = Counter(a)
	unique_skills = len(counter)
	skills = [(number, skill) for (skill, number) in counter.items()]
	x = 0
	for (num, s) in skills:
		if unique_skills > num:
			x = max(x, num)
		elif unique_skills == num:
			x = max(x, unique_skills - 1)
		else:
			x = max(x, unique_skills)
	writeint(x)
	write('\n')
	t -= 1


kl = int(input())
for l in range(kl):
	n = int(input())
	A = {}
	B = set()
	mx = 0
	for i in input().split():
		i = int(i)
		if i in B:
			A[i] += 1
		else:
			A[i] = 1
		mx = max(A[i], mx)
		B.add(i)
	rz = min(mx, len(B))
	if mx == len(B):
		print(rz - 1)
	else:
		print(rz)


import sys
import math
from collections import Counter
debug = 1
readln = sys.stdin.readline

def readint():
	return int(readln().strip())

def readints():
	return map(int, readln().split())

def readstr():
	return readln().strip()

def readstrs():
	return readln().split()

def dbg(*args):
	if debug:
		print(' '.join(map(str, args)))

def solve(n, A):
	if n == 1:
		return 0
	ct = Counter(A)
	diffs = len(ct)
	sames = ct.most_common()[0][1]
	if sames == 1:
		return 1
	return max(min(diffs, sames - 1), min(diffs - 1, sames))
t = readint()
for _ in range(t):
	n = readint()
	A = readints()
	print(solve(n, A))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	maxm = 0
	for j in l:
		if j in d.keys():
			d[j] += 1
		else:
			d[j] = 1
		maxm = max(maxm, d[j])
	unique = len(d.keys())
	size = max(min(maxm, unique - 1), min(maxm - 1, unique))
	print(size)


import math
x = int(input())
for _ in range(x):
	n = int(input())
	x = [int(el) for el in input().split()]
	dic = {}
	for i in x:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	f = len(dic)
	g = -1
	for y in dic:
		if dic[y] > g:
			g = dic[y]
	print(max(min(g, f - 1), min(g - 1, f)))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	d = len(c)
	b = 0
	for j in c:
		if b < c[j]:
			b = c[j]
	print(max(min(d - 1, b), min(b - 1, d)))


mod = 10 ** 9 + 7
import math
from bisect import bisect_left
from collections import Counter
from itertools import combinations

def ain():
	return map(int, input().split())

def fain():
	return map(float, input().split())

def mc2(s):
	return s * (s - 1) // 2 + s
for tc in range(int(input())):
	n = int(input())
	l = list(ain())
	c = Counter(l)
	dis = list()
	maxKey = max(c, key=c.get)
	val = c[maxKey]
	tot = len(c.keys())
	if val == tot:
		print(val - 1)
	elif val < tot:
		print(val)
	else:
		print(tot)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * n
	for j in range(n):
		cnt[a[j] - 1] += 1
	cnt_pos = 0
	cnt_max = 0
	for j in range(n):
		cnt_max = max(cnt[j], cnt_max)
		if cnt[j] > 0:
			cnt_pos += 1
	if cnt_max == 1:
		if cnt_pos == 1:
			print(0)
		else:
			print(1)
	elif cnt_max < cnt_pos:
		print(cnt_max)
	elif cnt_max == cnt_pos:
		print(cnt_pos - 1)
	else:
		print(cnt_pos)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	occ = Counter(x)
	m2 = occ.most_common(1)[0][1]
	m_player = occ.most_common(1)[0][0]
	x_set = set(x)
	x_set.remove(m_player)
	m1 = len(x_set)
	if n <= 1:
		print('0')
	elif m2 > m1:
		if m2 - m1 > 1:
			m1 += 1
			print(m1)
		else:
			print(m1)
	else:
		print(m2)


import sys
import heapq
import math

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, input().split())

def rlinput():
	return list(map(int, input().split()))

def YES(flag):
	if flag:
		return 'YES'
	return 'NO'

def main():
	n = iinput()
	q = rlinput()
	c = {}
	l = len(set(q))
	for i in q:
		c[i] = c.get(i, 0) + 1
	w = []
	for i in c.items():
		w.append(i[1])
	w = sorted(w)
	if l > w[-1]:
		print(w[-1])
	elif l < w[-1]:
		print(l)
	else:
		print(l - 1)
for i in range(iinput()):
	main()


import sys
import math
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	c = [0] * (n + 1)
	for i in range(n):
		c[a[i]] += 1
	d = 0
	for i in range(1, n + 1):
		if c[i] > 0:
			d += 1
	ans = 0
	for i in range(1, n + 1):
		if c[i] > 0:
			ans = max(ans, min(d - 1, c[i]), min(d, c[i] - 1))
	print(ans)


t = int(input())
for i in range(0, t):
	n = int(input())
	lst = list((int(num) for num in input().split()))[:n]
	count = {}
	max = 0
	for ele in lst:
		if ele in count:
			count[ele] += 1
		else:
			count[ele] = 1
		if max < count[ele]:
			max = count[ele]
	x = len(count)
	if max - x + 1 >= 2:
		print(x)
	elif max < x - 1:
		print(max)
	else:
		print(x - 1)


def get_moda_and_len(a):
	counter = 1
	curr_item = a[0]
	max_item = a[0]
	max_counter = 1
	for i in range(1, len(a)):
		if a[i] == curr_item:
			counter += 1
		else:
			if counter >= max_counter:
				max_counter = counter
				max_item = curr_item
			curr_item = a[i]
			counter = 1
		if i == len(a) - 1:
			if counter >= max_counter:
				max_counter = counter
				max_item = curr_item
	return (max_item, max_counter)

def solve(a):
	sorted_a = sorted(a)
	(moda, moda_len) = get_moda_and_len(sorted_a)
	nuniques = len(set(a))
	if moda_len >= nuniques - 1:
		if moda_len == nuniques - 1:
			print(moda_len)
		elif moda_len == nuniques:
			print(nuniques - 1)
		else:
			print(nuniques)
	else:
		print(moda_len)
t = int(input())
for i in range(t):
	n = int(input())
	input_s = list(map(lambda x: int(x), input().split(' ')))
	solve(input_s)


for _ in range(int(input())):
	_ = input()
	students = input().strip().split()
	(max_index, max_score) = (-1, -1)
	all_students = {}
	for x in students:
		s = int(x)
		if s not in all_students:
			all_students[s] = 1
			if max_index == -1:
				max_index = s
				max_score = 1
		else:
			all_students[s] += 1
			if all_students[s] > max_score:
				max_score = all_students[s]
				max_index = s
	distinc = len(all_students.keys())
	if distinc == 0 or max_score <= 0:
		print(0)
	elif distinc > max_score:
		print(max_score)
	elif distinc == max_score:
		print(max_score - 1)
	else:
		print(distinc)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	skills = set()
	count = dict()
	for i in range(n):
		skills.add(a[i])
		if a[i] in count.keys():
			count[a[i]] += 1
		else:
			count[a[i]] = 1
	mx = 0
	for e in skills:
		mx = max(count[e], mx)
	if mx == 1 and len(skills) > 1:
		print(1)
	elif len(skills) == 1 and n > 1:
		print(1)
	elif n == 1:
		print(0)
	elif len(skills) == mx:
		print(mx - 1)
	else:
		print(min(len(skills), mx))


from math import *
from collections import *
from operator import itemgetter
import bisect
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
I = lambda : list(map(int, input().split()))
chrIdx = lambda x: ord(x) - 96
idxChr = lambda x: chr(96 + x)
t = ii()
for _ in range(t):
	n = ii()
	a = iia()
	d = dict(Counter(a))
	x = max(d.values())
	y = len(d) - 1
	if x > y + 1:
		y += 1
	print(min(x, y))


for i in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		l = {}
		l[nums[0]] = 1
		mode = nums[0]
		maxi = 1
		for i in range(1, n):
			if nums[i] in l:
				l[nums[i]] += 1
				if l[nums[i]] > maxi:
					maxi = l[nums[i]]
					mode = nums[i]
			else:
				l[nums[i]] = 1
		if l[mode] - len(l) == 0:
			print(len(l) - 1)
		else:
			print(min(l[mode], len(l)))


from collections import defaultdict
tc = int(input())
while tc > 0:
	n = int(input())
	d = defaultdict(int)
	for a in input().split():
		d[a] += 1
	v = sorted(d.values())[-1]
	if len(d) == v:
		print(v - 1)
	else:
		print(min(v, len(d)))
	tc -= 1


from collections import Counter
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		n = II()
		aa = LI()
		ac = Counter(aa)
		c1 = len(ac)
		c2 = max(ac.values())
		if c1 == c2:
			print(c1 - 1)
		else:
			print(min(c1, c2))
main()


def solve(n, arr):
	arr.sort()
	d = {}
	for i in range(len(arr)):
		if arr[i] in d:
			d[arr[i]] += 1
		else:
			d[arr[i]] = 1
	diff = len(d)
	all_values = d.values()
	mx = max(all_values)
	print(max(min(mx - 1, diff), min(mx, diff - 1)))
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	solve(n, arr)


from collections import Counter as c
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	t = c(a)
	l = [[t[a], a] for a in t]
	l.sort(reverse=True)
	b = l[0][0]
	a = len(l) - 1
	print(max(min(a, b), min(a + 1, b - 1)))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	(m, c) = (0, 0)
	y = Counter(x)
	for (key, val) in y.items():
		if c < val:
			c = val
			m = key
	l = len(y)
	print(max(min(l - 1, c), min(l, c - 1)))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		if l[i] in d:
			d[l[i]] += 1
		else:
			d[l[i]] = 1
	mx = max(d.values())
	stsize = len(set(l)) - 1
	if mx - stsize > 1:
		print(min(stsize, mx) + 1)
	else:
		print(min(stsize, mx))


def interize(ln):
	for i in range(len(ln)):
		ln[i] = int(ln[i])
	return ln

def solve(skills):
	hm = {}
	for i in range(len(skills)):
		if skills[i] in hm:
			hm[skills[i]] += 1
		else:
			hm[skills[i]] = 1
	max_skills = -1
	for k in list(hm.keys()):
		if hm[k] > max_skills:
			max_skills = hm[k]
	num_skills = len(list(hm.keys()))
	if max_skills > num_skills:
		return num_skills
	if max_skills == num_skills:
		return num_skills - 1
	return max_skills
t = int(input())
for i in range(t):
	n = int(input())
	skills = interize(input().split())
	print(solve(skills))


import os
import sys
from math import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def array():
	return [int(i) for i in input().split()]
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = array()
	if n == 1:
		print(0)
		continue
	else:
		fr = {}
		for i in a:
			try:
				fr[i] += 1
			except:
				fr[i] = 1
		val = max(fr.values())
		dist = len(fr.keys()) - 1
		ans = min(dist + 1, val - 1)
		ans1 = min(dist, val)
		print(max(ans, ans1))


from collections import Counter
import sys
input = sys.stdin.readline
T = int(input())
for test in range(T):
	N = int(input())
	lst = list(map(int, input().split()))
	if len(set(lst)) == len(lst):
		if len(lst) > 1:
			print(1)
			continue
	countlst = Counter(lst).most_common(1)
	num = countlst[0][0]
	count = countlst[0][1]
	ans = 0
	if len(set(lst)) - 1 >= count:
		print(count)
	elif len(set(lst)) - 1 + 1 == count:
		print(len(set(lst)) - 1)
	elif len(set(lst)) - 1 + 2 <= count:
		print(len(set(lst)))


from sys import stdin, stdout
from collections import defaultdict, Counter
from functools import lru_cache
from math import gcd, floor, ceil

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

def iint():
	return int(stdin.readline().strip())

def istr():
	return stdin.readline().strip()
t = iint()
for _x in range(t):
	n = iint()
	arr = ilist()
	hmap = defaultdict(int)
	for x in arr:
		hmap[x] += 1
	maxk = -1
	maxv = -1
	c = 0
	for (k, v) in hmap.items():
		if v > maxv:
			maxv = v
			maxk = k
		c += 1
	if maxv == c - 1:
		print(maxv)
	elif maxv < c - 1:
		print(min(maxv, c - 1))
	else:
		print(min(maxv - 1, c))


import collections
t = int(input())
result = []
for _ in range(t):
	qty = int(input())
	students = list(map(int, input().split()))
	c = collections.Counter()
	for student in students:
		c[student] += 1
	max_qty = max(list(c.values()))
	unique = len(set(students)) - 1
	if max_qty > unique:
		if max_qty - 1 >= unique + 1:
			result.append(unique + 1)
		else:
			result.append(unique)
	else:
		result.append(max_qty)
for res in result:
	print(res)


from collections import defaultdict
from collections import Counter
from string import ascii_lowercase
import math
from sys import setrecursionlimit as srl
srl(2 * 10 ** 6)

def solve(n, a):
	mp = defaultdict(int)
	for x in a:
		mp[x] += 1
	mc = 0
	for (i, v) in mp.items():
		mc = max(mc, v)
	for x in range(n):
		if mc == x:
			if x <= len(mp) - 1:
				ans = x
		elif mc > x and x <= len(mp):
			ans = x
	return ans
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))
	t -= 1


from collections import *
for _ in range(int(input())):
	n = int(input())
	a = sorted(Counter(input().split()).values())
	m = len(a)
	ok = 0
	ng = n
	while ng - ok > 1:
		mid = ok + ng >> 1
		t = a[:]
		if t[-1] > mid and m >= mid or (t[-1] == mid and m > mid):
			ok = mid
		else:
			ng = mid
	print(ok)


t = int(input())
for i in range(t):
	n = int(input())
	temp = input().split()
	players = {}
	maxval = 0
	maxplay = 0
	for i in temp:
		if int(i) not in players.keys():
			players[int(i)] = 1
		else:
			players[int(i)] = players[int(i)] + 1
		if maxval < players[int(i)]:
			maxval = players[int(i)]
			maxplay = int(i)
	length = len(players) - 1
	if maxval - length >= 2:
		maxval -= 1
		length += 1
	val = min(length, maxval)
	print(str(val))


import collections
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	arr = collections.Counter(l)
	k = list(arr.values())
	print(max(min(len(arr) - 1, max(k)), min(max(k) - 1, len(arr))))


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
d = defaultdict(int)
x = 'abcdefghijklmnopqrstuvwxyz'
t = I()
for _ in range(t):
	n = I()
	d = defaultdict(int)
	v = defaultdict(int)
	l = L()
	w = set(l)
	m = 0
	for i in l:
		d[i] += 1
		m = max(m, d[i])
	print(max(min(len(w) - 1, m), min(len(w), m - 1)))


n = int(input())
skills = []
for _ in range(n):
	count = input()
	skills.append(list(map(int, input().split())))
s = dict({})
for skls in skills:
	s.clear()
	for sk in skls:
		if sk in s:
			s[sk] += 1
		else:
			s[sk] = 1
	n = len(s) - 1
	print(max(min(n + 1, max(s.values()) - 1), min(n, max(s.values()))))


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	A = list(map(int, stdin.readline().split()))
	d = {}
	distinct = []
	for i in range(n):
		d[A[i]] = d.get(A[i], 0) + 1
	x = len(d)
	y = max(d.values())
	if x == y:
		ans = y - 1
	else:
		ans = min(x, y)
	stdout.write(str(ans) + '\n')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = map(int, input().split())
	(cnt, m) = ([0] * (n + 1), 0)
	for x in arr:
		if cnt[x] == 0:
			m += 1
		cnt[x] += 1
	res = 0
	for (i, c) in enumerate(cnt):
		x = max(min(c - 1, m), min(c, m - 1))
		res = max(res, x)
	print(res)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = defaultdict(int)
	for i in arr:
		d[i] += 1
	ans = 0
	for i in d:
		ans = max(ans, d[i])
	ans2 = len(d) - 1
	if ans2 < ans:
		ans2 += 1
		ans -= 1
	print(min(ans, ans2))


for x in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	import collections
	data = collections.Counter(m)
	data_list = dict(data)
	max_value = max(list(data.values()))
	k = len(data_list)
	mode_val = [num for (num, freq) in data_list.items() if freq == max_value]
	if len(m) == 1:
		print('0')
	elif max_value > k:
		print(k)
	elif max_value == k:
		print(k - 1)
	else:
		print(max_value)


t = int(input())
for case in range(t):
	n = input()
	l = list(input().split())
	s = set(l)
	dis = len(s)
	compt = {}
	for c in s:
		compt[c] = 0
	for c in l:
		compt[c] += 1
	max_compt = max(compt.values())
	print(max(min(dis, max_compt - 1), min(dis - 1, max_compt)))


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	counter = dict()
	for i in range(n):
		ele = arr[i]
		if ele in counter:
			counter[ele] += 1
		else:
			counter[ele] = 1
	maxim = 0
	for i in counter:
		maxim = max(maxim, counter[i])
	ans = min(len(counter) - 1, maxim)
	if maxim > len(counter):
		ans += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	d = {}
	count = 0
	for i in l:
		if i not in d:
			d[i] = 1
			count += 1
		else:
			d[i] += 1
	d = {k: v for (k, v) in sorted(d.items(), key=lambda item: item[1], reverse=True)}
	max = list(d.keys())[0]
	if d[max] <= count - 1:
		print(d[max])
	elif d[max] - (count - 1) >= 2:
		print(count)
	else:
		print(count - 1)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	if n == 1:
		print(0)
	elif len(set(arr)) in [n, 1]:
		print(1)
	else:
		dct = Counter(arr)
		cnt = len(set(arr)) - 1
		maxi = max(dct.values())
		if cnt >= maxi:
			print(maxi)
		elif maxi - cnt >= 2:
			print(cnt + 1)
		else:
			print(cnt)


import bisect, math, collections

def foo(arr):
	counts = collections.Counter(arr)
	m = counts[max(counts.keys(), key=lambda x: counts[x])]
	s = len(set(arr)) - 1
	if s < m - 1:
		return s + 1
	if s > m:
		return m
	return s
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(foo(arr))


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def solve(n):
	return n * (n + 1) // 2
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	z = len(set(l))
	hash = defaultdict(int)
	maxi = 0
	ans = -1
	final = 0
	for i in l:
		hash[i] += 1
		if hash[i] > maxi:
			maxi = hash[i]
			ans = i
	print(max(min(z, hash[ans] - 1), min(z - 1, hash[ans])))


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	v = [0] * n
	o = 0
	m = 0
	for i in map(int, input().split()):
		v[i - 1] += 1
	for i in v:
		if i != 0:
			o += 1
		if i > m:
			m = i
	if m > 1 and o - 1 < m:
		m -= 1
	print(min(o, m) if n != 1 else 0)


from collections import Counter
for _ in range(int(input())):
	n = input()
	l = list(map(int, input().split()))
	d = Counter(l)
	pair = sorted(d.items(), key=lambda x: -x[1])
	max_count = pair[0][1]
	max_size = len(pair) - 1
	valid = min(max_count, max_size)
	if max_size + 1 <= max_count - 1:
		valid = max(valid, max_size + 1)
	print(valid)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	dic = {}
	for i in range(n):
		if l[i] not in dic:
			dic[l[i]] = 0
		dic[l[i]] += 1
	maxi = max(list(dic.values()))
	length = len(list(dic.values()))
	if length == maxi:
		print(maxi - 1)
	else:
		print(min(maxi, length))


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	x = len(set(ar))
	ar1 = [0] * (n + 1)
	for i in ar:
		ar1[i] += 1
	if x == max(ar1):
		print(x - 1)
	else:
		print(min(x, max(ar1)))


for _ in range(int(input())):
	count = [0] * (int(input()) + 1)
	a = list(map(int, input().split()))
	b = set(a)
	repeat = 0
	num = 0
	for x in a:
		count[x] += 1
	repeat = max(count)
	num = len(b)
	print(num - 1 if num == repeat else min(repeat, num))


import sys, bisect
from collections import defaultdict
input = sys.stdin.readline

def inp():
	return int(input().rstrip())

def inlt():
	return list(map(int, input().rstrip().split()))

def insr():
	s = input().rstrip()
	return s[:len(s) - 1]

def invr():
	return map(int, input().rstrip().split())
t = inp()
for _ in range(t):
	n = inp()
	nums = inlt()
	num_dict = defaultdict(int)
	for i in nums:
		num_dict[i] += 1
	ans = 0
	lng = len(num_dict)
	for i in num_dict:
		a = 0
		b = 0
		if num_dict[i] < lng:
			a = num_dict[i]
		elif num_dict[i] == lng:
			a = num_dict[i] - 1
		else:
			a = lng
		ans = max(ans, a)
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = Counter(l)
	un = len(c)
	ma = c.most_common(1)[0][1]
	if un == ma:
		print(ma - 1)
	else:
		print(min(ma, un))


for _ in range(int(input())):
	d = {}
	input()
	l = list(map(int, input().split()))
	for k in l:
		d[k] = d.get(k, 0) + 1
	a = max(d.values())
	b = len(d.keys())
	if a != b:
		print(min(a, b))
	else:
		print(a - 1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	if len(arr) == len(set(arr)):
		print(1)
		continue
	d = {}
	for i in arr:
		try:
			d[i] += 1
		except:
			d[i] = 1
	maxfr = max(d.values())
	uniq = len(set(arr))
	if uniq != maxfr:
		print(min(uniq, maxfr))
	else:
		print(uniq - 1)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	d = {}
	maxVal = 0
	for x in a:
		if x not in d:
			d[x] = 0
		d[x] += 1
		maxVal = max(maxVal, d[x])
	totalDistinct = len(d)
	print(max(min(maxVal, totalDistinct - 1), min(maxVal - 1, totalDistinct)))


def main():
	for _ in inputt():
		(n,) = inputi()
		A = inputl()
		A = Counter(A)
		(a, b) = (len(A), max(A.values()))
		print(min(a, b) - (a == b))
from math import *
from heapq import *
from itertools import *
from functools import reduce, lru_cache, partial
from collections import Counter, defaultdict, deque
import re, copy, operator, cmath, string
import calendar
import sys, io, os, builtins
sys.setrecursionlimit(1000)
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(io.IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	sys.stdout.write(kwargs.pop('split', ' ').join(map(str, args)))
	sys.stdout.write(kwargs.pop('end', '\n'))
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip()
inputt = lambda t=0: range(t) if t else range(int(input()))
inputs = lambda : input().split()
inputi = lambda k=int: map(k, inputs())
inputf = lambda : inputi(float)
inputl = lambda t=0, k=inputi: map(list, (k() for _ in range(t))) if t else list(k())

def len(a):
	if isinstance(a, range):
		return -((a.start - a.stop) // a.step)
	return builtins.len(a)

def bisect_left(a, x, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	while lo < hi:
		mid = (lo + hi) // 2
		if key(a[mid]) < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def bisect_right(a, x, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	while lo < hi:
		mid = (lo + hi) // 2
		if x < key(a[mid]):
			hi = mid
		else:
			lo = mid + 1
	return lo

def insort_left(a, x, key=None, lo=0, hi=None):
	lo = bisect_left(a, x, key, lo, hi)
	a.insert(lo, x)

def insort_right(a, x, key=None, lo=0, hi=None):
	lo = bisect_right(a, x, key, lo, hi)
	a.insert(lo, x)
do_nothing = lambda x: x
bisect = bisect_right
insort = insort_right

def index(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	i = bisect_left(a, x, key, lo, hi)
	if lo <= i < hi and key(a[i]) == x:
		return a[i]
	if default != None:
		return default
	raise ValueError

def find_lt(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_left(a, x, key, lo, hi)
	if lo < i <= hi:
		return a[i - 1]
	if default != None:
		return default
	raise ValueError

def find_le(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_right(a, x, key, lo, hi)
	if lo < i <= hi:
		return a[i - 1]
	if default != None:
		return default
	raise ValueError

def find_gt(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_right(a, x, key, lo, hi)
	if lo <= i < hi:
		return a[i]
	if default != None:
		return default
	raise ValueError

def find_ge(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_left(a, x, key, lo, hi)
	if lo <= i < hi:
		return a[i]
	if default != None:
		return default
	raise ValueError
main()


for i in range(int(input())):
	a = {}
	x = int(input())
	y = [int(i) for i in input().split()]
	z = set(y)
	max1 = 1
	for i in y:
		if i not in a:
			a[i] = 1
		else:
			a[i] += 1
			if max1 < a[i]:
				max1 = a[i]
	k = len(z)
	if max1 == k:
		print(max1 - 1)
	else:
		print(min(max1, k))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	counts = sorted(list(dict(Counter(arr)).items()), key=lambda x: x[1])
	size = len(counts)
	if size <= 1:
		if counts[0][1] >= 2:
			print(1)
		else:
			print(0)
		continue
	ans = 0
	ans = min(size - 1, counts[-1][1])
	if counts[-1][1] > size:
		ans += 1
	print(ans)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	l = list(map(int, sys.stdin.readline().split()))
	a = [0] * (n + 1)
	for i in range(n):
		a[l[i]] += 1
	diff = 0
	maxx = max(a)
	for i in range(n + 1):
		if a[i] > 0:
			diff += 1
	diff -= 1
	if maxx - diff > 1:
		diff += 1
	print(min(maxx, diff))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnts = Counter(a)
	curr_max = max(cnts.values())
	set_len = len(cnts.keys())
	print(max(min(set_len - 1, curr_max), min(set_len, curr_max - 1)))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	s = len(set(a))
	d = Counter(a)
	mx = 0
	for x in d:
		if d[x] > mx:
			mx = d[x]
	if s <= mx - 1:
		print(s)
	else:
		print(min(s - 1, mx))


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	f = {}
	for i in l:
		if i in f:
			f[i] = f[i] + 1
		else:
			f[i] = 1
	m = max(f.keys(), key=lambda k: f[k])
	a = set(l)
	p = len(a)
	if f[m] > p:
		print(p)
	elif f[m] == p:
		print(p - 1)
	else:
		print(f[m])


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	slen = len(s)
	d = {}
	mxc = a[0]
	mx = 1
	for i in a:
		d[i] = d.get(i, 0) + 1
		mx = max(d[i], mx)
	if slen > mx:
		ans = mx
	elif slen == mx:
		ans = slen - 1
	else:
		ans = slen
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	freq = Counter(a)
	b = -1
	f = 0
	for i in freq:
		if freq[i] > f:
			b = i
			f = freq[i]
	if len(freq) > f:
		print(f)
	else:
		print(min(len(freq), f - 1))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	num = list(map(int, input().split()))
	uniq = len(list(set(num)))
	same = max(Counter(num).values())
	if same == uniq:
		print(same - 1)
	elif same > uniq:
		print(uniq)
	elif same < uniq:
		print(same)


for mdaaaaaa in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	Max = 0
	x = 0
	d = dict()
	s = set()
	for i in range(n):
		s.add(a[i])
		if d.get(a[i]) == None:
			d[a[i]] = 1
		else:
			d[a[i]] += 1
		if d[a[i]] > Max:
			Max = d[a[i]]
			x = a[i]
	print(max(min(len(s) - 1, Max), min(len(s), Max - 1)))


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()))
	d = dd(int)
	for i in range(n):
		d[l[i]] += 1
	a = len(list(d.keys())) - 1
	b = max(list(d.values()))
	if a >= b:
		print(b)
	else:
		print(min(a + 1, b - 1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	d = {}
	for i in a:
		if i not in d:
			d[i] = 0
		d[i] += 1
	mx = -1
	pos = -1
	for j in d:
		if d[j] > mx:
			mx = d[j]
			pos = j
	if mx <= len(d) - 1:
		print(mx)
	else:
		ans = len(d) if mx - 1 >= len(d) else len(d) - 1
		print(ans)


import sys
import string
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq
import operator

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	n = ri()
	a = rl()
	d = dd(int)
	for ai in a:
		d[ai] += 1
	often = max(d.items(), key=operator.itemgetter(1))[0]
	if d[often] > 1:
		all_often = min(d[often], len(d) - 1)
		all_often_minus_1 = min(d[often] - 1, len(d))
		print(max(all_often, all_often_minus_1))
	elif n > 1:
		print('1')
	elif n == 1:
		print('0')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set()
	cnt = {}
	for x in a:
		s.add(x)
		if x not in cnt:
			cnt[x] = 0
		cnt[x] += 1
	mx = 0
	for x in s:
		mx = max(mx, min(len(s) - 1, cnt[x]))
		if cnt[x] - 1 >= len(s):
			mx = len(s)
	print(mx)


from collections import defaultdict
for _ in range(int(input())):
	input()
	a = map(int, input().split())
	d = defaultdict(int)
	for aa in a:
		d[aa] += 1
	maxim = max(d.values())
	print(max(min(maxim, len(d) - 1), min(maxim - 1, len(d))))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = Counter(l)
	m = max(list(d.values()))
	dis = len(Counter(l))
	if n == 1:
		print(0)
		continue
	elif n == 2 or dis == n:
		print(1)
		continue
	dis = dis - 1
	if dis >= m:
		print(m)
	elif dis < m:
		if m - dis == 1:
			print(dis)
		else:
			print(dis + 1)


T = int(input())
for t in range(T):
	N = int(input())
	arr = input().split()
	d = {}
	for el in arr:
		if el in d:
			d[el] += 1
		else:
			d[el] = 1
	distinct_el = len(d)
	max_freq = max(d.values())
	ans = 0
	if len(arr) == 1:
		ans = 0
	elif max_freq == distinct_el:
		ans = max_freq - 1
	else:
		ans = min(distinct_el, max_freq)
	print(ans)


for z in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	dir = {}
	i = 0
	while i < n:
		dir[a[i]] = 1
		i += 1
		while i < n and a[i] == a[i - 1]:
			dir[a[i]] += 1
			i += 1
	res = []
	n = len(dir)
	for ii in dir:
		res.append(max(min(n - 1, dir[ii]), min(n, dir[ii] - 1)))
	print(max(res))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	occ = max(Counter(l).values())
	dis = len(Counter(l))
	if n < 2:
		print(0)
	elif dis > occ:
		print(max(min(dis, occ), 1))
	else:
		print(max(min(dis, occ - 1), 1))


import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dis = set(arr)
	dic = {}
	for (i, val) in enumerate(arr):
		dic[val] = dic.get(val, 0) + 1
	dis_count = len(dis)
	maxx = 0
	for (key, value) in dic.items():
		maxx = max(maxx, value)
	if maxx == dis_count:
		print(maxx - 1)
	elif maxx > dis_count:
		print(dis_count)
	elif dis_count > maxx:
		print(maxx)


import math as mt

def mostFrequent(arr, n):
	Hash = dict()
	for i in range(n):
		if arr[i] in Hash.keys():
			Hash[arr[i]] += 1
		else:
			Hash[arr[i]] = 1
	max_count = 0
	res = -1
	for i in Hash:
		if max_count < Hash[i]:
			res = i
			max_count = Hash[i]
	return res
t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	m = {}
	g = 0
	k = 0
	z = 0
	su = 0
	g = mostFrequent(b, n)
	g = b.count(g)
	b = len(set(b))
	if b - 1 > g:
		print(g)
	elif b - 1 < g - 1:
		print(b)
	else:
		print(b - 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	skills = {}
	for x in a:
		if x in skills:
			skills[x] += 1
		else:
			skills[x] = 1
	counts = skills.values()
	m = 0
	for x in counts:
		if x > len(counts):
			m = max(m, len(counts))
		else:
			m = max(m, min(x, len(counts) - 1))
	print(m)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cal = [0 for i in range(n + 1)]
	uniq = 0
	for i in arr:
		if cal[i] == 0:
			uniq += 1
		cal[i] += 1
	max_c = max(cal)
	if uniq == max_c:
		print(uniq - 1)
	else:
		print(min(uniq, max_c, n // 2))


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	for i in a:
		try:
			dic[i] += 1
		except:
			dic[i] = 1
	m = 0
	ans = 0
	for i in dic:
		if dic[i] - 1 >= len(dic):
			m = len(dic)
		else:
			m = min(len(dic) - 1, dic[i])
		if ans < m:
			ans = m
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	A = []
	B = [0] * (n + 1)
	r = 0
	k = 0
	A = list(map(int, input().split()))
	for i in range(len(A)):
		B[int(A[i])] += 1
	if n < 2:
		print(0)
	else:
		for i in B:
			if i != 0:
				r += 1
		k = max(B)
		print(max(min(r, k - 1), min(r - 1, k)))


import math as mt

def mostFrequent(arr, n):
	Hash = dict()
	for i in range(n):
		if arr[i] in Hash.keys():
			Hash[arr[i]] += 1
		else:
			Hash[arr[i]] = 1
	max_count = 0
	res = -1
	for i in Hash:
		if max_count < Hash[i]:
			res = i
			max_count = Hash[i]
	return max_count
t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	s = set()
	for i in l:
		s.add(i)
	c = len(s)
	m = mostFrequent(l, n)
	if m >= c + 1:
		print(c)
	else:
		for i in range(c - 1, -1, -1):
			if i <= m:
				print(i)
				break


import math
from collections import Counter

def solve():
	n = int(input())
	l1 = list(map(int, input().split()))
	d = Counter(l1)
	if n == 1:
		print(0)
	elif len(d) == n:
		print(1)
	else:
		g = d.most_common(1)[0][0]
		v = d[g]
		j = len(d) - 1
		if v == j:
			print(v)
		elif abs(v - j) == 1:
			print(min(v, j))
		elif j >= v:
			print(v)
		else:
			print(j + 1)
for _ in range(int(input())):
	solve()


import sys
num_problems = None
num_members = None
for line in sys.stdin:
	if num_problems is None:
		num_problems = int(line)
	elif num_members is None:
		num_members = int(line)
	else:
		max_val = 0
		num_diff = 0
		member_dict = {}
		for m in line.strip().split(' '):
			val = int(m)
			if val not in member_dict:
				member_dict[val] = 0
				num_diff += 1
			member_dict[val] += 1
			val = member_dict[val]
			if max_val < val:
				max_val = val
		if num_diff > max_val:
			print(max_val)
		elif num_diff < max_val:
			print(num_diff)
		else:
			print(max_val - 1)
		num_members = None
		num_problems -= 1
		if num_problems == 0:
			break


from heapq import *
from collections import *
import os
import sys
from io import BytesIO, IOBase
import math
ins = lambda : [int(x) for x in input()]
inp = lambda : int(input())
inps = lambda : [int(x) for x in input().split()]

def main():
	t = inp()
	for i in range(t):
		n = inp()
		a = inps()
		dist = len(set(a))
		if dist == 1:
			if n <= 1:
				print(0)
			else:
				print(1)
			continue
		dc = Counter(a)
		for (k, v) in dc.most_common():
			if v == 1:
				if n > 1:
					print(1)
				else:
					print(0)
			elif v == dist:
				print(v - 1)
			elif v > dist:
				print(dist)
			else:
				print(v)
			break
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	for i in range(n):
		d[l[i]] += 1
	l = []
	for key in d.keys():
		l.append(d[key])
	m = max(l)
	k = len(d.keys()) - 1
	if k < m - 1:
		print(k + 1)
	elif k == m - 1:
		print(k)
	else:
		print(m)


from collections import Counter
t = int(input())
for j in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	C = Counter(L)
	distinct = 0
	maxoccurence = 0
	for elt in C:
		distinct += 1
		maxoccurence = max(maxoccurence, C[elt])
	distinct -= 1
	if distinct < maxoccurence - 1:
		print(distinct + 1)
	else:
		print(min(maxoccurence, distinct))


for _ in range(int(input())):
	dic = {}
	n = int(input())
	arr = list(map(int, input().split()))
	if len(arr) < 2:
		print(0)
	else:
		unique = len(set(arr))
		for i in range(len(arr)):
			if arr[i] in dic:
				dic[arr[i]] += 1
			else:
				dic[arr[i]] = 1
		aux1 = min(max(dic.values()), unique - 1)
		aux2 = min(max(dic.values()) - 1, unique)
		print(max(aux1, aux2))


from collections import Counter
a = int(input())
for x in range(a):
	b = input()
	b = [int(x) for x in input().split(' ')]
	c = Counter(b).most_common(1)[0][1]
	d = set(b)
	print(max(min(len(d) - 1, c), min(c - 1, len(d))))


from collections import Counter

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		fisrt_command = len(set(a))
		second_command = Counter(a).most_common()[0][1]
		if fisrt_command == second_command:
			print(fisrt_command - 1)
		elif fisrt_command > second_command:
			print(second_command)
		elif fisrt_command < second_command:
			print(fisrt_command)
main()


import collections
import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = int(input())
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	n = inp()
	a = inlt()
	counter = collections.Counter(a)
	(key, value) = counter.most_common(1)[0]
	item_count = len(counter)
	print(max(min(item_count - 1, value), min(item_count, value - 1)))


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	c = dict()
	for i in range(n):
		if a[i] in c:
			c[a[i]] += 1
		else:
			c[a[i]] = 1
	m = len(c)
	r = m - 1
	ans = 0
	for (k, v) in c.items():
		if v <= r:
			ans = max(ans, v)
		elif v == r + 1:
			ans = max(ans, r)
		elif v >= r + 2:
			ans = max(ans, m)
	print(ans)


from collections import Counter
for aaa in range(int(input())):
	n = int(input())
	d = Counter(map(int, input().split()))
	l = len(d)
	m = 0
	for value in d.values():
		m = max(m, min(l - 1, value), min(l, value - 1))
	print(m)


from collections import deque
import math
for i in range(int(input())):
	n = int(input())
	array = input().split()
	hashMap = dict()
	for i in range(len(array)):
		array[i] = int(array[i])
		if array[i] in hashMap:
			hashMap[array[i]] = hashMap[array[i]] + 1
		else:
			hashMap[array[i]] = 1
	maxCount = 1
	diference = len(hashMap)
	for i in hashMap:
		maxCount = max(hashMap[i], maxCount)
	if diference == maxCount:
		print(maxCount - 1)
	elif maxCount > diference:
		print(diference)
	else:
		print(maxCount)


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	ntest = int(stdin.readline())
	for test in range(ntest):
		n = int(stdin.readline())
		a = [int(x) for x in stdin.readline().split()]
		c = {}
		for x in a:
			c[x] = c.get(x, 0) + 1
		b = list(c.values())
		b.sort()
		m = len(b)
		top = b[-1]
		res = min(m, top - 1)
		if top > m:
			res = min(top, m)
		elif top < m:
			res = top
		stdout.write('%d\n' % res)
	return 0
main()


from collections import Counter
t = int(input())
while t > 0:
	b = int(input())
	c = map(int, input().split())
	d = Counter(c)
	l1 = min(max(d.values()), len(d) - 1)
	l2 = min(max(d.values()) - 1, len(d))
	print(max(l1, l2))
	t -= 1


def tts(arr, n):
	if n < 2:
		return 0
	dic = {}
	for k in arr:
		if k not in dic:
			dic[k] = 1
		else:
			dic[k] += 1
	if len(dic) == n or len(dic) == 1:
		return 1
	ma = max(dic.values())
	m = ma
	k = len(dic) - 1 + int(m != ma)
	while k < m:
		m -= 1
		k = len(dic) - 1 + int(m != ma)
	return m
ts = int(input())
for t in range(ts):
	n = int(input())
	arr = list(map(int, input().split()))
	res = tts(arr, n)
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2 or n == 3:
		print(1)
	else:
		l.sort()
		a = []
		i = 0
		while i < n:
			c = 0
			j = l[i]
			while l[i] == j:
				c = c + 1
				i = i + 1
				if i == n:
					break
			a.append(c)
		d = 0
		s = []
		for i in range(len(a)):
			if a[i] == 1:
				d = d + 1
			else:
				s.append(a[i])
		if len(s) == 0:
			print(1)
		else:
			if len(s) > 1:
				d = d + len(s) - 1
			m = max(s)
			if m - d >= 2:
				print(d + 1)
			elif m < d:
				print(m)
			else:
				print(d)


def max_size(size):
	b = -1
	k = None
	for (key, value) in zip(c.keys(), c.values()):
		if value > b:
			b = value
			k = key
	if k in s and b == size:
		ls = len(s) - 1
	else:
		ls = len(s)
	if size > b or size > ls:
		return -1
	return min(size, ls)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	c = {}
	for v in a:
		if v not in c:
			c[v] = 1
		else:
			c[v] += 1
	k = -1
	for j in range(1, n):
		j = n // 2 ** j
		if j >= n or j < 1:
			break
		while max_size(k + j) < max_size(k + j + 1):
			k += j
	print(k + 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = list(set(a))
	s = len(s)
	l = []
	d = {}
	for i in range(0, n):
		if a[i] in d:
			d[a[i]] = d[a[i]] + 1
		else:
			d[a[i]] = 1
	m = max(d.values())
	if m == s:
		ans = m - 1
	else:
		ans = min(m, s)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in a:
		try:
			d[i] += 1
		except KeyError:
			d[i] = 1
	ld = len(d)
	ans = -1
	for i in d:
		if d[i] == 1:
			ans = max(ans, min(d[i], ld - 1))
		else:
			ans = max(ans, max(min(ld - 1, d[i]), min(ld, d[i] - 1)))
	print(ans)


a = int(input())
for _ in range(a):
	element_num = int(input())
	lis = list(map(int, input().split()))
	lol = len(set(lis))
	dic = {}
	for i in lis:
		if i not in dic:
			dic[i] = 1
		else:
			dic[i] += 1
	t = max(dic.values())
	if lol == t:
		print(t - 1)
	else:
		print(min(lol, t))


for t in range(int(input())):
	n = int(input())
	dct = {}
	lst = list(map(int, input().split()))
	for key in lst:
		dct[key] = dct.get(key, 0) + 1
	print(min(len(dct.keys()), max(dct.values())) - (len(dct.keys()) == max(dct.values())))


def teamy(a):
	d = {}
	M = 0
	unique = set()
	for skill in a:
		if skill in d:
			d[skill] += 1
		else:
			d[skill] = 1
		M = max(M, d[skill])
		unique.add(skill)
	t1 = len(unique) - 1
	t2 = M
	if t2 > t1:
		diff = t2 - t1
		if diff >= 2:
			return t1 + 1
		return t1
	elif t2 < t1:
		return t2
	else:
		return t1

def main():
	t = int(input())
	for i in range(t):
		n = input()
		a = list(map(int, input().split(' ')))
		print(teamy(a))
main()


t = int(input())
s1 = 'abcdefghijklmnopqrstuvwxyz'
for i in range(t):
	f = False
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	b = []
	lmax = 0
	for j in range(n):
		if b == [] or a[j] == b[-1]:
			b.append(a[j])
		else:
			if len(b) > lmax:
				lmax = len(b)
			b = [a[j]]
	if len(b) > lmax:
		lmax = len(b)
	if n == 1:
		print(0)
	elif lmax == 1:
		print(1)
	else:
		u = len(set(a))
		j = 0
		if lmax == u:
			print(min(lmax - 1, u - 1))
		else:
			print(min(lmax, u))


t = int(input())
for _ in range(t):
	n = int(input())
	points = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if d.get(points[i]):
			d[points[i]] += 1
		else:
			d[points[i]] = 1
	m = n // 2
	max_rep = max(d.values())
	distincts = len(d.keys())
	if distincts > max_rep:
		print(max_rep)
	elif distincts == max_rep:
		print(distincts - 1)
	else:
		print(distincts)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	d = {}
	m = 0
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
		if d[i] > m:
			m = d[i]
	u = len(d.keys())
	if m == u:
		print(m - 1)
	else:
		print(min(m, u))


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	(team1, team2) = (len(set(s)), 0)
	if len(s) == 1:
		print(0)
		continue
	count = [0] * (len(s) + 5)
	for num in s:
		count[num] += 1
	max_count = max(count)
	if max_count == 1:
		print(1)
		continue
	team1 -= 1
	team2 = max_count
	if team1 == team2:
		print(team1)
	elif team1 > team2:
		print(team2)
	elif team1 < team2:
		if team2 > team1 + 1:
			print(team1 + 1)
		else:
			print(team1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * (n + 1)
	for i in range(n):
		cnt[a[i]] += 1
	diff = [0]
	c = 0
	for i in range(n + 1):
		if cnt[i] != 0:
			c += 1
	mx = max(cnt)
	c -= 1
	if mx - c > 1:
		c += 1
	print(min(mx, c))


T = int(input())
for _ in range(0, T):
	n = int(input())
	s = [int(x) for x in input().split()]
	pos = [0] * (n + 5)
	for i in range(0, len(s)):
		pos[s[i]] += 1
	mx1 = 0
	mx2 = 0
	for i in range(1, n + 1):
		mx1 = max(mx1, pos[i])
		if pos[i] > 0:
			mx2 += 1
	if mx1 == mx2:
		print(mx1 - 1)
	else:
		print(min(mx1, mx2))


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_int_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(get_int()):
	n = get_int()
	lst = get_int_list()
	d = {}
	for i in lst:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	m = max(d.values())
	mx = 0
	for (i, v) in d.items():
		if v == m:
			mx = i
			break
	l = len(set(lst))
	if l - 1 == m:
		print(m)
	elif l - 1 < m:
		print(min(l, m - 1))
	else:
		print(m)


def main_function():
	from sys import stdin
	from sys import stdout
	input = stdin.readline
	from collections import Counter
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		max_x_first_party = len(set(a)) - 1
		max_x_second_party = Counter(a).most_common(1)[0][1]
		stdout.write(str(max(min(max_x_first_party, max_x_second_party), min(max_x_first_party + 1, max_x_second_party - 1))) + '\n')
main_function()


import sys
input = sys.stdin.readline

def main():
	q = int(input())
	for _ in range(q):
		n = int(input())
		tab = list(map(int, input().split()))
		dic = {}
		for x in range(n):
			if tab[x] in dic:
				dic[tab[x]] += 1
			else:
				dic[tab[x]] = 1
		tab = list(dic.values())
		tab.sort()
		tab = tab[::-1]
		m = 0
		for x in range(len(tab)):
			if tab[x] > len(tab) - (x + 1):
				m = max(m, min(len(tab) - (x + 1) + 1, tab[x] - 1))
			else:
				m = max(m, min(len(tab) - (x + 1), tab[x]))
		print(m)
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = [0]
	for i in range(n):
		l.append(0)
	for i in range(n):
		l[a[i]] += 1
	m = 0
	c = 0
	for i in range(n + 1):
		if l[i] > 0:
			c += 1
		if m < l[i]:
			m = l[i]
	c -= 1
	if m <= c:
		print(m)
	elif m - c >= 2:
		print(c + 1)
	else:
		print(c)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	a = sorted(a)
	uniq = 1
	maxcount = 1
	count = 0
	prev = a[0]
	for j in a:
		if j != prev:
			if count > maxcount:
				maxcount = count
			uniq += 1
			count = 1
			prev = j
		else:
			count += 1
	if count > maxcount:
		maxcount = count
	end1 = maxcount
	end2 = uniq - 1
	if maxcount > uniq:
		end1 -= 1
		end2 += 1
	print(min([end1, end2]))


from collections import Counter
n = int(input())
for i in range(n):
	num = input()
	lst = input().split()
	num_distinct = len(set(lst))
	m = int(max(list(map(int, dict(Counter(lst)).values()))))
	if num_distinct - m > 0:
		print(m)
	elif m - num_distinct > 0:
		print(num_distinct)
	else:
		print(m - 1)


from collections import Counter
t = int(input())
for _ in range(t):
	(n, skills) = (int(input()), list(map(int, input().split())))
	c = Counter(skills)
	(maxi, l_remain) = (max(c.values()), len(set(skills)) - 1)
	if l_remain >= maxi:
		print(maxi)
	elif maxi - l_remain == 1:
		print(l_remain)
	else:
		print(l_remain + 1)


T = int(input())
for t in range(T):
	length = int(input())
	Skills = list(map(int, input().split()))
	d = {}
	for s in Skills:
		if s in d:
			d[s] += 1
		else:
			d[s] = 1
	Key = 0
	Val = 0
	d_len = len(d)
	for (k, v) in d.items():
		if v > Val:
			Key = k
			Val = v
	if d_len == Val:
		print(Val - 1)
	elif d_len > Val:
		print(Val)
	else:
		print(d_len)


for i in range(int(input())):
	(n, a, stud, maxt) = (int(input()), input().split(), {}, 0)
	for i in range(n):
		stud[a[i]] = 1 if a[i] not in stud else stud[a[i]] + 1
		maxt = stud[a[i]] if stud[a[i]] > maxt else maxt
	print(max(min(maxt, len(stud) - 1), min(maxt - 1, len(stud))))


import math
testCases = int(input())
for _ in range(testCases):
	n = int(input())
	skills = dict()
	for skill in [int(x) for x in input().split()]:
		skills[skill] = skills.get(skill, 0) + 1
	uniqueSkills = len(skills)
	repeataionOfSkills = skills.values()
	maxRepeatedSkillCount = max(repeataionOfSkills)
	print(min(uniqueSkills, max(repeataionOfSkills)) - (uniqueSkills == maxRepeatedSkillCount))


import sys
input = sys.stdin.readline
from collections import *
from math import *
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	f = Counter(a)
	f1 = 0
	se = 0
	for i in f.keys():
		if f[i] == 1:
			f1 += 1
		else:
			se = max(se, f[i])
	if se == 0:
		print(1)
		continue
	if se <= f1:
		print(se)
	elif len(f) > se:
		print(se)
	elif len(f) == se:
		print(se - 1)
	else:
		print(len(f))


from sys import stdin, stdout
from math import *
for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	d = {}
	if n == 1:
		print(0)
		continue
	for i in range(n):
		if d.get(ar[i]) == None:
			d[ar[i]] = 1
		else:
			d[ar[i]] += 1
	k = list(d.keys())
	v = list(d.values())
	dis = len(k)
	same = 0
	for i in range(len(k)):
		same = max(same, v[i])
	if dis == 1 or same == 0:
		print(1)
	elif dis == same:
		print(dis - 1)
	elif dis > same:
		print(same)
	else:
		print(dis)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	dist = len(d)
	ans = 0
	for i in d:
		if d[i] > dist:
			ans = max(ans, dist)
		elif d[i] < dist:
			ans = max(ans, d[i])
		else:
			ans = max(ans, d[i] - 1)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dic = dict()
	d_max = 0
	raz = 0
	if n == 1:
		print(0)
	elif n == 2:
		print(1)
	else:
		for num in a:
			if num not in dic:
				dic[num] = 1
				raz += 1
				if dic[num] > d_max:
					d_max = dic[num]
			else:
				dic[num] += 1
				if dic[num] > d_max:
					d_max = dic[num]
		if d_max == 1:
			print(1)
		elif d_max == 2:
			print(min(raz - 1, d_max))
		else:
			print(max(min(raz - 1, d_max), min(raz, d_max - 1)))


t = int(input())
while t != 0:
	n = int(input())
	arr = list(map(int, input().split()))[:n]
	uniqueCount = 0
	myMap = {}
	for i in range(n):
		if myMap.get(arr[i]) is None:
			uniqueCount += 1
			myMap[arr[i]] = 1
		else:
			myMap[arr[i]] += 1
	sameCount = max(myMap.values())
	if sameCount == uniqueCount - 1:
		print(sameCount)
	else:
		print(max(min(sameCount, uniqueCount - 1), min(sameCount - 1, uniqueCount)))
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	yes = 1
	no = 1
	maxno = 0
	a.sort()
	for i in range(1, n):
		if a[i] != a[i - 1]:
			if no > maxno:
				maxno = no
				no = 1
			no = 1
			yes += 1
		else:
			no += 1
	if no > maxno:
		maxno = no
	if maxno - 1 >= yes:
		print(min(yes, maxno))
	else:
		print(min(yes - 1, maxno))


T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	D = [0] * (N + 1)
	for a in A:
		D[a] += 1
	maxd = max(D)
	mul = N + 1 - D.count(0)
	if mul > maxd:
		print(maxd)
	else:
		print(min(maxd - 1, mul))


import sys
from statistics import mode

def maxsize(n):
	std = list(map(int, sys.stdin.readline().strip().split()))
	if n == 1:
		return 0
	std.sort()
	unis = comms = cpres = 1
	for i in range(1, n):
		if std[i] == std[i - 1]:
			cpres += 1
		else:
			if cpres > comms:
				comms = cpres
			cpres = 1
			unis += 1
	if cpres > comms:
		comms = cpres
	if unis > comms:
		return comms
	elif unis == comms:
		return unis - 1
	else:
		return unis
t = int(input())
for i in range(t):
	n = int(input())
	print(maxsize(n))


t = int(input())
from collections import Counter
for I in range(t):
	n = int(input())
	line = map(int, input().split())
	a = Counter(line)
	x1 = max(a.values())
	x2 = len(a)
	if x1 == x2:
		print(x1 - 1)
	elif x1 < x2:
		print(x1)
	else:
		print(x2)


for _ in range(int(input())):
	i = int(input())
	l = list(sorted(map(int, input().split())))
	s = set(l)
	d = {}
	smm = 0
	mxx = 0
	for num in l:
		if num in d:
			d[num] += 1
		else:
			d[num] = 1
		if d[num] > mxx:
			mxx = d[num]
	print(max(min(mxx - 1, len(s)), min(mxx, len(s) - 1)))


from collections import defaultdict
T = int(input())
for t in range(T):
	N = int(input())
	a = input()
	a = list(map(int, a.split(' ')))
	counted = defaultdict(int)
	for i in a:
		counted[i] = counted[i] + 1
	counted_tuple = tuple(((j, i) for (i, j) in counted.items()))
	counted_tuple = sorted(counted_tuple, reverse=True)
	upper_limit = counted_tuple[0][0]
	distinct = len(counted_tuple)
	if upper_limit == distinct:
		distinct -= 1
	print(min(upper_limit, distinct))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	myDict = defaultdict(int)
	for i in a:
		myDict[i] += 1
	myMaxC = 0
	myMaxV = 0
	for (i, v) in myDict.items():
		if myMaxC < v:
			myMaxC = v
			myMaxV = i
	if len(myDict) == myMaxC:
		print(myMaxC - 1)
	else:
		print(min(len(myDict), myMaxC, len(a) // 2))


from sys import stdin
t = int(stdin.readline())
for z in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = [0] * (max(a) + 1)
	for i in a:
		b[i] += 1
	x = max(b)
	y = 0
	for i in b:
		if i != 0:
			y += 1
	ans = max(min(x - 1, y), min(x, y - 1))
	print(ans)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	skillSet = set(a)
	skillCount = dict(Counter(a))
	sl = [(skillCount[k], k) for k in skillCount]
	sl.sort(reverse=True)
	if len(skillSet) != sl[0][0]:
		print(min(sl[0][0], len(skillSet)))
	else:
		print(min(sl[0][0], len(skillSet)) - 1)


for q in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	uni = 0
	for i in a:
		if d.get(i, 0) == 0:
			uni += 1
			d[i] = 1
		else:
			d[i] += 1
	res = 0
	uni -= 1
	for k in d.keys():
		res = max(min(d[k], uni), res)
		if d[k] > uni:
			if d[k] - uni >= 2:
				res = max(min(d[k] - 1, uni + 1), res)
	print(res)


def main():
	T = int(input())
	for t in range(T):
		n = int(input())
		a = list(map(int, input().split()))
		counts = [0 for i in range(n + 1)]
		for ai in a:
			counts[ai] += 1
		maxRepeat = 0
		dist = 0
		for c in counts:
			if c > 0:
				dist += 1
				if c > maxRepeat:
					maxRepeat = c
		if dist < maxRepeat:
			print(dist)
		elif dist == maxRepeat:
			print(maxRepeat - 1)
		else:
			print(maxRepeat)
main()


test = int(input())
for t in range(test):
	n = int(input())
	list = [int(x) for x in input().split()]
	c_list = list[:]
	c_list.sort()
	diff = 1
	same = 1
	max_same = same
	for i in range(1, n):
		if c_list[i] != c_list[i - 1]:
			diff += 1
			same = 1
		else:
			same += 1
		if same > max_same:
			max_same = same
	print(max(min(diff - 1, max_same), min(diff, max_same - 1)))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	n = int(input())
	studs = list(map(int, input().split()))
	unique = set(studs)
	dict1 = {}
	for s in range(n):
		if not studs[s] in dict1:
			dict1[studs[s]] = 1
		else:
			dict1[studs[s]] += 1
	reps = max(dict1.values())
	if reps == len(unique) - 1:
		if reps == 0:
			if n == 1:
				print(0)
			else:
				print(1)
		else:
			print(reps)
	elif reps > len(unique) - 1:
		if reps > len(unique):
			print(len(unique))
		else:
			print(len(unique) - 1)
	elif reps < len(unique) - 1:
		print(reps)


import sys
import random
from math import log2, log, ceil
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a > 0:
		return gcd(b % a, a)
	return b

def primefs(n):
	if n == 1:
		return 1
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]
spf_sieve()

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = int_array()
	dick = {}
	for i in a:
		dick[i] = dick.get(i, 0) + 1
	dists = len(dick)
	ans = 0
	for i in dick:
		this = min(dick[i], dists - 1)
		ans = max(this, ans)
		this = min(dick[i] - 1, dists)
		ans = max(this, ans)
	print(ans)


t = int(input())
l = []
temp = {}
for i in range(t):
	n = int(input())
	x = int(n / 2)
	temp = []
	for j in range(n):
		temp.append(0)
	k = input().split()
	for j in range(n):
		temp[int(k[j]) - 1] += 1
	max = 0
	un_l = 0
	ans = 0
	for j in range(n):
		if temp[j] > max:
			max = temp[j]
			un_l += 1
		elif temp[j] > 0:
			un_l += 1
	if un_l > max:
		ans = max
	elif un_l == max:
		ans = un_l - 1
	else:
		ans = un_l
	if ans < x:
		x = ans
	l.append(x)
for i in range(t):
	print(int(l[i]))


import sys
import os
from io import IOBase, BytesIO
import math
import collections
mod = 10 ** 9 + 7
pie = 3.1415926536

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def ain():
	return list(map(int, sin().split()))

def sin():
	return input().strip()

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def read2DIntArray(row, col):
	arr = []
	for i in range(0, row):
		temp = list(map(int, sin().split()))
		arr.append(temp)
	return arr

def read2DCharArray(row, col):
	arr = []
	for i in range(0, row):
		temp = str(sin())
		arr.append(temp)
	return arr

def smallestNumber(n):
	lst = list(str(n))
	lst.sort()
	tmp = ''
	for (i, n) in enumerate(lst):
		if n != '0':
			tmp = lst.pop(i)
			break
	return str(tmp) + ''.join(lst)

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	lst = []
	for p in range(n + 1):
		if prime[p]:
			lst.append(p)
	return lst

def nCr(n, r):
	a = 1
	b = 1
	c = 1
	for i in range(1, n + 1):
		c *= i
	for i in range(1, r + 1):
		b *= i
	for i in range(1, n - r + 1):
		a *= i
	return c // (a * b)

def sieveOfEratosthenes1(N, s):
	prime = [False] * (N + 1)
	for i in range(2, N + 1, 2):
		s[i] = 2
	for i in range(3, N + 1, 2):
		if prime[i] == False:
			s[i] = i
			for j in range(i, int(N / i) + 1, 2):
				if prime[i * j] == False:
					prime[i * j] = True
					s[i * j] = i

def generatePrimeFactors(N):
	s = [0] * (N + 1)
	sieveOfEratosthenes1(N, s)
	curr = s[N]
	cnt = 1
	factors = []
	power = []
	while N > 1:
		N //= s[N]
		if curr == s[N]:
			cnt += 1
			continue
		factors.append(curr)
		power.append(cnt)
		curr = s[N]
		cnt = 1
	return (factors, power)

def main():
	for _ in range(int(sin())):
		n = int(sin())
		arr = ain()
		dic = dict.fromkeys(arr, 0)
		for num in arr:
			dic[num] += 1
		lst = sorted(dic.values())
		l = len(lst)
		m = lst[-1]
		if l == 1:
			if n > 1:
				print(1)
			else:
				print(0)
		elif l == m:
			print(l - 1)
		elif l > m:
			print(m)
		else:
			print(l)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


import sys
import collections
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	a = collections.Counter(lst)
	a = list(a.items())
	a.sort(key=lambda x: x[1])
	ans = max(min(a[-1][1], len(a) - 1), min(a[-1][1] - 1, len(a)))
	print(ans)


import collections

def calc(A):
	cnt = collections.Counter(A)
	max_ = max(cnt.values())
	len_ = len(cnt)
	res = min(len_ - 1, max_)
	if res + 1 <= len(A) // 2 and max_ > res + 1:
		res += 1
	return res
N = int(input())
cases = []
for _ in range(N):
	_ = input()
	cases.append(list(map(int, input().split())))
for case in cases:
	print(calc(case))


from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	res = list(sys.stdin.readline())
	if res[-1] == '\n':
		return res[:-1]
	return res

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
	t = I()
	for _ in range(t):
		n = I()
		a = LI()
		x = len(set(a))
		d = defaultdict(lambda : 0)
		for i in a:
			d[i] += 1
		y = max(d.values())
		if x < y:
			print(x)
		elif x == y:
			print(x - 1)
		else:
			print(y)
	return
solve()


t = int(input())
for q in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = list(set(l))
	c = 0
	num = 0
	l = sorted(l)
	k = 1
	for i in range(1, n):
		if l[i] == l[i - 1]:
			k += 1
		else:
			k = 1
		if c < k:
			num = l[i]
			c = k
	if c >= len(s) + 1:
		print(len(s))
	else:
		print(min(c, len(s) - 1))


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	d = {}
	m = 0
	for i in range(n):
		if a[i] in d:
			d[a[i]] += 1
			m = max(d[a[i]], m)
		else:
			d[a[i]] = 1
			m = max(d[a[i]], m)
	if len(d) == 1 and m > 1:
		print(1)
	elif m == 1 and len(d) > 1:
		print(m)
	else:
		print(max(min(m - 1, len(d)), min(len(d) - 1, m)))


t = int(input())
from collections import defaultdict
cases = []
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	d = defaultdict(int)
	for i in a:
		d[i] += 1
	s = max(d.values())
	f = len(d.values()) - 1
	if f >= s:
		print(s)
	else:
		print(min(f + 1, s - 1))


t = int(input())
while t:
	n = int(input())
	a = input().split()
	a = [int(x) for x in a]
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 0
		d[a[i]] += 1
	m = 0
	for key in d:
		if len(d) - 1 >= d[key]:
			m = max(m, d[key])
		else:
			m = max(m, min(d[key] - 1, len(d)))
	print(m)
	t -= 1


t = int(input())
for x in range(t):
	n = int(input())
	eq = 1
	noteq = 0
	a = list(sorted(list(map(int, input().split()))))
	tmpeq = 1
	for i in range(1, n):
		if a[i] == a[i - 1]:
			tmpeq += 1
		else:
			noteq += 1
			eq = max(eq, tmpeq)
			tmpeq = 1
	eq = max(eq, tmpeq)
	tmpeq = 1
	if eq - noteq >= 2:
		noteq += 1
		eq -= 1
	print(min(eq, noteq, n // 2))


def main():
	from collections import Counter
	import sys
	input = sys.stdin.buffer.readline

	def GCD(x, y):
		while y:
			(x, y) = (y, x % y)
		return x

	def lcm(a, b):
		return a * b // GCD(a, b)

	def merge(a, start, mid, end, d):
		p = start
		q = mid + 1
		arr = [0] * (end - start + 1)
		k = 0
		coun = 0
		for i in range(start, end + 1):
			if p > mid:
				arr[k] = a[q]
				q += 1
				k += 1
			elif q > end:
				arr[k] = a[p]
				d[a[p]] += coun
				k += 1
				p += 1
			elif a[p] < a[q]:
				arr[k] = a[p]
				d[a[p]] += coun
				k += 1
				p += 1
			else:
				arr[k] = a[q]
				k += 1
				q += 1
				coun += 1
		for i in range(k):
			a[start] = arr[i]
			start += 1

	def mergesort(a, start, end, d):
		if start < end:
			mid = (start + end) // 2
			mergesort(a, start, mid, d)
			mergesort(a, mid + 1, end, d)
			merge(a, start, mid, end, d)

	def inversion(l):
		d = {}
		for i in l:
			d[i] = 0
		mergesort(l, 0, len(l) - 1, d)
		return d

	def binary_search_lower(A, low, high, k):
		if high - low != 1:
			mid = (low + high) // 2
			x = A[mid]
			if x >= k:
				return binary_search_lower(A, low, mid, k)
			else:
				return binary_search_lower(A, mid, high, k)
		if A[low] >= k:
			return low
		else:
			return high

	def binary_expo(x, n):
		if n == 0:
			return 1
		elif n % 2 == 0:
			return binary_expo(x * x, n // 2)
		else:
			return x * binary_expo(x * x, (n - 1) // 2)

	def modu_expo(x, n, m):
		if n == 0:
			return 1
		elif n % 2 == 0:
			return modu_expo(x * x % m, n // 2, m)
		else:
			return x * modu_expo(x * x % m, (n - 1) // 2, m) % m

	def eEuclid(c, m):
		if m == 0:
			global d
			global x
			global y
			d = c
			x = 1
			y = 0
		else:
			eEuclid(m, c % m)
			temp = x
			x = y
			y = temp - c // m * y

	def mod_Inv(c, m):
		eEuclid(c, m)
		return (x % m + m) % m

	def ncr(n, r, p):
		num = den = 1
		for i in range(r):
			num = num * (n - i) % p
			den = den * (i + 1) % p
		return num * pow(den, p - 2, p) % p
	for _ in range(int(input())):
		n = int(input())
		l = [int(y) for y in input().split()]
		if n == 1:
			print(0)
			continue
		d = Counter(l)
		ans = []
		for i in d.values():
			ans.append(i)
		ans.sort()
		if ans[-1] > len(ans):
			print(len(ans))
		else:
			print(min(len(ans) - 1, ans[-1]))
main()


t = int(input())
for j in range(t):
	n = int(input())
	ch = input()
	L = [int(i) for i in ch.split()]
	L.sort()
	nb = 1
	maxx = 1
	s = set()
	s.add(L[0])
	for i in range(1, n):
		s.add(L[i])
		if L[i] == L[i - 1]:
			nb += 1
			maxx = max(maxx, nb)
		else:
			nb = 1
	if maxx > len(s):
		print(len(s))
	else:
		print(min(len(s) - 1, maxx))


import sys
import math
from sys import stdin, stdout

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(input())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def main():
	for t in range(get_int()):
		n = int(input())
		arr = get_ints_in_list()
		distinct = {}
		for i in range(0, n):
			if arr[i] not in distinct:
				distinct[arr[i]] = 1
			else:
				distinct[arr[i]] += 1
		sameInt = 0
		for key in distinct:
			if distinct[key] > sameInt:
				sameInt = distinct[key]
		dist = len(distinct)
		if dist - 1 == sameInt:
			print(sameInt)
		else:
			ans1 = min(sameInt - 1, dist)
			ans2 = min(sameInt, dist - 1)
			print(max(ans1, ans2))
main()


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		s = set(l)
		d = {}
		for i in l:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		m = list(d.values())
		x = max(m)
		print(max(min(len(s) - 1, x), min(len(s), x - 1)))
	t -= 1


for _ in range(int(input())):
	n = int(input())
	lst = [int(i) for i in input().split()]
	set1 = set(lst)
	lst2 = [0] * (n + 1)
	for i in lst:
		lst2[i] += 1
	max1 = max(lst2)
	n = len(set1)
	if n == max1:
		print(n - 1)
	else:
		print(min([n, max1]))


def main():
	cases = int(input())
	for case in range(cases):
		n = int(input())
		l = list(map(int, input().split()))
		(d, mx) = ({}, -1)
		for i in l:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			if d[i] > mx:
				mx = d[i]
		if len(d) > mx:
			print(mx)
		elif len(d) == mx:
			print(mx - 1)
		else:
			print(len(d))
main()


from collections import Counter
T = int(input())
for t in range(T):
	n = int(input())
	skills = list(map(int, input().split()))
	diff = len(set(skills))
	same = Counter(skills).most_common(1)[0]
	(fe, fc) = same
	if fc < diff:
		ret = fc
	elif fc == diff:
		ret = fc - 1
	else:
		ret = diff
	print(ret)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	hash_a = {}
	ma = 0
	for i in a:
		hash_a[i] = hash_a.get(i, 0) + 1
		if hash_a[i] > ma:
			ma = hash_a[i]
	print(max(min(len(hash_a), ma - 1), min(len(hash_a) - 1, ma)))


from sys import stdin
input = stdin.readline
import bisect
import math
for xoxo in range(1):
	for _ in range(int(input())):
		n = int(input())
		u = []
		a = list(map(int, input().split()))
		c = [0] * n
		for i in range(n):
			c[a[i] - 1] += 1
			if c[a[i] - 1] == 1:
				u.append(a[i])
		m = max(c)
		if m < len(u):
			print(m)
		elif m > len(u):
			print(len(u))
		elif m == len(u):
			print(m - 1)


import math
t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	arr = []
	ans = 0
	for (key, value) in d.items():
		temp_key = key
		temp_val = value
		temp = min(value, len(d) - 1)
		temp1 = min(value - 1, len(d))
		ans = max(ans, max(temp1, temp))
	print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	count = defaultdict(lambda : 0)
	n = int(input())
	a = list(map(int, input().split()))
	for i in a:
		count[i] += 1
	distinct = len(count.keys())
	ma = 0
	for i in count.keys():
		if count[i] > ma:
			ma = count[i]
	if distinct > ma:
		print(ma)
	elif ma > distinct:
		print(distinct)
	else:
		print(ma - 1)


from collections import Counter
c = int(input())
for _ in range(c):
	n = int(input())
	str = input().split()
	c = Counter()
	for i in range(len(str)):
		c[str[i]] += 1
	c = dict(c.most_common())
	i_max = list(c)[0]
	a = c[i_max]
	b = len(c)
	if a == b:
		print(a - 1)
	else:
		print(min(a, b))


t = int(input())
for tt in range(t):
	n = int(input())
	lis = input().split()
	dic = {}
	for i in range(n):
		tmp = int(lis[i])
		if tmp not in dic:
			dic[tmp] = 0
		dic[tmp] += 1
	answers = []
	distinct = len(dic)
	for i in dic:
		if distinct - 1 >= dic[i]:
			answers.append(dic[i])
		elif distinct - 1 == dic[i] - 1:
			answers.append(distinct - 1)
		else:
			answers.append(distinct)
	print(max(answers))


from sys import stdin
t = int(stdin.readline())
while t > 0:
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	a.sort()
	maxi = 0
	count = 1
	s = set()
	s.add(a[0])
	for i in range(1, len(a)):
		s.add(a[i])
		if a[i] == a[i - 1]:
			count += 1
		else:
			if maxi < count:
				maxi = count
			count = 1
	if maxi < count:
		maxi = count
	if n == 1:
		print(0)
	elif maxi == len(s):
		print(maxi - 1)
	elif maxi < len(s):
		print(maxi)
	else:
		print(len(s))
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	items = list(map(int, input().split()))
	d = {}
	for item in items:
		if item not in d:
			d[item] = 0
		d[item] += 1
	distinct = len(d)
	same = 0
	for item in list(d.values()):
		if item > same:
			same = item
	if same < distinct:
		print(same)
	elif same > distinct:
		print(distinct)
	else:
		print(same - 1)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = [0] * (n + 1)
	m = set(a)
	for i in a:
		d[i] += 1
	o = max(d)
	p = len(m)
	ans1 = min(o, p - 1)
	ans2 = min(o - 1, p)
	print(max(ans1, ans2))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = {}
	for i in a:
		if i not in l:
			l[i] = 1
		else:
			l[i] += 1
	m = 0
	for i in l.values():
		if i > m:
			m = i
	if len(l) == m:
		print(min(n // 2, len(l) - 1, m))
	else:
		print(min(n // 2, len(l), m))


def two_teams(arr, n):
	if n == 1:
		return 0
	else:
		d = {}
		for i in arr:
			if i in d.keys():
				d[i] += 1
			else:
				d[i] = 1
		freq = sorted(list((d[i] for i in d)))
		if freq[-1] - (len(freq) - 1) > 1:
			return len(freq)
		return min(len(freq) - 1, freq[-1])
t = int(input())
for t_itr in range(t):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	result = two_teams(arr, n)
	print(result)


x = int(input())
for a in range(x):
	x1 = int(input())
	y = input().split()
	d = {}
	for b in range(0, x1):
		y[b] = int(y[b])
		if y[b] in d:
			d[y[b]] += 1
		else:
			d[y[b]] = 1
	n = x1 // 2
	if n == 0 or n == 1:
		print(n)
	else:
		m = 0
		s = 0
		for b in d.keys():
			if d[b] > m:
				m = d[b]
		if m >= len(d) + 1:
			print(min(n, m - 1, len(d)))
		else:
			print(min(n, m, len(d) - 1))


from collections import defaultdict as dfd
for _ in range(int(input())):
	val = dfd(int)
	N = int(input())
	A = list(map(int, input().split()))
	B = list(set(list(A)))
	if N == 1:
		print(0)
	else:
		temp = 0
		for i in range(len(A)):
			val[A[i]] += 1
		for (k, v) in val.items():
			temp = max(temp, v)
		if N == 2 and temp == 2:
			print(1)
		elif temp == 1:
			print(1)
		elif temp > len(B) - 1:
			print(min(temp - 1, len(B)))
		else:
			print(min(len(B) - 1, temp))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	i = 0
	l1 = []
	c = 0
	if n == 1:
		print(0)
	else:
		while i < n:
			if i == n - 1:
				c += 1
			elif l[i] != l[i + 1]:
				c += 1
			else:
				k = 1
				while i < n:
					if i == n - 1:
						l1.append(k)
					elif l[i] == l[i + 1]:
						k += 1
					else:
						l1.append(k)
						break
					i += 1
			i += 1
		le = len(l1)
		if le == 0:
			print(1)
		else:
			c += le - 1
			m = max(l1)
			if c == m:
				print(c)
			elif m == c + 1:
				print(c)
			elif m > c + 1:
				print(c + 1)
			elif m < c:
				print(m)


def check(n, a):
	if n <= 1:
		return 0
	b = [0] * n
	for i in range(0, n, 1):
		x = a[i]
		b[x - 1] = b[x - 1] + 1
	y = 0
	for j in range(0, n, 1):
		if b[j] != 0:
			y += 1
	l = y
	ma = max(b)
	s = l - 1
	if s == ma:
		return s
	if l < ma:
		return l
	if l > ma:
		return ma
	if l == ma:
		return l - 1
t = int(input())
for k in range(0, t, 1):
	n = int(input())
	a = list(map(int, input().split(' ')))
	z = check(n, a)
	print(z)


def read_int():
	return int(input().strip())

def read_ints():
	return list(map(int, input().strip().split(' ')))

def solve():
	n = read_int()
	a = read_ints()
	a.sort()
	diff = 1
	max_same = 1
	same = 1
	for i in range(1, n):
		if a[i] != a[i - 1]:
			diff += 1
			same = 1
		else:
			same += 1
			max_same = max(max_same, same)
	if max_same > diff:
		return diff
	if max_same < diff:
		return max_same
	return diff - 1
T = read_int()
for _ in range(T):
	print(solve())


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	ans1 = min(len(c) - 1, max(c.values()))
	ans2 = min(len(c), max(c.values()) - 1)
	print(max(ans1, ans2))


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	same = 1
	dis = 1
	ts = 1
	mode = s[0]
	for i in range(1, n):
		if s[i] == s[i - 1]:
			ts += 1
		else:
			if same < ts:
				same = ts
				mode = s[i - 1]
			ts = 1
			dis += 1
	if same < ts:
		same = ts
		mode = s[n - 1]
	x = min(same, dis)
	if same == dis:
		x -= 1
	print(x)


T = int(input())
while T > 0:
	N = int(input())
	L = list(map(int, input().split()))
	cnt = [0] * (N + 1)
	for i in L:
		cnt[i] += 1
	a = max(cnt)
	b = len(set(L))
	if a == b:
		print(a - 1)
	else:
		print(min(a, b))
	T -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i not in d:
			d[i] = 0
		d[i] += 1
	m = max(d.values())
	l = len(d) - 1
	print(min(m, l + (m > l + 1)))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	counts = [0] * (max(arr) + 1)
	for x in arr:
		counts[x] += 1
	s = sum([1 for x in counts if x])
	s1 = max(counts)
	if s1 == 1 and n > 1:
		print(1)
	elif s > s1:
		print(min(s - 1, s1))
	else:
		print(min(s, s1 - 1))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict(Counter(a))
	l = len(d.keys())
	v = max(d.values())
	if l == v:
		print(l - 1)
	else:
		size = min(n // 2, l, v)
		print(size)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr.sort()
	dif = 0
	i = 0
	equals = -1
	while i < n:
		temp = arr[i]
		dif += 1
		rep = 0
		while i < n and arr[i] == temp:
			i += 1
			rep += 1
		equals = max(equals, rep)
	dif -= 1
	equals -= 1
	if equals == dif:
		print(dif)
	else:
		print(min(dif, equals) + 1)


import sys
input = sys.stdin.buffer.readline
ans = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	maxSame = 1
	same = 1
	distinct = set({})
	distinct.add(a[0])
	for i in range(1, n):
		if a[i] == a[i - 1]:
			same += 1
			maxSame = max(maxSame, same)
		else:
			same = 1
		distinct.add(a[i])
	dist = len(distinct)
	if maxSame > dist:
		ans.append(dist)
	elif dist > maxSame:
		ans.append(maxSame)
	else:
		ans.append(maxSame - 1)
print('\n'.join(map(str, ans)))


from collections import Counter
ans = []
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxcnt = Counter(a).most_common(1)[0][1]
	diff = len(list(set(a)))
	ans.append(max(min(diff - 1, maxcnt), min(diff, maxcnt - 1)))
for i in ans:
	print(i)


def main_function():
	for i in range(int(input())):
		n = int(input())
		a = [int(k) for k in input().split(' ')]
		hash = [0 for h in range(len(a) + 2)]
		max_same = 0
		max_dif = 0
		for j in a:
			if hash[j] == 0:
				max_dif += 1
			hash[j] += 1
			if hash[j] > max_same:
				max_same = hash[j]
		if max_same > max_dif:
			print(max_dif)
		elif max_dif == max_same:
			print(max_dif - 1)
		else:
			print(max_same)
main_function()


t = int(input())
for i in range(0, t, 1):
	n = int(input())
	list1 = list(map(int, input().split(' ')))
	a = set(list1)
	list1.sort()
	count = 1
	maxi = 0
	for i in range(1, n, 1):
		if list1[i] == list1[i - 1]:
			count = count + 1
			if count >= maxi:
				maxi = count
		else:
			if count >= maxi:
				maxi = count
			count = 1
	if maxi == len(a):
		print(maxi - 1)
	else:
		print(min(maxi, len(a)))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	if len(s) == n and n > 1:
		print(1)
	else:
		d = {}
		m = 0
		e = a[0]
		for j in a:
			if j in d:
				d[j] += 1
			else:
				d[j] = 1
			if d[j] > m:
				m = d[j]
		y = len(s) - 1
		if m > y:
			m -= 1
			y += 1
		print(min(m, y))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = Counter(a)
	s_skills = x.most_common(1)[0][1]
	d_skills = len(set(a))
	if s_skills == d_skills:
		ans = s_skills - 1
	elif s_skills < d_skills:
		ans = s_skills
	else:
		ans = d_skills
	print(ans)


t = int(input())
for t1 in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	mx = 1
	for i in l:
		if i not in d:
			d[i] = 0
		d[i] += 1
		if d[i] > mx:
			mx = d[i]
	k = len(set(l))
	if n == 1:
		print(0)
	elif mx > k:
		print(k)
	elif mx == k:
		print(mx - 1)
	elif mx < k:
		print(mx)


t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	max = 1
	count = 1
	count2 = 1
	for y in range(1, n):
		if a[y] == a[y - 1]:
			count += 1
		else:
			if count > max:
				max = count
			count = 1
			count2 += 1
	if count > max:
		max = count
	if max > count2:
		print(count2)
	elif count2 - max >= 2:
		print(max)
	elif count2 - max == 1:
		print(max)
	else:
		print(max - 1)


t = int(input())
p = []
for i in range(0, t):
	n = input()
	a = input()
	a = a.split()
	y = set(a)
	dup = []
	for i in a:
		if i in y:
			y.remove(i)
		else:
			dup.append(i)
	y = set(a)
	if len(a) == 1:
		p.append(0)
	elif len(dup) == 0:
		p.append(1)
	elif len(a) == 2:
		p.append(1)
	elif len(a) == 3:
		p.append(1)
	elif len(a) > 3:
		if len(set(dup)) == len(dup):
			if len(a) == 4:
				if len(y) == len(dup):
					p.append(1)
				else:
					p.append(2)
			else:
				p.append(2)
		else:
			num_dup = 0
			for h in set(dup):
				num_iter = 0
				for z in dup:
					if h == z:
						num_iter += 1
				if num_iter > num_dup:
					num_dup = num_iter
			while num_dup > 0:
				if num_dup == len(y):
					p.append(num_dup)
					break
				elif len(y) > num_dup:
					if len(y) > num_dup + 1:
						p.append(num_dup + 1)
						break
					else:
						p.append(num_dup)
						break
				else:
					num_dup -= 1
for j in p:
	print(j)


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	d = {}
	for i in l:
		d[i] = d.get(i, 0) + 1
	o = len(set(l))
	Keymax = max(d, key=d.get)
	p = d[Keymax]
	print(max(min(o - 1, p), min(o, p - 1)))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = [0] * (max(a) + 1)
	x = set(a)
	for item in a:
		c[item] += 1
	m = max(c)
	l = len(x)
	if m > l:
		print(l)
	elif m == l:
		print(l - 1)
	else:
		print(m)


for _ in range(int(input())):
	n = int(input())
	array = list(map(int, input().split()))
	dic = {}
	for i in array:
		if i not in dic:
			dic[i] = 1
		else:
			dic[i] += 1
	d = len(dic)
	s = max(dic.values())
	if s == d:
		print(s - 1)
	else:
		print(min(d, s))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	(num, cnt) = c.most_common(1)[0]
	other = len(c) - 1
	if cnt > other:
		cnt -= 1
		other += 1
	print(min(cnt, other))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	if n < 2:
		print(0)
	else:
		a.sort()
		same = 1
		max_same = 1
		dist = 1
		for j in range(n - 1):
			if a[j] == a[j + 1]:
				same += 1
				if same >= max_same:
					max_same = same
			else:
				dist += 1
				same = 1
		team_len = min(max_same, dist)
		if max_same == dist:
			team_len = min(max_same - 1, dist)
		if team_len == 0:
			team_len = 1
		print(team_len)


import collections
test = int(input())
for i in range(test):
	n = int(input())
	game = collections.Counter(list(map(int, input().split()))).values()
	ln = len(game)
	mx = max(game)
	if ln > mx:
		print(mx)
	elif ln == mx:
		print(mx - 1)
	else:
		print(ln)


for _ in range(int(input())):
	n = int(input())
	l = sorted(list(map(int, input().split())))
	l1 = [l[0]]
	c = 1
	d = 1
	e = 0
	for i in range(1, len(l)):
		if l[i] == l1[-1]:
			c += 1
			if i == len(l) - 1:
				if c > e:
					e = c
		else:
			if c > e:
				e = c
			d += 1
			l1 = [l[i]]
			c = 1
	if e > d:
		e -= 1
	else:
		d -= 1
	print(min(d, e))


import sys
from collections import Counter

def most_frequent(List):
	occurence_count = Counter(List)
	return occurence_count.most_common(1)[0][0]
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	b = most_frequent(a)
	c = a.count(b)
	f = len(set(a)) - 1
	if n == 1:
		print(0)
	elif f == c:
		print(c)
	elif c > f:
		if c - f >= 2:
			print(f + 1)
		else:
			print(f)
	else:
		print(c)


t = int(input())
for i in range(t):
	n = int(input())
	b = {}
	a = list(map(int, input().split()))
	for elem in a:
		if elem not in b:
			b[elem] = 1
		else:
			b[elem] += 1
	otv = [min(max(b.values()), len(b) - 1), min(max(b.values()) - 1, len(b))]
	print(max(otv))


import sys
from collections import Counter
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	s2 = set()
	c = Counter(a)
	for (key, value) in c.most_common(1):
		mc = key
		count = value
	s2.add(mc)
	s1 = s - s2
	alen = len(s1)
	if count - alen > 1:
		print(alen + 1)
	elif count - alen == 1:
		print(alen)
	elif count - alen == 0:
		print(count)
	else:
		print(count)


import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def def_value():
	return False
t = inp()
for _ in range(t):
	n = inp()
	a = inlist()
	count = defaultdict(def_value)
	for i in a:
		count[i] += 1
	distinct = len(count)
	maxCount = max(count.values())
	res = 0
	if maxCount == distinct:
		res = maxCount - 1
	elif maxCount > distinct:
		res = distinct
	elif maxCount < distinct:
		res = maxCount
	print(res)


import collections
for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	d = collections.Counter(a)
	repeat = max(d.values())
	unq = len(set(a))
	print(min(unq, repeat) - 1 if unq == repeat else min(unq, repeat))


for _ in range(int(input())):
	n = int(input())
	l = sorted(list(map(int, input().split())))
	x = [0] * (max(l) + 1)
	for i in range(n):
		x[l[i]] += 1
	z = max(x)
	f = 0
	count = 0
	for i in x:
		if i == z and f == 0:
			f = 1
		elif i > 0:
			count += 1
	if count >= z:
		print(z)
	else:
		print(min(count + 1, (count + z) // 2))


t = int(input())
for test in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = set(x)
	d = {}
	c = 0
	for i in y:
		d[i] = 0
		c += 1
	for i in range(len(x)):
		d[x[i]] += 1
	max = -1
	for i in x:
		if d[i] > max:
			max = d[i]
	if c == max:
		print(max - 1)
	elif c < max:
		print(c)
	elif c > max:
		print(max)


from collections import Counter
t = int(input())
while t > 0:
	n = int(input())
	li = list(map(int, input().split()))
	p = len(set(li))
	q = Counter(li)
	b = max(q.values())
	if b == p:
		print(p - 1)
	elif b > p:
		print(p)
	else:
		print(b)
	t = t - 1


import math
import sys
from collections import defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]
from collections import Counter
INF = 10 ** 18
MOD = 1000000000 + 7
for _ in range(int(input())):
	N = int(input())
	A = alele()
	if N == 1:
		print(0)
	else:
		B = Counter(A)
		T = B.most_common(1)
		x = len(B)
		y = T[0][1]
		if y > x:
			print(x)
		elif y == x:
			print(y - 1)
		else:
			print(y)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	ss = list(map(int, input().split()))
	tt = set(ss)
	mm = Counter(ss).most_common(1)
	m = mm[0][0]
	cc = mm[0][1]
	cc = ss.count(m)
	tt.remove(m)
	if cc > len(tt) + 1:
		print(len(tt) + 1)
		continue
	elif cc > len(tt):
		print(len(tt))
		continue
	else:
		print(cc)


t = int(input())
s = 'abcdefghijklmnopqrstuvwxyz'
while t:
	d = {}
	n = int(input())
	arr = list(map(int, input().split()))
	for ke in arr:
		if ke in d:
			d[ke] += 1
		else:
			d[ke] = 1
	v = list(d.values())
	k = list(d.keys())
	mkey = k[v.index(max(v))]
	mval = d[mkey]
	nkl = len(k) - 1
	if mval - nkl == 1 or mval - nkl == 0:
		print(nkl)
	elif mval - nkl >= 2:
		print(nkl + 1)
	elif nkl >= mval:
		print(mval)
	t -= 1


import math
from sys import stdin

def input():
	return stdin.readline()
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	l1 = len(s) - 1
	d = {}
	m = 0
	t = 0
	for i in range(n):
		d[l[i]] = d.get(l[i], 0) + 1
		if m < d[l[i]]:
			m = d[l[i]]
			t = l[i]
	mi = min(l1, m)
	if l1 + 1 < m:
		mi += 1
	print(mi)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sarr = set(arr)
	distele = len(sarr)
	if distele == n:
		if distele > 1:
			print(1)
		else:
			print(0)
	else:
		from statistics import mode
		md = mode(arr)
		mdcount = arr.count(md)
		if distele > mdcount:
			print(mdcount)
		elif distele == mdcount:
			print(mdcount - 1)
		else:
			print(distele)


from collections import Counter as cou
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		tm = input()
		print(0)
		continue
	l = list(map(int, input().split()))
	c = cou(l)
	m = len(c)
	arr = []
	for i in c:
		arr.append(c[i])
	h = max(arr)
	if h == m:
		h -= 1
	print(min(h, m))


t = int(input())
for _ in range(t):
	n = int(input())
	skills = map(int, input().split())
	d = {}
	for s in skills:
		if s in d:
			d[s] += 1
		else:
			d[s] = 1
	m = 0
	for k in d.keys():
		m = max(m, max(min(d[k], len(d) - 1), min(d[k] - 1, len(d))))
	print(m)


from statistics import mode
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	n_distinct = len(set(a))
	if n_distinct == n:
		n_rep = 1
	else:
		n_rep = a.count(mode(a))
	opt1 = min(n_distinct - 1, n_rep)
	opt2 = min(n_distinct, n_rep - 1)
	print(max(opt2, opt1))


q = int(input())
Dict = dict({})
while q > 0:
	q -= 1
	n = int(input())
	a = list(map(int, input().split()))
	a.insert(0, 0)
	Dict.clear()
	for i in range(1, n + 1):
		if Dict.get(a[i]) == None:
			Dict[a[i]] = 1
		else:
			Dict[a[i]] += 1
	print(max(min(max(Dict.values()) - 1, len(Dict)), min(max(Dict.values()), len(Dict) - 1)))


def RA():
	l = input().split()
	return [int(s) for s in l]

def solve():
	n = int(input())
	arr = RA()
	if n == 1:
		print(0)
		return
	freq = {}
	for a in arr:
		if a in freq:
			freq[a] += 1
		else:
			freq[a] = 1
	maxSame = 0
	for k in freq:
		maxSame = max(maxSame, freq[k])
	ans = min(maxSame, len(freq) - 1)
	ans2 = min(maxSame - 1, len(freq))
	print(max(ans, ans2))
t = int(input())
for i in range(t):
	solve()


for i in range(int(input())):
	input()
	x = input().split()
	y = dict()
	for j in x:
		y[j] = y.get(j, 0) + 1
	a = max(y.values())
	b = len(y) - 1
	print((min(a, b), min(a - 1, b + 1))[a - 1 > b])


t = int(input())
while t > 0:
	n = int(input())
	l = [int(s) for s in input().split()]
	d = {}
	k = 0
	for i in l:
		if i not in d:
			d[i] = 1
			k = max(k, d[i])
		else:
			d[i] += 1
			k = max(k, d[i])
	if len(d) == k - 1:
		print(len(d))
	elif len(d) - 1 == k or len(d) == k:
		print(len(d) - 1)
	elif len(d) < k:
		print(len(d))
	else:
		print(k)
	t -= 1


import sys
from collections import Counter
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve(n, arr):
	if len(arr) == 1:
		print(0)
		return
	maxi = 0
	ele = 0
	for (e, t) in Counter(arr).items():
		if t > maxi:
			maxi = t
			ele = e
	if maxi == 1:
		print(1)
		return
	ll = len(set(arr)) - 1
	if ll > maxi:
		print(maxi)
	elif ll < maxi:
		if maxi - 1 >= ll + 1:
			print(ll + 1)
		else:
			print(ll)
	else:
		print(ll)
for _ in range(int(input())):
	n = int(input())
	arr = int_arr()
	solve(n, arr)


from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = []
for i in range(26):
	abc.append(chr(97 + i))
for _ in range(ii()):
	n = ii()
	a = li()
	n1 = len(set(a))
	m = {}
	for i in a:
		if i not in m:
			m[i] = 1
		else:
			m[i] += 1
	x = 0
	for i in m.keys():
		x = max(x, m[i])
	x1 = min(n1 - 1, x)
	x2 = min(n1, x - 1)
	print(max(x1, x2))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	hash = [0] * (n + 1)
	for i in range(0, len(a)):
		hash[a[i]] += 1
	maxi = max(hash)
	ans = 0
	hash.pop(hash.index(max(hash)))
	for i in range(1, len(hash)):
		if hash[i] > 0:
			ans = ans + 1
	if ans >= maxi:
		print(maxi)
	elif maxi - ans == 1:
		print(ans)
	else:
		print(ans + 1)


from collections import Counter as CC
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	d = CC(l)
	x = max(min(len(d), max(d.values()) - 1), min(len(d) - 1, max(d.values())))
	if x == 0:
		print(1)
	else:
		print(x)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	l.sort()
	k = 1
	c = 1
	for i in range(1, n):
		if l[i - 1] == l[i]:
			c += 1
		else:
			if c > k:
				k = c
			c = 1
	if c > k:
		k = c
	if k == len(s):
		print(k - 1)
	elif k > len(s):
		print(len(s))
	else:
		print(k)


from collections import Counter
t = int(input())
while t > 0:
	n = int(input())
	x = input().split()
	ls = []
	for i in x:
		ls.append(int(i))
	c = Counter(ls)
	count = len(c) - 1
	y = c.most_common()[0][1]
	if count >= y:
		print(y)
	elif y - count == 1:
		print(count)
	else:
		print(count + 1)
	t -= 1


for i in range(int(input())):
	k = int(input())
	a = list(map(int, input().split()))
	b = {}
	c = len(set(a))
	for i in a:
		if i not in b:
			b[i] = 1
		else:
			b[i] += 1
	d = 0
	for i in b.values():
		if i > d:
			d = i
	if c == d:
		print(d - 1)
	elif c > d:
		print(d)
	elif c < d:
		print(c)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	c = Counter(map(int, input().split()))
	x = max(c.values())
	k = c.keys()
	if x >= len(k):
		x = min(x - 1, len(k))
	print(x)


t = int(input())
while t:
	t += -1
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	mx = 0
	c = 1
	for i in range(n - 1):
		if l[i] == l[i + 1]:
			c -= -1
		else:
			mx = max(mx, c)
			c = 1
	mx = max(mx, c)
	l = list(set(l))
	mx2 = len(l)
	if mx > mx2:
		print(mx2)
	elif mx == mx2:
		print(mx - 1)
	else:
		print(mx)


from sys import stdin
inp = stdin.readline
t = int(inp().strip())
ans = []
for i in range(t):
	d = {}
	nn = int(inp().strip())
	arr = [int(x) for x in inp().strip().split()]
	s = set(arr)
	mxsame = 0
	for at in arr:
		d[at] = d.setdefault(at, 0)
		d[at] += 1
		mxsame = max(mxsame, d[at])
	mxunique = len(s)
	if mxsame == mxunique:
		mxsame -= 1
	print(min(mxsame, mxunique))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if len(arr) == 0 or len(arr) == 1:
		print(0)
	else:
		p = len(set(arr))
		l = [0 for i in range(max(arr) + 1)]
		for i in arr:
			l[i] += 1
		q = max(l) - 1
		mini = min(p, q)
		max1 = 1
		max1 = max(max1, mini)
		w = len(set(arr)) - 1
		r = max(l)
		d = min(w, r)
		max2 = 1
		max2 = max(max2, d)
		print(max(max1, max2))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	if n < 2:
		print(0)
	elif len(s) == n:
		print(1)
	else:
		d = Counter(l)
		m = max(d.values())
		for (i, j) in d.items():
			if j == m:
				x = i
				break
		s.discard(x)
		if len(s) >= m:
			print(m)
		else:
			print(min(len(s) + 1, m - 1))


def array_to_string(a):
	s = ''
	for i in a:
		s += str(i)
	return s
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	d = dict()
	for i in a:
		if d.get(i):
			d[i] += 1
			continue
		d[i] = 1
	temp = [1]
	s = 0
	for (k, v) in d.items():
		if v > 1:
			temp.append(v)
		s += v
	mx = max(temp)
	size = len(d)
	if size < mx:
		print(size)
	elif size == mx:
		print(size - 1)
	else:
		print(mx)


from collections import defaultdict, Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	l = len(c)
	(e, count) = c.most_common(1)[0]
	rest = l - 1
	opt = max(min(count, rest), min(count - 1, rest + 1))
	print(opt)


from sys import stdin
from collections import Counter
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	arr = [int(x) for x in stdin.readline().split()]
	c = Counter(arr)
	count = c.most_common(1)[0][1]
	print(max(min(len(c) - 1, count), min(len(c), count - 1)))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	p = {}
	for i in range(n):
		if a[i] in p.keys():
			p[a[i]] += 1
		else:
			p[a[i]] = 1
	mx = -1
	good = -1
	for i in p.keys():
		if p[i] > mx:
			good = i
		mx = max(mx, p[i])
	s = set()
	for i in a:
		if i != good:
			s.add(i)
	s = len(s)
	print(max(min(s + 1, mx - 1), min(s, mx)))


import bisect
for _ in range(int(input())):
	n = int(input())
	s = sorted(list(map(int, input().split())))
	a = 0
	for i in set(s):
		p = bisect.bisect(s, i) - bisect.bisect_left(s, i)
		if p > a:
			a = p
	if n < 2:
		print(0)
	elif n == len(set(s)):
		print(1)
	elif a == len(set(s)):
		print(a - 1)
	elif a > len(set(s)):
		print(len(set(s)))
	else:
		print(a)


from collections import Counter
t = int(input())
while t:
	n = int(input())
	c = sorted(Counter(input().split()).items(), key=lambda x: x[1], reverse=True)
	first = c[0]
	del c[0]
	firstOcc = first[1]
	ans = 0
	for i in c:
		ans += 1
		firstOcc -= 1
		if not firstOcc:
			break
	if firstOcc > 1:
		ans += 1
	print(ans)
	t -= 1


import sys
input = sys.stdin.readline
from collections import defaultdict as dd
t = int(input())
while t:
	b = int(input())
	l = list(map(int, input().split()))
	d = dd(int)
	mx = -1
	ind = -1
	for i in l:
		d[i] += 1
		if d[i] > mx:
			mx = d[i]
			ind = i
	nn = len(d)
	a = nn - 1
	b = d[ind]
	if abs(a - b) <= 1:
		print(min(a, b))
	elif a > b:
		print(b)
	else:
		print(a + 1)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	sp = list(map(int, input().split()))
	d = dict()
	for j in sp:
		if j in d:
			d[j] += 1
		else:
			d[j] = 1
	u = max(d.values())
	count1 = len(d) - 1
	w = min(count1, u)
	if u - count1 > 1 and u > 1:
		w = max(w, count1 + 1)
	print(min(w, n // 2))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	if n == 1:
		print(0)
		continue
	k = 0
	for i in l:
		d[i] += 1
	for (key, val) in d.items():
		k = max(k, val - 1)
	if k + 1 < len(d):
		k += 1
		print(min(k, len(d)))
		continue
	print(min(k, len(d)))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	fir = max(c.values())
	sec = len(c.keys())
	if fir > sec:
		print(sec)
	elif fir < sec:
		print(fir)
	else:
		print(fir - 1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		counter = {}
		for i in arr:
			try:
				counter[i] += 1
			except KeyError:
				counter[i] = 1
		arr = set(arr)
		counter = list(counter.values())
		temp = max(counter)
		if len(arr) <= temp:
			temp -= 1
		print(min(temp, len(arr)))


from collections import defaultdict
for _ in range(int(input())):
	Hash = defaultdict(lambda : 0)
	N = int(input())
	L = list(map(int, input().split()))
	for i in L:
		Hash[i] += 1
	M = 0
	for (i, j) in Hash.items():
		M = max(M, j)
	for (i, j) in Hash.items():
		if Hash[i] == M:
			check = i
			break
	x = 0
	for (i, j) in Hash.items():
		if i != check:
			x += 1
	if M == x:
		print(M)
	elif M > x and M - x == 1:
		print(x)
	elif M > x and M - x > 1:
		print(x + 1)
	else:
		print(min(M, x))


from collections import Counter
t = int(input())
while t:
	n = int(input())
	ai = input().split()
	d = Counter(ai)
	if len(list(d.keys())) == max(d.values()):
		print(max(d.values()) - 1)
	elif max(d.values()) < len(list(d.keys())):
		print(max(d.values()))
	else:
		print(min(max(d.values()), len(list(d.keys()))))
	t = t - 1


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s1 = set(l)
	c = Counter(l)
	maxi = 0
	for i in c.keys():
		if c[i] > maxi:
			maxi = c[i]
	k = len(s1)
	if k == maxi:
		maxi = maxi - 1
	print(min(k, maxi))


for t in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	unique_skills = set()
	skill_to_count = [0] * (n + 1)
	for a in A:
		skill_to_count[a] += 1
		unique_skills.add(a)
	max_size = -1
	for skill in unique_skills:
		count = skill_to_count[skill]
		m1 = min(count - 1, len(unique_skills))
		m2 = min(count, len(unique_skills) - 1)
		m = max(m1, m2)
		max_size = max(max_size, m)
	print(max_size)


for T in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n > 1:
		count = 0
		stats = [0 for i in range(n + 2)]
		for i in range(n):
			stats[arr[i]] += 1
		s = max(stats)
		for i in range(n + 2):
			if stats[i] > 0:
				count += 1
		if count == s:
			print(count - 1)
		elif count < s:
			print(count)
		else:
			print(s)
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	numberDistinct = 1
	most = 1
	temp = 1
	for i in range(1, n):
		if a[i] != a[i - 1]:
			temp = 1
			numberDistinct += 1
		else:
			temp += 1
			most = max(most, temp)
	ans = 0
	if numberDistinct - 1 >= most:
		ans = max(ans, most)
	if numberDistinct >= most - 1:
		ans = max(ans, most - 1)
	else:
		ans = max(ans, numberDistinct)
	print(min(ans, n // 2))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = [0 for i in range(max(l))]
	for i in l:
		l1[i - 1] += 1
	cnt = l1.count(0)
	distinct = len(l1) - cnt
	m = max(l1)
	if n == 1:
		print(0)
	elif n == distinct:
		print(1)
	elif distinct > m:
		print(m)
	elif distinct == m:
		print(m - 1)
	else:
		print(distinct)


for _ in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()))
	h = [0 for a in range(0, n + 1)]
	dis = 0
	for i in l:
		if h[i] == 0:
			dis += 1
		h[i] += 1
	maximum = max(h)
	if maximum == 1:
		if h.count(1) > 1:
			print(1)
		else:
			print(0)
		continue
	if maximum <= dis - 1:
		print(maximum)
	else:
		while True:
			maximum -= 1
			if dis >= maximum:
				print(maximum)
				break


t = int(input())
for x in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 2:
		print(0)
	else:
		hashmap = {}
		max_u = 1
		count = 0
		for ele in a:
			if ele in hashmap.keys():
				hashmap[ele] += 1
				if hashmap[ele] > max_u:
					max_u = hashmap[ele]
			else:
				count += 1
				hashmap[ele] = 1
		if max_u == count:
			print(count - 1)
		else:
			print(min(max_u, count))


t = int(input())
ch = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in a:
		if not d.get(i, None):
			d[i] = 0
		d[i] += 1
	k = d.keys()
	lk = len(k)
	ma = 0
	for i in k:
		ma = max(ma, d[i])
	if ma < lk:
		print(ma)
	elif ma > lk:
		print(lk)
	else:
		print(ma - 1)


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l = sorted(l)
	e = 0
	c = 1
	for i in range(n - 1):
		if l[i] == l[i + 1]:
			c += 1
		else:
			c = 1
		e = max(e, c)
	ne = len(set(l))
	if ne == e:
		print(e - 1)
	else:
		print(min(e, ne))


a = int(input())
for i in range(0, a):
	b = int(input())
	arr = list(map(int, input().split()))
	d = dict()
	for i in arr:
		d[i] = 0
	for i in arr:
		d[i] = d[i] + 1
	counter = 0
	s = set(arr)
	key = None
	for i in s:
		count = d[i]
		if count > counter:
			counter = count
			key = i
	if counter < len(s):
		print(counter)
	elif counter > len(s):
		print(len(s))
	else:
		print(len(s) - 1)


import statistics
from statistics import mode
a = int(input())
for i in range(0, a):
	b = int(input())
	c = list(map(int, input().split()))
	d = c.count(mode(c))
	e = len(set(c))
	if d == e:
		print(d - 1)
	else:
		print(min(d, e))


t = int(input())
for _ in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	i = 0
	similar = 0
	different = 0
	flag = 0
	while i < n - 1:
		count = 0
		while i < n - 1 and l[i] == l[i + 1]:
			count = count + 1
			i = i + 1
		count = count + 1
		similar = max(similar, count)
		while i < n - 1 and l[i] != l[i + 1]:
			different = different + 1
			i = i + 1
	if similar - different > 1:
		print(min(similar, different) + 1)
	else:
		print(min(similar, different))


from collections import Counter
gi = lambda : list(map(int, input().split()))
for _ in range(gi()[0]):
	(n,) = gi()
	a = gi()
	occ = Counter(a)
	diff = len(occ)
	ans = 0
	for e in occ:
		if occ[e] - 1 >= diff:
			ans = diff
		else:
			ans = max(ans, min(occ[e], diff - 1))
	print(ans)


outputs = []
for __ in range(int(input())):
	N = int(input())
	A = [int(num) for num in input().split()]
	if N < 2:
		outputs.append(0)
	else:
		d = {}
		for i in A:
			d[i] = d.get(i, 0) + 1
		a = len(d.keys())
		b = max(d.values()) - 1
		if b == 0:
			outputs.append(1)
		elif a - b <= 1:
			outputs.append(min(a, b))
		else:
			outputs.append(b + 1)
for output in outputs:
	print(output)


for t in range(int(input())):
	n = int(input())
	abc = list(map(int, input().split()))
	sett = set()
	for ele in abc:
		sett.add(ele)
	cnt = []
	c = 1
	abc.sort()
	for i in range(1, n):
		if abc[i] == abc[i - 1]:
			c += 1
		else:
			cnt.append(c)
			c = 1
	cnt.append(c)
	print(max(min(len(sett), max(cnt) - 1), min(len(sett) - 1, max(cnt))))


def compute(arr, n):
	if n == 1:
		return 0
	else:
		dic = {}
		unique_len = 0
		copied_len = 0
		for i in arr:
			try:
				dic[i] += 1
			except KeyError:
				dic[i] = 1
		for i in dic.keys():
			if dic[i] == 1:
				unique_len += 1
			elif copied_len == 0:
				copied_len = dic[i]
			elif dic[i] > copied_len:
				copied_len = dic[i]
				unique_len += 1
			else:
				unique_len += 1
		if unique_len == 0 or copied_len == 0:
			return 1
		else:
			return max(min(copied_len - 1, unique_len + 1), min(copied_len, unique_len))
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(compute(arr, n))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	s = set()
	for i in range(n):
		if d.get(l[i]) == None:
			d[l[i]] = 1
		else:
			d[l[i]] += 1
		s.add(l[i])
	lens = len(s)
	m = max(d.values()) - 1
	if lens == m:
		ans = lens
	elif lens < m:
		ans = lens
	else:
		ans = min(lens - 1, m + 1)
	print(ans)


import sys
input = sys.stdin.readline

def input_list():
	return list(map(int, input().split()))

def f():
	n = int(input())
	l = input_list()
	d = {}
	nb_skills = 0
	max_skills = 0
	for x in l:
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
			nb_skills += 1
		max_skills = max(max_skills, d[x])
	result = 0
	if n == 1:
		result = 0
	elif max_skills == 1:
		result = 1
	else:
		result = max(min(nb_skills - 1, max_skills), min(nb_skills, max_skills - 1))
	print(result)

def main():
	t = int(input())
	for _ in range(t):
		f()
main()


import statistics
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	x = len(set(a))
	d = m = 0
	for j in range(1, n):
		if a[j] == a[j - 1]:
			d += 1
		else:
			if d > m:
				m = d
			d = 0
	m = max(d, m) + 1
	if m > x:
		print(min(x, m))
	else:
		print(min(x - 1, m))


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	freq = {}
	maxi = float('-inf')
	for x in a:
		freq[x] = freq.get(x, 0) + 1
		maxi = max(maxi, freq[x])
	uniq = len(freq)
	if n == 1:
		print(0)
		continue
	if uniq == n:
		print(1)
		continue
	print(max(min(uniq - 1, maxi), min(uniq, maxi - 1)))


from collections import Counter

def res(n, a):
	d = Counter(a)
	first = len(d.keys())
	second = max(d.values())
	if first == second:
		return first - 1
	else:
		return min(first, second)
t = int(input())
for i in range(t):
	n = int(input())
	a = input().split()
	if len(a) == 1:
		print(0)
	elif len(a) == 2:
		print(1)
	else:
		print(res(n, a))


t = int(input())
count = 0
while count < t:
	count += 1
	n = int(input())
	l = list(map(int, input().split(' ')))
	l = sorted(l)
	prev = l[0]
	tcount = 1
	champ = 1
	acc = 0
	for i in l:
		if i == prev:
			acc += 1
		else:
			tcount += 1
			prev = i
			champ = max(champ, acc)
			acc = 1
	champ = max(champ, acc)
	if n == 1:
		print(0)
	elif l[0] == l[-1]:
		print(1)
	else:
		print(max(min(tcount - 1, champ), min(tcount, champ - 1)))


from collections import *
for s in [*open(0)][2::2]:
	a = Counter(s.split())
	n = len(a)
	m = max(a.values())
	print(min(n, m) - (n == m))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	freq = [0] * (n + 1)
	cnt = 0
	for j in range(0, n):
		freq[a[j]] += 1
	maxx_eq = max(freq)
	maxx_un = n - maxx_eq
	for j in range(0, n + 1):
		if freq[j] == 0:
			cnt += 1
	val = max(min(maxx_eq - 1, n + 1 - cnt), min(maxx_eq, n - cnt))
	print(val)


import time

def skils(n, l):
	mydict = {}
	for i in range(n):
		if l[i] not in mydict:
			mydict[l[i]] = 0
		mydict[l[i]] = mydict[l[i]] + 1
	skill_count = len(mydict.key())
	ans = 0
	ans = max(mydict.values())
	if skill_count >= ans:
		return ans
	return skill_count
t = int(input())
for i in range(t):
	n = int(input())
	l = input().split()
	mydict = {}
	for j in range(n):
		x = int(l[j])
		if x not in mydict:
			mydict[x] = 0
		mydict[x] = mydict[x] + 1
	skill_count = len(mydict.keys())
	ans = 0
	ans = max(mydict.values())
	if skill_count - 1 >= ans:
		print(ans)
		continue
	if skill_count == ans:
		print(skill_count - 1)
		continue
	print(skill_count)


test = int(input())
while test != 0:
	n = int(input())
	arr = list(map(int, input().split()))[:n]
	h = {}
	if n == 1:
		print(0)
	else:
		for i in range(n):
			if arr[i] in h.keys():
				h[arr[i]] += 1
			else:
				h[arr[i]] = 1
		index = 0
		maxx = 0
		for i in h:
			x = max(maxx, h[i])
			if x == h[i]:
				index = i
				maxx = x
		distinct = len(h)
		if maxx == distinct - 1:
			print(maxx)
		else:
			print(max(min(maxx, distinct - 1), min(maxx - 1, distinct)))
	test -= 1


import sys
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 7
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	dic = {}
	for i in a:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	maxx = -1
	for i in dic:
		val1 = min(dic[i], len(dic) - 1)
		val2 = min(dic[i] - 1, len(dic))
		tempm = max(val1, val2)
		maxx = max(tempm, maxx)
	print(maxx)


T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	last = None
	count = 0
	m = 0
	diff = 0
	for e in a:
		if e == last:
			count += 1
			if count > m:
				m = count
		else:
			last = e
			diff += 1
			count = 1
			if count > m:
				m = count
	k = min(m, diff)
	k -= m == diff
	print(k)


import string
import random
t = int(input())
for i in range(t):
	n = int(input())
	ll = [int(i) for i in input().split(' ')]
	ll.sort()
	ls = []
	count = 1
	if len(ll) == 1:
		print(0)
		continue
	for j in range(len(ll) - 1):
		if ll[j] == ll[j + 1]:
			count += 1
		else:
			ls.append(count)
			count = 1
	ls.append(count)
	ls.sort()
	nbe = len(ls)
	nm = ls[nbe - 1]
	res = min(nbe, nm)
	res = min(res, n // 2)
	if nm == nbe and nm <= n // 2:
		res = res - 1
	print(res)


from collections import Counter, defaultdict, deque
from operator import itemgetter
import math
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	arr = [int(x) for x in input().split()]
	c = Counter(arr)
	mc = max(c.items(), key=itemgetter(1))[1]
	l = len(set(arr))
	if mc == l:
		print(mc - 1)
	else:
		print(min(l, mc))
tt = int(input())
for test in range(tt):
	solve()


import sys
from math import sqrt, floor, ceil
from collections import deque, Counter
inp = sys.stdin.readline
read = lambda : list(map(int, inp().split()))

def a():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		res = abs(n - 1 - ceil(n / 2) + n % 2)
		ans += str(res) + '\n'
	print(ans)

def b():
	ans = ''
	alph = 'abcdefghijklmnopqrstuvwxyz'
	for _ in range(int(inp())):
		(n, a, b) = read()
		seq = alph[:b] + alph[:b] * ((a - b) // b) + alph[:b][:(a - b) % b]
		res = seq * (n // a) + seq[:n % a]
		ans += res + '\n'
	print(ans)

def c():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		arr = read()
		uniq = len(set(arr))
		c = Counter(arr)
		mi = min(uniq, c.most_common()[0][1]) - (uniq == c.most_common()[0][1])
		ans += str(mi) + '\n'
	print(ans)
c()


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	nbocc = [0 for i in range(n + 1)]
	nbdiff = 0
	nbmax = 0
	for x in a:
		if nbocc[x] == 0:
			nbdiff += 1
		nbocc[x] += 1
		nbmax = max(nbmax, nbocc[x])
	rep = min(nbdiff, nbmax)
	if nbdiff == nbmax:
		rep -= 1
	print(rep)


n_tests = int(input())
for _ in range(n_tests):
	n = int(input())
	arr = [int(i) for i in input().split()]
	if n != 1:
		counts = dict()
		for item in arr:
			if item not in counts:
				counts.update({item: 0})
			counts[item] += 1
		max_d = len(counts)
		max_s = 0
		for i in counts.values():
			if i > max_s:
				max_s = i
		if max_s == max_d:
			max_s -= 1
		print(min(max_d, max_s))
	else:
		print('0')


dct = {}

def ok(x, uniq):
	for (key, value) in dct.items():
		if x == value:
			if uniq - 1 >= x:
				return 1
			else:
				continue
		elif value > x:
			if uniq >= x:
				return 1
			else:
				continue
	return 0

def main():
	t = int(input())
	for _ in range(t):
		dct.clear()
		n = int(input())
		lst = list(map(int, input().split()))
		for x in lst:
			dct[x] = dct.get(x, 0) + 1
		uniq = len(dct)
		l = 0
		r = n // 2 + 1
		ans = 0
		while l <= r:
			mid = l + (r - l) // 2
			if ok(mid, uniq):
				ans = mid
				l = mid + 1
			else:
				r = mid - 1
		print(ans)
main()


import sys
import os
from io import BytesIO, IOBase

def cel(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return defaultdict(int)

def ddl():
	return defaultdict(list)

def ddd():
	return defaultdict(defaultdict(int))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
for _ in range(ii()):
	n = ii()
	a = li()
	d = dd()
	for i in range(n):
		d[a[i]] += 1
	mx = 0
	ln = len(d)
	for i in d:
		mx = max(mx, max(min(d[i] - 1, ln), min(d[i], ln - 1)))
	print(mx)


for _ in range(int(input())):
	n = int(input())
	data = list(map(int, input().split()))
	temp = {}
	summary = 0
	for i in data:
		if i not in temp:
			temp[i] = 1
			summary += 1
		else:
			temp[i] += 1
	result = 0
	for (key, value) in temp.items():
		now = max(min(temp[key], summary - 1), min(temp[key] - 1, summary))
		result = max(result, now)
	print(result)


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
n = inp()
for i in range(n):
	t = inp()
	l = inlt()
	if len(l) == 1:
		print(0)
	else:
		types = 0
		d = {}
		for x in l:
			if x not in d:
				d[x] = 1
				types += 1
			else:
				d[x] += 1
		max_cnt = 0
		max_val = 0
		for x in d:
			if d[x] > max_cnt:
				max_cnt = d[x]
				max_val = x
		if max_cnt - 1 >= types:
			print(types)
		else:
			print(min(max_cnt, types - 1))


from collections import Counter
for _ in range(int(input())):
	N = int(input())
	List = [int(x) for x in input().split()]
	Dict = Counter(List)
	A = []
	for i in Dict.keys():
		A.append((Dict[i], i))
	A.sort(reverse=True)
	Len = len(A)
	Max = A[0][0]
	if Len - 1 >= Max:
		Ans = Max
	elif Len == Max:
		Ans = Max - 1
	else:
		Ans = Len
	print(Ans)


import sys

class CTwoTeamsComposing:

	def solve(self):
		from collections import defaultdict
		for _ in range(int(input())):
			n = int(input())
			a = [int(_) for _ in input().split()]
			cnt = defaultdict(int)
			for x in a:
				cnt[x] += 1
			vals = list(dict(cnt).values())
			diff = len(cnt)
			same = max(vals)
			v1 = min(same - 1, diff)
			v2 = min(same, diff - 1)
			ans = max(v1, v2)
			print(ans)
solver = CTwoTeamsComposing()
input = sys.stdin.readline
solver.solve()


from collections import *
I = input
for _ in [0] * int(I()):
	I()
	a = Counter(I().split()).values()
	x = len(a)
	y = max(a)
	print(min(x, y) - (x == y))


try:
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split()))
		if n == 1:
			print(0)
		else:
			B = [0] * (n + 1)
			for i in range(n):
				B[A[i]] += 1
			count0 = B.count(0) - 1
			if max(B) == 1:
				print(1)
			elif max(B) - 1 >= n - count0:
				print(n - count0)
			elif max(B) >= n - count0 - 1:
				print(n - count0 - 1)
			else:
				print(max(B))
except:
	pass


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	di = dict(Counter(ls))
	ma = 0
	uni = 0
	for (i, j) in di.items():
		tem = j
		if tem != 1:
			if ma < tem:
				if ma != 0:
					uni += 1
				ma = tem
			else:
				uni += 1
		else:
			uni += 1
	if ma > uni:
		ma -= 1
		uni += 1
	if n == 1:
		print(0)
	elif ma == 0:
		print(1)
	else:
		print(min(ma, uni))


t = int(input())
for _ in range(t):
	n = int(input())
	count = {}
	max_same_size = 0
	array = map(int, input().split(' '))
	for a in array:
		if a not in count:
			count[a] = 1
		else:
			count[a] += 1
		max_same_size = max(max_same_size, count[a])
	if max_same_size > len(count):
		print(len(count))
	elif max_same_size == len(count):
		print(max_same_size - 1)
	else:
		print(max_same_size)


for t in range(int(input())):
	n = int(input())
	a = map(int, input().split())
	freq = [0 for i in range(n + 1)]
	for x in a:
		freq[x] += 1
	mode = 1
	for i in range(1, n + 1):
		if freq[i] > freq[mode]:
			mode = i
	unique_elements = sum(map(lambda x: 1 if x > 0 else 0, freq))
	print(max(min(unique_elements, freq[mode] - 1), min(unique_elements - 1, freq[mode])))


t = int(input())
for _ in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	d = {}
	s = len(set(b))
	if len(b) < 2:
		print(0)
		continue
	for i in b:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	p = max(d.values())
	if p > s:
		print(s)
	elif p == s:
		print(s - 1)
	else:
		print(p)


def main():
	t = int(input())
	while t:
		t = t - 1
		n = int(input())
		li = list(map(int, input().strip().split()))
		dick = {}
		for x in li:
			dick.setdefault(x, 0)
			dick[x] = dick[x] + 1
		mx = max(dick.values())
		if len(li) < 2:
			print(0)
		elif len(dick) > mx:
			print(mx)
		elif len(dick) == mx:
			print(mx - 1)
		else:
			print(len(dick))
main()


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = Counter(l)
	v = list(d.values())
	h = min(n // 2, max(v))
	if len(d) < max(v):
		print(min(len(d), h))
	else:
		print(min(len(d) - 1, h))


from collections import Counter
t = int(input())
for z in range(t):
	n = int(input())
	l = [int(i) for i in input().split()][:n]
	d = dict(Counter(l))
	m = max(d.values())
	d = len(set(l))
	if d > m:
		print(m)
	elif m == d:
		print(d - 1)
	else:
		print(d)


n = int(input())
for i in range(n):
	di = {}
	nn = int(input())
	l = list(map(int, input().split()))
	for i in l:
		if i in di:
			di[i] += 1
		else:
			di[i] = 1
	val = list(di.values())
	val.sort()
	a = val[-1]
	del val[-1]
	if nn == 1:
		print(0)
	elif nn == 2:
		print(1)
	elif len(val) > a:
		print(a)
	elif a - len(val) > 1:
		print(len(val) + 1)
	else:
		print(len(val))


import sys
from collections import Counter

def solve(A):
	c = Counter(A)
	(max_count, max_letter) = (0, None)
	for (letter, count) in c.items():
		if count > max_count:
			(max_letter, max_count) = (letter, count)
	del c[max_letter]
	team_a = set(c)
	if max_count >= len(team_a) + 2:
		return len(team_a) + 1
	else:
		return min(max_count, len(team_a))
t = int(input())
for _ in range(t):
	n = int(input())
	A = [int(x) for x in sys.stdin.readline().split(' ')]
	print(solve(A))


t = int(input())
for test in range(t):
	n = int(input())
	students = {}
	cnt1 = 0
	cnt2 = 1
	arr = input().split()
	for a in arr:
		a = int(a)
		if a in students.keys():
			students[a] += 1
			if students[a] > cnt2:
				cnt2 = students[a]
		else:
			students[a] = 1
			cnt1 += 1
	res = max(min(cnt1, cnt2 - 1), min(cnt1 - 1, cnt2))
	print(res)


T = int(input())
t = 0
while t < T:
	N = int(input())
	L = list(map(int, input().split()))
	L.sort()
	a = 1
	b = 0
	c = 1
	n = 0
	while n < N - 1:
		if L[n] != L[n + 1]:
			a = a + 1
			c = 1
		else:
			c = c + 1
		if c > b:
			b = c
		n += 1
	if a > b:
		print(b)
	if a == b:
		print(a - 1)
	if a < b:
		print(a)
	t += 1


t = int(input())
a = []
for i in range(t):
	b = []
	b.append(int(input()))
	b.append(list(map(int, input().split())))
	a.append(b)
for i in range(t):
	c = list(set(a[i][1]))
	m = 0
	d = [0] * (max(c) + 1)
	for el in a[i][1]:
		d[el] += 1
	m = max(d)
	if m > len(c):
		m = len(c)
	elif m < len(c):
		m = m
	else:
		m -= 1
	print(m)


from sys import stdin

def solve(n, a):
	d = {}
	h = len(set(a))
	for i in range(n):
		if a[i] in d:
			d[a[i]] += 1
		else:
			d[a[i]] = 1
	idx = -1
	m = -1
	for i in d.keys():
		if d[i] > m:
			m = d[i]
			idx = i
	if m == 1:
		if n == 1:
			print(0)
		else:
			print(1)
		return
	print(max(min(m, h - 1), min(m - 1, h)))

def main():
	inp = stdin.readline
	for _ in range(int(inp())):
		n = int(inp())
		a = list(map(int, inp().split()))
		solve(n, a)
main()


import sys
t = int(sys.stdin.readline())
ans_arr = []
for i in range(t):
	n = int(sys.stdin.readline())
	a = [int(x) for x in sys.stdin.readline().split()]
	d = {}
	dist = 0
	for w in range(n):
		if a[w] not in d:
			d[a[w]] = 1
			dist += 1
		else:
			d[a[w]] += 1
	ans = 0
	for k in d:
		ans = max(ans, min(dist, d[k] - 1))
		ans = max(ans, min(dist - 1, d[k]))
	ans_arr.append(str(ans))
print('\n'.join(ans_arr))


from collections import Counter

def solve(n, a):
	a_counter = Counter(a)
	first_max = len(a_counter)
	second_max = a_counter.most_common(1)[0][1]
	if second_max == first_max:
		return second_max - 1
	else:
		return min(first_max, second_max)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		result = solve(n, a)
		print(result)
	return 0
main()


def solution():
	cnt = 0
	n = int(input())
	a = [0] * (n + 2)
	l = list(map(int, input().split()))
	x = len(set(l))
	if n == 1:
		print(0)
		return
	for i in range(n):
		a[l[i]] += 1
	y = max(a)
	for i in range(1, len(a)):
		if a[i] > 1:
			cnt += 1
	if y < x:
		print(y)
	elif x == y:
		print(x - 1)
	else:
		print(x)
for _ in range(int(input())):
	solution()


from collections import Counter

def Sol(arr, n):
	data = Counter(arr)
	diff = len(data)
	m = data.most_common(1)[0][1]
	if diff > m:
		print(m)
	elif m == diff:
		print(m - 1)
	else:
		print(diff)
	return
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	Sol(arr, n)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	Skills = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		continue
	c = Counter(Skills)
	maximum = max(c.values())
	Chisla = set(Skills)
	print(max(min(len(Chisla) - 1, maximum), min(len(Chisla), maximum - 1)))


def solve(l, n):
	t = max(l)
	return max(min(n - 1, t), min(n, t - 1))
T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().strip().split()))
	d = {}
	for item in a:
		if item not in d:
			d[item] = 1
		else:
			d[item] += 1
	l = sorted(d.values())
	print(solve(l, len(l)))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	diff = len(set(a))
	s = Counter(a)
	maxi = max(s.values())
	print(max(min(diff, maxi - 1), min(diff - 1, maxi)))


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	best = 0
	uq = len(c)
	for (k, v) in c.items():
		if v >= uq - 1:
			best = max(best, uq - 1)
		else:
			best = max(best, v)
		if v - 1 > 0 and v - 1 >= uq:
			best = max(best, uq)
	print(best)


t = int(input())
d = dict()
maxim = dif = 0
for o in range(t):
	n = int(input())
	if n > 1:
		maxim = 1
	a = list(map(int, input().split()))
	for i in a:
		if i in d:
			d[i] += 1
			if d[i] > maxim:
				maxim = d[i]
		else:
			d[i] = 1
			dif += 1
	dif -= 1
	if maxim > n // 2 + 1:
		print(dif + 1)
	elif maxim > dif:
		print(dif + 1 if not maxim == dif + 1 else dif)
	else:
		print(maxim)
	maxim = dif = 0
	d = dict()


from collections import defaultdict
t = int(input())
for i in range(t):
	N = int(input())
	skill = defaultdict(int)
	distinct = set()
	students = [int(x) for x in input().split()]
	for s in students:
		skill[s] += 1
		distinct.add(s)
	max_same = max(students, key=lambda x: skill[x])
	max_same = skill[max_same]
	num_distinct = len(distinct) - 1
	if num_distinct >= max_same:
		print(max_same)
	elif max_same == num_distinct + 1:
		print(num_distinct)
	else:
		print(num_distinct + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	skills = list(map(int, input().split()))
	diff_sk = list(set(skills))
	dict_counts = {}
	for el in diff_sk:
		dict_counts[el] = 0
	for el in skills:
		dict_counts[el] += 1
	max_sim = max(list(dict_counts.values()))
	max_diff = len(diff_sk)
	if max_diff > max_sim:
		print(max_sim)
	elif max_diff == max_sim:
		print(max_sim - 1)
	else:
		print(max_diff)


import sys
from collections import Counter as counter
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	carr = counter(arr)
	ans = 0
	mostcnt = carr.most_common()[0][1]
	gou = len(carr)
	if gou == 1 and mostcnt > 1:
		print(gou)
	elif gou == 1:
		print(0)
	elif mostcnt - 1 >= gou:
		print(gou)
	elif mostcnt == gou:
		print(gou - 1)
	else:
		print(mostcnt)


from collections import defaultdict
import sys
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	if n == 1:
		print(0)
	else:
		pop = [0 for i in range(10)]
		poop = 0
		for i in l:
			d[i] += 1
		m = -sys.maxsize
		for i in d:
			cid = [0 for i in range(10)]
			poop += 1
			if d[i] > m:
				m = d[i]
		if m > len(d) - 1:
			if poop < 4:
				t = 1
			if m - 1 >= len(d):
				poop += 1
				print(len(d))
			else:
				print(len(d) - 1)
		else:
			print(m)


from collections import Counter

def solve(students):
	freq = Counter(students)
	(most_freq_sk, most_freq_num) = freq.most_common(1)[0]
	freq[most_freq_sk] = 0
	freq = +freq
	lf = len(freq)
	if lf >= most_freq_num:
		return most_freq_num
	elif most_freq_num - lf > 1:
		return lf + 1
	else:
		return lf
num_inputs = int(input())
for _ in range(num_inputs):
	n = int(input())
	students = map(int, input().split())
	print(solve(students))


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def case():
	n = I()
	a = li()
	c = Counter(a)
	ans = []
	for i in c:
		ans.append([c[i], i])
	ans.sort(reverse=True)
	p = ans[0][0]
	d = len(c) - 1
	if n == 1:
		print(0)
		return
	if len(c) == 1:
		print(1)
		return
	if p >= d + 2:
		print(min(p, d) + 1)
	else:
		print(min(p, d))
for _ in range(int(input())):
	case()


t = int(input())
for i in range(t):
	n = int(input())
	numbers = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	counter = {}
	distinct_count = 0
	for number in numbers:
		if number in counter:
			counter[number] += 1
		else:
			counter[number] = 1
			distinct_count += 1
	max_team_length = 1
	for (number, count) in counter.items():
		if count > distinct_count:
			max_team_length = max(max_team_length, distinct_count)
		if count == distinct_count:
			max_team_length = max(max_team_length, distinct_count - 1)
		if count < distinct_count:
			max_team_length = max(max_team_length, count)
	print(max_team_length)


from collections import Counter
ans = []
for i in range(int(input())):
	n = int(input())
	li = [int(i) for i in input().split()]
	if n is 1:
		ans.append(0)
	else:
		x = max(Counter(li).values())
		if x > len(set(li)) - 1:
			unique = len(set(li)) - 1
			unique += 1
			x -= 1
			if unique > x:
				ans.append(x)
			else:
				ans.append(unique)
		else:
			ans.append(x)
for i in ans:
	print(i)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter

def solution(n, arr):
	counts = Counter(arr)
	unique = len(counts)
	max_cnt = counts.most_common()[0][1]
	write(max(min(unique, max_cnt - 1), min(unique - 1, max_cnt)))

def main():
	for _ in range(r_int()):
		n = r_int()
		arr = r_array()
		solution(n, arr)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(end)

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

def r_int():
	return int(input())
main()


x = int(input())
for i in range(x):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	j = len(s)
	k = n // 2

	def mostFrequent(arr, n):
		Hash = dict()
		for i in range(n):
			if arr[i] in Hash.keys():
				Hash[arr[i]] += 1
			else:
				Hash[arr[i]] = 1
		max_count = 0
		for i in Hash:
			if max_count < Hash[i]:
				max_count = Hash[i]
		return max_count
	t = mostFrequent(a, n)
	if t == j:
		print(len(s) - 1)
	else:
		print(min(j, t, k))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	dct = defaultdict(int)
	for el in A:
		dct[el] += 1
	ans = 1
	for (k, v) in dct.items():
		ans = max(ans, min(v, len(dct) - 1))
		ans = max(ans, min(v - 1, len(dct)))
	print(ans)


T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	A = sorted(A)
	dif = 0
	sim = 0
	i = 0
	while i < N:
		s = 1
		while i < N - 1 and A[i] == A[i + 1]:
			s += 1
			i += 1
		sim = max(sim, s)
		dif += 1
		i += 1
	result = 0
	if dif == sim:
		result = dif - 1
	else:
		result = min(sim, dif)
	print(result)


import math
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = min(a)
	count = [0] * (max(a) - x + 1)
	for j in range(0, n):
		count[a[j] - x] = count[a[j] - x] + 1
	y = max(count)
	w = len(set(a)) - 1
	if y > w:
		if y - w < 2:
			print(w)
		else:
			print(w + 1)
	else:
		print(y)


t = int(input())
from collections import Counter
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		c = Counter(arr)
		max_value = max(c.values())
		if max_value == 1:
			print(1)
		elif len(c) - 1 >= max_value:
			print(max_value)
		elif len(c) - 1 == max_value - 1:
			print(len(c) - 1)
		else:
			print(len(c))


from collections import Counter
for _ in range(int(input())):
	length = int(input())
	arr = [int(x) for x in input().split()]
	nums = len(set(arr))
	cnt = Counter(arr)
	ans = 0
	for skill in cnt.most_common(nums):
		of_skill = skill[1]
		if of_skill < ans:
			break
		if of_skill < nums:
			ans = of_skill
			break
		elif of_skill == nums:
			ans = max(ans, of_skill - 1)
		else:
			ans = max(ans, nums)
	print(ans)


from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect

def solve(inf):
	n = inf
	a = list(map(int, input().split()))
	count = Counter(a)
	totKeys = len(count)
	ans = 0
	for key in count:
		ans = max(ans, min(count[key], totKeys - 1))
		ans = max(ans, min(count[key] - 1, totKeys))
	return ans
for _ in range(int(input())):
	inf = list(map(int, input().split()))
	print(solve(inf))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	x = len(d)
	maxi = max(d.values())
	if x == maxi:
		print(x - 1)
	else:
		print(min(x, maxi))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	f = [0] * (n + 1)
	for i in range(n):
		f[a[i]] += 1
	diff = sum((1 for i in range(n + 1) if f[i] != 0))
	ma = max(f)
	print(max(0, max(min(diff - 1, ma), min(diff, ma - 1))))


import sys

def readlines(type=int):
	return list(map(type, sys.stdin.readline().split()))

def read(type=int):
	return type(sys.stdin.readline())
joint = lambda it, sep=' ': sep.join([str(i) if type(i) != list else sep.join(map(str, i)) for i in it])

def solve():
	read()
	nums = readlines()
	numdic = dict()
	for num in nums:
		if num not in numdic:
			numdic[num] = 1
		else:
			numdic[num] += 1
	maxonum = numdic[max(numdic, key=numdic.get)]
	rest = len(numdic) - 1
	if maxonum <= rest:
		return maxonum
	elif maxonum == rest + 1:
		return rest
	elif maxonum > rest + 1:
		return rest + 1

def main():
	print(joint([solve() for x in range(read())], '\n'))
main()


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	d = {}
	for i in a:
		try:
			if d[i] != -1:
				pass
		except:
			d[i] = 1
	l = []
	c = 0
	x = a[0]
	for i in a:
		if i == x:
			c += 1
		else:
			l.append(c)
			c = 1
			x = i
	l.append(c)
	l.sort(reverse=True)
	ln = len(d) - 1
	for i in l:
		if i == ln or i == ln + 1:
			print(ln)
			break
		if i > ln + 1:
			print(ln + 1)
			break
		if ln > i:
			print(i)
			break


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	ans = 0
	s = {}
	for skill in arr:
		if skill not in s:
			s[skill] = 0
		s[skill] = s[skill] + 1
	max_same = 0
	for skill in s:
		if max_same < s[skill]:
			max_same = s[skill]
	max_diff = len(s)
	if max_diff > max_same:
		print(max_same)
	elif max_same > max_diff:
		print(min(max_same - 1, max_diff))
	else:
		print(max_same - 1)


import sys
from collections import Counter
cases = int(input())
for _ in range(cases):
	n = int(input())
	c = list(map(int, input().split()))
	d = Counter(c).most_common()
	index = 0
	l1 = []
	l2 = []
	for (key, values) in d:
		if index == 0:
			for i in range(values):
				l2.append(key)
			index += 1
			continue
		l1.append(key)
		index += 1
	r1 = len(l1)
	r2 = len(l2)
	l1 = min(r1, r2)
	l2 = min(r1 + 1, r2 - 1)
	print(max(l1, l2))


from collections import Counter

def coun(n, arr):
	d = Counter(arr)
	if n < 2:
		return 0
	maxi = 0
	unique = len(d)
	for item in d:
		if d[item] > unique:
			maxi = max(maxi, min(unique, d[item]))
		else:
			maxi = max(maxi, min(unique - 1, d[item]))
	return maxi
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	res = coun(n, arr)
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	s = {}
	for i in arr:
		if i in s:
			s[i] = s[i] + 1
		else:
			s[i] = 1
	diff = len(s) - 1
	x = max(s, key=s.get)
	m = s[x]
	if m <= diff:
		print(m)
	elif abs(m - diff) == 1:
		print(min(m, diff))
	else:
		print(min(m, diff) + 1)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = defaultdict(int)
	freq = {}
	for i in arr:
		d[i] += 1
	for i in d:
		freq[d[i]] = i
	diff = len(list(set(arr)))
	low = 0
	high = n - 1
	while low < high:
		mid = (high + low) // 2
		f = 0
		for i in freq:
			if i > mid:
				if diff >= mid:
					f = 1
				break
			elif i == mid:
				if diff - 1 >= mid:
					f = 1
					break
		if f == 0:
			high = mid
		else:
			low = mid
		if high - low == 1:
			break
	ans = -1
	for i in freq:
		if i > high:
			if diff >= high:
				ans = high
			break
		elif i == high:
			if diff - 1 >= high:
				ans = high
				break
	if ans != -1:
		print(ans)
	else:
		print(low)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	else:
		d = defaultdict(int)
		for i in ar:
			d[i] += 1
		maxi = 0
		for keys in d:
			maxi = max(d[keys], maxi)
		k = len(d)
		if maxi > k:
			print(k)
		if maxi == k:
			print(k - 1)
		if maxi < k:
			print(maxi)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		k = Counter(x)
		p = k.values()
		kk = max(p)
		if int(kk) > len(set(x)):
			print(min(int(kk), len(set(x))))
		else:
			print(min(int(kk), len(set(x)) - 1))


from collections import Counter
T = int(input())
for i in range(T):
	N = int(input())
	l = list(map(int, input().split()))
	c = Counter(l)
	unique = 0
	ma = 0
	for val in c.values():
		unique += 1
		if ma < val:
			ma = val
	if unique > 1 and ma == 1:
		print(1)
	else:
		if unique > ma:
			unique -= 1
		else:
			ma -= 1
		print(min(unique, ma))


def f(arr, n):
	maxFreq = 1
	hash = {}
	for i in range(n):
		if arr[i] not in hash:
			hash[arr[i]] = 1
		else:
			hash[arr[i]] += 1
		maxFreq = max(maxFreq, hash[arr[i]])
	size = len(hash)
	m1 = min(maxFreq, size - 1)
	m2 = min(maxFreq - 1, size)
	return max(m1, m2)
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(f(arr, n))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	s = Counter(list(map(int, input().split())))
	max_size = s.most_common()[0][1]
	if n <= 1:
		print(0)
	elif max_size != 1:
		lst = s.most_common()[::-1]
		i = len(s) - 1
		if max_size == i:
			print(i)
		elif max_size > i:
			print(i + 1 if max_size > i + 1 else i)
		else:
			print(max_size)
	else:
		print(1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	maxi = 0
	value = 0
	unique = []
	li = list(set(arr))
	if n == 1:
		print(0)
	elif len(li) == n:
		print(1)
	else:
		for i in range(n):
			if arr[i] in d.keys():
				d[arr[i]] = d[arr[i]] + 1
			else:
				d[arr[i]] = 1
		for i in d.keys():
			unique.append(i)
			d[i] = d[i] - 1
		for i in d.keys():
			if maxi < d[i]:
				maxi = d[i]
				value = i
		if maxi >= len(unique):
			print(len(unique))
		elif len(unique) - maxi > 1:
			print(maxi + 1)
		elif len(unique) - maxi == 1:
			print(maxi)


test_num = int(input())
tests = []
for t in range(test_num):
	lenght = int(input())
	tests.append([int(x) for x in input().split()])
for test in tests:
	if len(test) == 1:
		print(0)
		continue
	frequencies = {}
	for item in test:
		if item in frequencies:
			frequencies[item] += 1
		else:
			frequencies[item] = 1
	maximum = max(frequencies.values())
	if maximum > len(frequencies):
		print(len(frequencies))
		continue
	elif maximum < len(frequencies):
		print(maximum)
		continue
	elif maximum == len(frequencies):
		print(maximum - 1)


def foo():
	n = int(input())
	lis = list(map(int, input().split()))
	d = {}
	for i in lis:
		d[i] = d.get(i, 0) + 1
	a = max(d.values())
	b = len(d.values()) - 1
	if a > b + 1:
		print(b + 1)
	else:
		print(min(a, b))
t = int(input())
for i in range(t):
	foo()


from collections import defaultdict

def mode(arr):
	value_counts = defaultdict(int)
	for item in arr:
		value_counts[item] = value_counts[item] + 1
	y = max(value_counts, key=value_counts.get)
	return value_counts[y]
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		u = len(set(l))
		c = mode(l)
		if u == c:
			print(u - 1)
		elif u > c:
			print(c)
		else:
			print(u)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	diff = len(set(a))
	cnt = [0 for i in range(n + 1)]
	for i in a:
		cnt[i] += 1
	Mcnt = max(cnt)
	print(max(min(Mcnt, diff - 1), min(diff, Mcnt - 1)))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	M = len(set(a))
	seen = {}
	for x in a:
		if x not in seen:
			seen[x] = 1
		else:
			seen[x] += 1
	v = max(seen, key=lambda x: seen[x])
	N = seen[v]
	if M < N:
		print(M)
	elif M == N:
		print(M - 1)
	else:
		print(N)


import math
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = map(int, input().split())
	c = Counter(a)
	same = max(c.values())
	distinct = len(c.keys()) - 1
	p1 = min(same, distinct)
	p2 = min(same - 1, distinct + 1)
	print(max(p1, p2))


from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = dict(Counter(map(int, input().split())))
	if n == 1:
		print(0)
		continue
	maxi = 0
	for v in a.values():
		if v > maxi:
			maxi = v
	print(max(min(len(a) - 1, maxi), min(len(a), maxi - 1)))


t = int(input())
while t > 0:
	n = int(input())
	ll = [int(x) for x in input().split()]
	dist = len(set(ll))
	maxsame = 1
	ll.sort()
	same = 1
	for i in range(len(ll) - 1):
		j = i + 1
		if ll[j] != ll[j - 1]:
			maxsame = max(same, maxsame)
			same = 1
		else:
			same += 1
	maxsame = max(same, maxsame)
	print(max(min(maxsame - 1, dist), min(maxsame, dist - 1)))
	t -= 1


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	dic = Counter(map(int, input().split()))
	un = len(dic)
	maxrep = max(dic.values())
	if maxrep < un:
		print(maxrep)
	elif maxrep == un:
		print(maxrep - 1)
	else:
		print(un)


import sys
input = sys.stdin.readline
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	s = len(set(w))
	w = max(Counter(w).values())
	print(min(w, s) - (w == s))


import math
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	d1 = sorted(d.items(), key=lambda kv: kv[1], reverse=True)
	b = len(d1)
	if n == 1:
		print(0)
	else:
		s = []
		for i in range(b):
			x = min(d1[i][1], b - 1)
			y = min(d1[i][1] - 1, b)
			s.append(max(x, y))
		print(max(s))


n = int(input())
for _ in range(n):
	t = int(input())
	st = [int(i) for i in input().split()]
	st_set = set(st)
	d = {}
	for i in st:
		d[i] = d.get(i, 0) + 1
	max_sim = max(d.values())
	set_len = len(st_set) - 1
	if max_sim > set_len:
		print(min(max_sim - 1, set_len + 1))
	else:
		print(min(max_sim, set_len))


test = int(input())
i = 1
while i <= test:
	n = int(input())
	skill = list(map(int, input().split()))
	dis = 0
	maxi = 0
	count = [0 for i in range(n + 1)]
	for j in range(n):
		if count[skill[j]] == 0:
			dis += 1
		count[skill[j]] += 1
	for j in count:
		maxi = max(maxi, j)
	if dis == maxi:
		print(dis - 1)
	else:
		print(min(dis, maxi))
	i += 1


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		arry = list(map(int, input().split()))
		dic = {}
		for ele in arry:
			freq = dic.get(ele, 0)
			dic[ele] = freq + 1
		maxbro = max(dic, key=dic.get)
		manum = dic[maxbro]
		other = len(dic) - 1
		if manum > other:
			yield min((manum + other) // 2, other + 1)
		else:
			yield manum
t = int(input())
ans = gift()
print(*ans, sep='\n')


t = int(input())
for _ in range(t):
	n = int(input())
	vet = list(map(int, input().split()))
	res = {}
	for i in vet:
		if i in res:
			res[i] += 1
		else:
			res[i] = 1
	m = 0
	for i in res:
		if m < res[i]:
			m = res[i]
	a = len(res) - 1
	if m - a >= 2:
		a += 1
		m -= 1
	print(min(a, m))


from collections import Counter, OrderedDict
from operator import itemgetter
t = int(input())
ans = [0] * t
for i in range(t):
	n = int(input())
	a = map(int, input().split(' '))
	if n > 1:
		d = Counter(a)
		d = OrderedDict(sorted(d.items(), key=itemgetter(1), reverse=True))
		d = list(d.items())
		maxsame = d[0][1]
		dif = len(d) - 1
		if maxsame > dif:
			ans[i] = min(maxsame - 1, dif + 1)
		else:
			ans[i] = maxsame
print(*ans, sep='\n')


for t in range(int(input())):
	n = int(input())
	a = [0] * (n + 5)
	la = list(map(int, input().split()))
	for i in la:
		a[i] += 1
	mx = 0
	ix = 0
	for i in range(n + 5):
		if a[i] > mx:
			mx = a[i]
			ix = i
	s = 0
	for i in range(n + 5):
		if i != ix:
			if a[i] != 0:
				a[i] = 1
			else:
				a[i] = 0
			s += a[i]
	x = max(min(s, mx), min(s + 1, mx - 1))
	print(x)


from collections import *
for _ in range(int(input())):
	n = int(input())
	l = [*map(int, input().split())]
	if n == 1:
		print(0)
		continue
	dic = Counter(l)
	cnt = 0
	maxi = 1
	arr = []
	for i in dic:
		if dic[i] == 1:
			cnt += 1
		else:
			maxi = max(maxi, dic[i])
			arr.append(dic[i])
	x = len(arr)
	cnt += len(arr) - 1
	if maxi > cnt + 1:
		print(cnt + 1)
	else:
		print(min(cnt, maxi))


N = int(input())
from collections import Counter
for i in range(N):
	A = int(input())
	l = list(map(int, input().split()))
	b = Counter(l)
	ma = 0
	for j in b:
		ma = max(ma, b[j])
	t = len(b)
	if t == 1 and ma < 2:
		print(0)
	elif ma == t:
		print(ma - 1)
	elif ma < t:
		print(ma)
	else:
		print(t)


for t in range(int(input())):
	n = int(input())
	abc = list(map(int, input().split()))
	repeat = []
	if n > 1:
		repeat.append(1)
	else:
		repeat.append(0)
	abc.sort()
	abc.append(-1)
	c = 0
	k = n
	for i in range(1, n):
		if abc[i] == abc[i - 1]:
			c += 1
			k -= 1
			if abc[i + 1] != abc[i]:
				repeat.append(c)
				c = 0
	if k - max(repeat) >= 2 and len(repeat) != 1:
		print(min(max(repeat) + 1, k))
	else:
		print(min(max(repeat), k))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ls = set(l)
	ans = [0] * (n + 1)
	for i in l:
		ans[i] += 1
	x = len(ls)
	ma = max(ans)
	if ma == x:
		print(ma - 1)
	else:
		print(min(ma, x))


from collections import Counter
t = int(input())
for case in range(1, t + 1):
	n = int(input())
	counter = Counter(input().split(' '))
	max_dup = max((v for v in counter.values()))
	num_dist = len(counter)
	if max_dup > num_dist:
		print(num_dist)
	elif max_dup == num_dist:
		print(max_dup - 1)
	else:
		print(max_dup)


t = int(input())
for i in range(t):
	ans = 0
	n = int(input())
	a = input()
	a = a.split(' ')
	freq = {}
	for j in a:
		if j in freq:
			freq[j] += 1
		else:
			freq[j] = 1
	max_freq = max(freq.values())
	distinct_el = len(freq.keys())
	if n == 1:
		ans = 0
	elif max_freq == distinct_el:
		ans = max_freq - 1
	else:
		ans = min(max_freq, distinct_el)
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	v = list(map(int, input().strip().split()))[0:n]
	x = 0
	y = 0
	w = set(v)
	x = len(w)
	z = 1
	i = 1
	v.sort()
	while i < n:
		if v[i] == v[i - 1]:
			z += 1
		else:
			y = max(y, z)
			z = 1
		i += 1
	y = max(y, z)
	if y == x:
		print(y - 1)
	else:
		print(min(x, y))


from collections import Counter
t = int(input())
for l in range(t):
	n = int(input())
	li = Counter(map(int, input().split()))
	k = max(li.values())
	m = li.keys()
	if k >= len(m):
		k = min(k - 1, len(m))
	print(k)


def amount(know):
	un_kn = set(know)
	un = len(un_kn)
	rep_am = [0] * (n + 1)
	for elem in know:
		rep_am[elem] += 1
	rep1 = max(rep_am)
	if rep1 > un:
		rep = un
	elif rep1 == un:
		rep = un - 1
	else:
		rep = rep1
	return rep
t = int(input())
for _ in range(t):
	n = int(input())
	know = [int(x) for x in input().split(' ')]
	print(amount(know))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = defaultdict(int)
	for i in range(n):
		d[l[i]] += 1
	l = sorted(d.items(), key=lambda x: x[1], reverse=True)
	count = len(l)
	m = l[0][1]
	print(max(min(count - 1, m), min(count, m - 1)))


from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = list(Counter(a).values())
	c.sort()
	y = len(c) - 1
	if c[-1] != 1:
		x = max(c)
	else:
		x = 0
	o = min(x, y)
	if o == n // 2 or (x == y and x < n // 2):
		print(o)
	elif x == 0:
		print(1)
	elif x < y:
		print(x)
	elif x - y == 1:
		print(y)
	elif x - y > 1:
		print(y + 1)


from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 1000010
MOD = 10 ** 9 + 7
spf = [i for i in range(MAXN)]
spf[0] = spf[1] = -1

def sieve():
	for i in range(2, MAXN, 2):
		spf[i] = 2
	for i in range(3, int(MAXN ** 0.5) + 1):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def fib(n, m):
	if n == 0:
		return [0, 1]
	else:
		(a, b) = fib(n // 2)
		c = a % m * (b % m * 2 - a % m) % m
		d = a % m * (a % m) % m + b % m * b % m % m
		if n % 2 == 0:
			return [c, d]
		else:
			return [d, c + d]

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN(x=' '):
	return list(map(int, sys.stdin.readline().strip().split(x)))

def ncr(n, r):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % MOD
		den = den * (i + 1) % MOD
	return num * pow(den, MOD - 2, MOD) % MOD

def flush():
	return sys.stdout.flush()
for _ in range(int(input())):
	n = int(input())
	a = arrIN()
	if n == 1:
		print(0)
		continue
	s = set(a)
	c = Counter(a)
	l = len(s)
	mx = 1
	for i in s:
		if c[i] >= l - 1:
			if c[i] >= l + 1:
				mx = max(mx, l)
			else:
				mx = max(mx, l - 1)
		else:
			mx = max(mx, c[i])
	print(mx)


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for elem in a:
		if elem not in d:
			d[elem] = 1
		else:
			d[elem] += 1
	max_c = 0
	for elem in d:
		if d[elem] > max_c:
			max_c = d[elem]
	l = len(d)
	if n < 2:
		print(0)
	elif l == max_c:
		print(min(max_c, l) - 1)
	else:
		print(min(max_c, l))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	check = {}
	if n < 2:
		print(0)
		continue
	for x in a:
		if check.get(x) == None:
			check[x] = 1
		else:
			check[x] += 1
	countcheck = list(check.values())
	countcheck = sorted(countcheck, reverse=True)
	same = countcheck[0]
	unique = len(countcheck) - 1
	if same > unique:
		if same - 1 > unique:
			print(unique + 1)
		else:
			print(unique)
	else:
		print(max(same, 1))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	now = [0] * n
	for elem in a:
		now[elem - 1] += 1
	ans1 = max(now)
	ans2 = 0
	for elem in now:
		if elem != 0:
			ans2 += 1
	if ans2 == ans1:
		print(ans2 - 1)
	else:
		print(min(ans1, ans2))


def sol1(n):
	f = 1
	u = 1
	mf = 0
	for i in range(1, len(n)):
		if n[i - 1] == n[i]:
			f = f + 1
			if f > mf:
				mf = f
		else:
			u = u + 1
			f = 1
	return (mf, u)

def sol(l, n):
	if n == 1:
		return 0
	l.sort()
	(mf, sl) = sol1(l)
	if sl == n:
		return 1
	elif sl == mf:
		return mf - 1
	elif sl > mf:
		return mf
	else:
		return sl
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(sol(l, n))


t = int(input())
for case in range(t):
	f = {}
	n = int(input())
	a = [int(x) for x in input().split()]
	options = len(set(a))
	for p in a:
		if p not in f:
			f[p] = 0
		f[p] += 1
	same_team = max(f.values())
	x = max(min(same_team - 1, options), min(same_team, options - 1))
	print(x)


t = int(input())
for _ in range(t):
	n = int(input())
	xs = [int(x) for x in input().split()]
	freq = {}
	for it in xs:
		if it not in freq:
			freq[it] = 0
		freq[it] += 1
	mx = max(freq, key=lambda x: freq[x])
	rm = len(freq) - 1
	if rm >= xs.count(mx):
		print(xs.count(mx))
	elif rm + 1 == xs.count(mx):
		print(rm)
	else:
		print(rm + 1)


import sys
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = defaultdict(list)
	for i in a:
		try:
			d[i] += 1
		except:
			d[i] = 1
	l = len(d)
	m = -sys.maxsize
	for i in d:
		if m < d[i]:
			m = d[i]
	if m == l and m != 1:
		print(m - 1)
	elif m == l and m == 1:
		print(m - 1)
	else:
		print(min(m, l))


from collections import Counter as ct
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dst = len(set(a)) - 1
	dct = dict(ct(a))
	same = 0
	for key in ct(a).keys():
		same = max(dct[key], same)
	print(max(min(dst, same), min(same - 1, dst + 1)))


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		int(input())
		print(0)
	else:
		a = [int(x) for x in input().split()]
		s = set()
		m = {}
		for j in range(n):
			s.add(a[j])
			if a[j] in m:
				m[a[j]] += 1
			else:
				m[a[j]] = 1
		arr = list(m.values())
		arr.sort(reverse=True)
		b = len(s)
		c = len(arr)
		for j in range(c):
			if arr[j] > b:
				print(b)
			elif arr[j] == b:
				print(b - 1)
			elif arr[j] < b:
				print(arr[j])
			break


t = int(input())
for x in range(t):
	n = int(input())
	inp1 = list(map(int, input().split()))
	dic = {}
	for i in inp1:
		if i in dic.keys():
			dic[i] += 1
		else:
			dic[i] = 1
	distinct = len(dic)
	maxsame = max(dic, key=lambda x: dic[x])
	print(max(min(distinct, dic[maxsame] - 1), min(distinct - 1, dic[maxsame])))


from collections import *
for _ in range(int(input())):
	input()
	x = [int(x) for x in input().split()]
	comman = max(Counter(x).values())
	uncomman = len(set(x))
	if len(x) == 1:
		print(0)
	elif len(x) == len(set(x)):
		print(1)
	elif comman > uncomman:
		print(uncomman)
	elif uncomman > comman:
		print(comman)
	else:
		print(uncomman - 1)


import sys
input = sys.stdin.readline
from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(item) for item in input().split()]
	cnt = Counter(a)
	same_skill = []
	same_cnt = 0
	kind = len(cnt)
	for (key, val) in cnt.items():
		if val > same_cnt:
			same_cnt = val
			same_skill = [key]
		elif val == same_cnt:
			same_skill.append(key)
	if kind > same_cnt:
		print(same_cnt)
	elif kind == same_cnt:
		print(min(same_cnt - 1, kind))
	else:
		print(min(same_cnt, kind))


for _ in range(int(input())):
	d = {}
	n = int(input())
	l = list(map(int, input().split()))
	for i in l:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	l1 = []
	for i in d:
		l1.append([i, d[i]])
	l1.sort(key=lambda x: x[1], reverse=True)
	f = 0
	x = len(l1)
	if x == n:
		if x == 1:
			print(0)
		else:
			print(1)
	else:
		for i in range(x, 0, -1):
			if l1[0][1] > x:
				print(x)
				break
			elif x > l1[0][1]:
				print(l1[0][1])
				break
			else:
				x -= 1


for _ in range(int(input())):
	n = int(input())
	k = [0] * n
	for i in map(int, input().split()):
		k[i - 1] += 1
	p = max(k)
	o = n - k.count(0) - 1
	ans = min(p, o)
	ans = max(ans, min(p - 1, o + 1))
	print(ans)


for _ in range(int(input())):
	ma = 0
	n = int(input())
	d = {}
	a = list(map(int, input().split()))
	s = set(a)
	for i in a:
		d[i] = d.get(i, 0) + 1
	l = len(s)
	for i in d:
		k = d[i]
		if k > l:
			ma = max(ma, l)
		else:
			ma = max(ma, min(k, l - 1))
	print(ma)


for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	d = {}
	for i in arr:
		d[i] = d.get(i, 0) + 1
	print(max(min(len(d) - 1, max(list(d.values()))), min(len(d), max(list(d.values())) - 1)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		try:
			d[i] += 1
		except KeyError:
			d[i] = 1
	s = len(d)
	ans = 0
	for (i, j) in d.items():
		ans = max(min(s, j - 1), ans)
		ans = max(ans, min(s - 1, j))
	print(ans)


t = int(input())
for j in range(t):
	n = int(input())
	a = [int(_) for _ in input().split()]
	a.sort()
	b = []
	s_max = 0
	for i in range(n):
		if b == [] or a[i] == b[-1]:
			b.append(a[i])
		else:
			if len(b) > s_max:
				s_max = len(b)
			b = [a[i]]
	if len(b) > s_max:
		s_max = len(b)
	if n == 1:
		print(0)
	elif s_max == 1:
		print(1)
	else:
		z = len(set(a))
		j = 0
		if s_max == z:
			print(min(s_max - 1, z - 1))
		else:
			print(min(s_max, z))


t = int(input())
for xy in range(t):
	k = int(input())
	s = list(map(int, input().split()))
	lp = sorted(s)
	r = 0
	sp = 0
	bool = False
	slq = {lp[0]}
	for a in range(1, k):
		slq.add(lp[a])
		if lp[a] == lp[a - 1]:
			sp += 1
			bool = True
		elif bool:
			if sp + 1 > r:
				r = sp + 1
			sp = 0
			bool = False
	if bool:
		r = max(r, sp + 1)
	if len(slq) == k and len(slq) > 1:
		print(1)
	elif len(slq) > r or r > len(slq):
		print(min(len(slq), r))
	elif r == len(slq):
		print(r - 1)


def solve(a):
	d = {}
	for x in a:
		d[x] = d.get(x, 0) + 1
	mf = max((f for (x, f) in d.items()))
	return max(min(len(d) - 1, mf), min(len(d), mf - 1))
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	cc = Counter(l)
	val = cc.values()
	ma = max(val)
	rest = len(val) - 1
	if rest >= ma:
		print(ma)
	else:
		d = ma - rest
		if d == 1:
			print(rest)
		else:
			print(rest + 1)


def solve(i):
	d = {}
	for n in i:
		if n in d:
			d[n] += 1
		else:
			d[n] = 1
	g1 = max(list(d.values()))
	g2 = len(list(d.values()))
	print(max(min(g1 - 1, g2), min(g1, g2 - 1)))

def main():
	t = int(input())
	for _ in range(t):
		_ = input()
		i = [int(x) for x in input().split()]
		solve(i)
main()


import sys
input = sys.stdin.buffer.readline
import collections

def solve(n, arr):
	count = collections.Counter(arr)
	maxC = max(count.values())
	v1 = min(len(count) - 1, maxC)
	v2 = min(len(count), maxC - 1)
	return max(v1, v2)
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = solve(n, arr)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = [0] * (len(a) + 1)
	for x in a:
		cnt[x] += 1
	l = len(set(a))
	m = max(cnt)
	if l == m:
		print(m - 1)
	else:
		print(min(l, m))


t = int(input())
for i in range(t):
	n = int(input())
	l = sorted(list(map(int, input().split())))
	s = []
	x = 1
	if n == 1:
		print(0)
	else:
		for j in range(n - 1):
			if l[j] == l[j + 1]:
				x += 1
			else:
				s.append(x)
				x = 1
			if j == n - 2 and l[j] == l[j + 1]:
				s.append(x)
		m = max(s)
		an = len(set(l)) - 1
		al = min(m, an)
		if m - al > 1:
			al += 1
		print(al)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = dict()
	maxd = 1
	for i in l:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
			maxd = max(d[i], maxd)
	dis = len(set(l))
	if dis < maxd:
		print(dis)
	elif dis == maxd:
		print(dis - 1)
	else:
		print(maxd)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = len(set(a))
	if n == 1:
		print(0)
		continue
	if s == 1 or s == n:
		print(1)
		continue
	table = [0 for i in range(n + 1)]
	best = 0
	for i in a:
		table[i] += 1
		best = max(best, table[i])
	ans = min(n // 2, max(min(s - 1, best), min(best - 1, s)))
	print(ans)


t = int(input())
for i in range(t):
	d = int(input())
	c = list(map(int, input().split()))
	f = {}
	for j in range(len(c)):
		if c[j] in f:
			f[c[j]] += 1
		else:
			f[c[j]] = 1
	maximum = max(f, key=f.get)
	maximum = f[maximum]
	unique = len(f) - 1
	if maximum <= unique:
		print(maximum)
	elif maximum - unique >= 2:
		print(unique + 1)
	else:
		print(unique)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	co = Counter(a)
	s = set(a)
	c = 0
	x = len(s) - 1
	for i in s:
		if min(x, co[i]) > c:
			c = min(x, co[i])
		if min(x + 1, co[i] - 1) > c:
			c = min(x + 1, co[i] - 1)
	print(c)


from collections import defaultdict
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	d = defaultdict(int)
	for a in A:
		d[a] += 1
	ans1 = min(max(d.values()), len(d) - 1)
	ans2 = min(max(d.values()) - 1, len(d))
	print(max(ans1, ans2))


from sys import stdin, stdout
from collections import Counter
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()

def main():
	T = int(rl())
	for _ in range(T):
		n = int(rl())
		A = [int(x) for x in rll()]
		F = Counter(A)
		Aset = set(A)
		num_distinct = len(Aset)
		largest_same = max((F[k] for k in F.keys()))
		(x, y) = (num_distinct - 1, largest_same)
		m = min(x, y)
		if x < y - 1:
			stdout.write(str(m + 1))
			stdout.write('\n')
		else:
			stdout.write(str(m))
			stdout.write('\n')
	stdout.close()
main()


import sys
input = sys.stdin.readline
from collections import Counter
Q = int(input())
Query = []
for _ in range(Q):
	N = int(input())
	A = list(map(int, input().split()))
	Query.append((N, A))
for (N, A) in Query:
	C = Counter(A)
	B = max(list(C.values()))
	ans = max(min(len(C) - 1, B), min(len(C), B - 1))
	print(ans)


n = int(input())
while n:
	n -= 1
	m = int(input())
	if m == 0:
		print(0)
		continue
	li = list(map(int, input().strip().split(' ')))
	d = dict()
	maxd = 0
	maxc = set()
	for x in li:
		if x in d.keys():
			d[x] += 1
		else:
			d[x] = 1
			maxd += 1
	for x in d:
		maxc.add(d[x])
	maxc = list(maxc)
	maxc.sort(reverse=True)
	flag = 0
	for x in maxc:
		if x == maxd:
			flag = 1
			break
		elif x > maxd:
			flag = 2
			break
	if flag == 1:
		print(maxd - 1)
	elif flag == 2:
		print(maxd)
	else:
		print(maxc[0])


t = int(input())
for test in range(t):
	n = int(input())
	arr = [0] * n
	something = list(map(int, input().split()))
	for i in range(n):
		arr[something[i] - 1] += 1
	maxval = arr[0]
	count = 0
	for i in range(n):
		if arr[i] != 0:
			count += 1
			if arr[i] > maxval:
				maxval = arr[i]
	if count > maxval:
		print(maxval)
	if count < maxval:
		print(count)
	if count == maxval:
		print(maxval - 1)


t = int(input())
for i in range(t):
	n = int(input())
	skills = input().split()
	diff = len(set(skills))
	same = dict()
	for skill in skills:
		skill = int(skill)
		if skill not in same:
			same[skill] = 1
		else:
			same[skill] += 1
	same = sorted(same.values(), reverse=True)[0]
	if diff == same:
		print(diff - 1)
	else:
		print(min([diff, same]))


from collections import Counter
import sys
input = sys.stdin.readline
t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	a = input().split()
	se = len(set(a))
	m = Counter(a).most_common(1)[0][1]
	ans.append(max(min(se, m - 1), min(se - 1, m)))
print('\n'.join(map(str, ans)))


from collections import defaultdict

def countofelements(a):
	b = defaultdict(int)
	for i in a:
		b[i] = b[i] + 1
	setval = b.values()
	return max(setval)
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	b = set(l)
	b = list(b)
	siz = len(b)
	maxcou = countofelements(l)
	if n == 1:
		ans = 0
	elif maxcou > siz:
		ans = siz
	elif siz > maxcou:
		ans = maxcou
	elif siz == maxcou:
		ans = siz - 1
	print(ans)


from collections import Counter
t = int(input())
for i in range(t):
	length = input()
	a = input().split(' ')
	a = [int(i) for i in a]
	b = dict(Counter(a))
	maxl = max(b.values())
	dif = len(b)
	if dif < maxl:
		print(dif)
	elif dif == maxl:
		print(dif - 1)
	else:
		print(maxl)


t = int(input())
for case in range(t):
	n = int(input())
	skill = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		hash_map = {}
		max_same = 1
		distinct = 0
		for i in range(n):
			if skill[i] in hash_map:
				hash_map[skill[i]] += 1
				max_same = max(max_same, hash_map[skill[i]])
			else:
				hash_map[skill[i]] = 1
				distinct += 1
		max_same -= 1
		if distinct - max_same > 1:
			print(max_same + 1)
		else:
			print(min(max_same, distinct))


for i in range(int(input())):
	n = int(input())
	s = list(map(str, input().split()))
	d = {i: 0 for i in s}
	for i in s:
		d[i] += 1
	b = len(d)
	a = 0
	for i in d:
		if d.get(i) > a:
			a = d.get(i)
	if a == b:
		print(b - 1)
	elif a > b:
		print(b)
	else:
		print(a)


from collections import Counter

def func(n, arr):
	if n == 1:
		print(0)
		return
	s = set(arr)
	if len(s) == 1:
		print(1)
		return
	a = Counter(arr)
	maxm = a.most_common()[0][1]
	rest = len(a) - 1
	if rest >= maxm:
		print(maxm)
		return
	if rest + 1 == maxm:
		print(rest)
		return
	print(rest + 1)
	return
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	func(n, arr)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	k = list(map(int, input().split()))
	maxcnt = max([i for i in Counter(k).values()])
	diff = len(list(set(k)))
	print(max(min(diff, maxcnt - 1), min(diff - 1, maxcnt)))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

def main():
	for _ in range(N()):
		n = N()
		arr = list(RL())
		c = Counter(arr)
		ma = max(c.values())
		al = len(c)
		ta = al - 1
		tb = ma
		print(max(min(ta, tb), min(ta + 1, tb - 1)))
main()


T = int(input())
for t in range(T):
	n = int(input())
	skills = list(map(int, input().split()))
	atr = {}
	for i in skills:
		if i not in atr:
			atr[i] = 1
		else:
			atr[i] += 1
	if n == 1:
		team_size = 0
	elif len(atr) == 1:
		team_size = 1
	else:
		team_size = 0
		for i in atr.keys():
			first_equal = atr[i] - 1
			first_non_equal = len(atr)
			first_possible = min(first_equal, first_non_equal)
			second_equal = atr[i]
			second_non_equal = len(atr) - 1
			second_possible = min(second_equal, second_non_equal)
			possible = max(first_possible, second_possible)
			if possible > team_size:
				team_size = possible
	print(team_size)


for j in range(0, int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	p = set(a)
	q = []
	s = 0
	for i in range(1, n):
		if a[i - 1] == a[i]:
			s = s + 1
		else:
			q.append(s + 1)
			s = 0
	q.append(s + 1)
	if len(p) == max(q):
		print(len(p) - 1)
	else:
		print(min(len(p), max(q)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		dic = {}
		for i in range(n):
			if l[i] in dic:
				dic[l[i]] += 1
			else:
				dic[l[i]] = 1
		s = 0
		b = 0
		for k in dic:
			s += 1
			b = max(dic[k], b)
		if s == b:
			print(s - 1)
		elif s < b:
			print(s)
		else:
			print(b)


t = int(input())
from collections import Counter
for i in range(1, t + 1):
	n = int(input())
	skills = [int(i) for i in input().split()]
	a = dict(Counter(skills))
	all_values = a.values()
	same = max(all_values)
	diff = len(a)
	if n == 1:
		print(0)
	elif same == diff:
		print(same - 1)
	else:
		print(min(same, diff))


def sam(a):
	maks = 0
	loc = 1
	for i in range(1, len(a)):
		if a[i] == a[i - 1]:
			loc += 1
		else:
			if loc > maks:
				maks = loc
			loc = 1
	if loc > maks:
		maks = loc
	return maks
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	dist = len(b)
	same = 0
	a.sort()
	same = sam(a)
	if dist == same:
		print(dist - 1)
	elif dist < same:
		print(dist)
	else:
		print(same)


from collections import Counter
t = int(input())
for ti in range(t):
	n = int(input())
	arr = map(int, input().split())
	(k, s) = (Counter(), set())
	isTwo = False
	for i in arr:
		k[i] = k[i] + 1
		s.update({i})
	mxcnt = k.most_common(1)[0][1]
	l = len(s)
	print(max(min(l - 1, mxcnt), min(l, mxcnt - 1)))


import collections
n = input()
for i in range(int(n)):
	size = int(input())
	st = input().split()
	coun = collections.Counter(st)
	ans = -1
	dd = dict(coun)
	it = sorted([item for item in dd.values()])
	cur = it[len(it) - 1]
	if len(it) < cur:
		ans = len(it)
	elif len(it) == cur:
		ans = len(it) - 1
	else:
		ans = cur
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	ai = list(map(int, input().split()))
	ai.sort()
	num = 1
	ans = 0
	for i in range(1, n):
		if ai[i] == ai[i - 1]:
			num += 1
		else:
			ans = max(ans, num)
			num = 1
	ans = max(ans, num)
	ai2 = list(set(ai))
	print(max(min(len(ai2) - 1, ans), min(len(ai2), ans - 1)))


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().strip().split()))[:n]
	m = dict()
	max_cnt = 0
	for i in range(len(l)):
		if l[i] in m:
			m[l[i]] = m[l[i]] + 1
		else:
			m[l[i]] = 1
		max_cnt = max(max_cnt, m[l[i]])
	if len(m) > max_cnt:
		print(max_cnt)
	elif len(m) < max_cnt:
		print(len(m))
	else:
		print(len(m) - 1)
	t -= 1


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = int(sys.stdin.readline())
	list_student = list(map(int, sys.stdin.readline().split()))
	list_student.sort()
	now_length = 1
	independent = 1
	max_length = 0
	for i in range(1, N):
		if list_student[i] == list_student[i - 1]:
			now_length += 1
		else:
			now_length = 1
			independent += 1
		max_length = max(now_length, max_length)
	print(max(min(max_length, independent - 1), min(max_length - 1, independent)))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	a = Counter(lst)
	maxcnt = max(a.values())
	diff = len(set(lst))
	ans = max(min(diff - 1, maxcnt), min(diff, maxcnt - 1))
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	skills = tuple([int(_) for _ in input().split()])
	hash_map_uh = {}
	for skill in skills:
		if skill in hash_map_uh:
			hash_map_uh[skill] += 1
		else:
			hash_map_uh[skill] = 1
	print(max(min(len(hash_map_uh.keys()) - 1, max(hash_map_uh.values())), min(len(hash_map_uh.keys()), max(hash_map_uh.values()) - 1)))


for _ in range(int(input())):
	n = [0 for x in range(int(input()))]
	d = 0
	for x in input().split():
		i = int(x) - 1
		n[i] += 1
		d += n[i] == 1
	if max(n) < d:
		print(max(n))
	elif d == max(n):
		print(d - 1)
	else:
		print(d)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	n = int(input())
	li = [int(i) for i in input().split(' ')]
	d = {}
	m = 0
	mv = 0
	for (p, i) in enumerate(li):
		d[i] = d.get(i, 0) + 1
		if d[i] > mv:
			m = i
			mv = d[i]
	if len(d) > mv:
		print(mv)
	elif len(d) == mv:
		print(mv - 1)
	elif len(d) < mv:
		print(len(d))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	if n == 1:
		print(0)
	else:
		dis_pairs = list(set(arr))
		count = Counter(arr)
		val = list(count.values())
		m = max(val)
		d = len(dis_pairs)
		if m > len(dis_pairs):
			m -= 1
		else:
			d -= 1
		print(min(m, d))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		d[i] = 0
	for i in l:
		d[i] += 1
	myk = len(d)
	maksik = 0
	dupa = -12131
	for i in l:
		if d[i] > maksik:
			maksik = min(d[i], myk - 1)
		if d[i] > myk:
			dupa = myk
	print(max(maksik, dupa))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	freq = {}
	m = 0
	for i in a:
		if i not in freq:
			freq[i] = 0
		freq[i] += 1
		m = max(m, freq[i])
	ones = 0
	ones = len(list(set(a)))
	if m > ones - 1:
		if m - 1 >= ones:
			print(ones)
		elif ones - 1 == 0:
			print(0)
		else:
			print(ones - 1)
	elif m < ones - 1:
		print(m)
	else:
		print(m)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = len(set(a))
	d = Counter(a)
	b = max(d.values())
	if b == x:
		print(x - 1)
	elif b > x:
		print(x)
	else:
		print(b)


from collections import defaultdict
t = int(input())
for w in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	h = defaultdict(int)
	h[a[0]] += 1
	k = a[0]
	m = 1
	for i in range(1, n):
		h[a[i]] += 1
		if h[a[i]] > m:
			m = h[a[i]]
			k = a[i]
	c = 0
	for u in h:
		if u != k:
			c += 1
	if m > c + 1:
		print(c + 1)
	else:
		print(min(m, c))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(most_common, num_most_common) = Counter(a).most_common(1)[0]
	ba = list(set(a))
	b = len(ba)
	if n == 1:
		print(0)
	elif b - num_most_common >= 1:
		print(num_most_common)
	elif b == num_most_common:
		print(b - 1)
	elif num_most_common > b:
		print(b)


from collections import Counter
t = int(input())
while t > 0:
	n = int(input())
	y = [int(x) for x in input().split()]
	x = set(y)
	c = Counter(y)
	if n == 1:
		print(0)
	elif len(x) == 1:
		print(1)
	else:
		(i, j) = c.most_common(1)[0]
		if j == 1:
			print(1)
		elif j - 1 >= len(x):
			print(len(x))
		else:
			print(min(j, len(x) - 1))
	t -= 1


from collections import *
for i in range(int(input())):
	n = int(input())
	lt = []
	l = list(map(int, input().split()))
	m = max(Counter(l).values())
	s = set(l)
	x = len(s) - 1
	if x >= m:
		print(m)
	elif m - x >= 2:
		print(x + 1)
	else:
		print(x)


import sys
from collections import *
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	C = Counter(A)
	numEls = len(C)
	maxEl = max(C.values())
	if numEls == maxEl:
		print(str(numEls - 1))
	else:
		print(str(min(numEls, maxEl)))


t = int(input())
for i in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	sls = set(ls)
	mxl = -1
	ls.sort()
	c = 1
	for j in range(1, n):
		if ls[j] == ls[j - 1]:
			c = c + 1
		else:
			mxl = max(mxl, c)
			c = 1
	mxl = max(mxl, c)
	l = len(sls)
	if l < 2:
		if mxl >= 2:
			print(1)
		else:
			print(0)
	elif mxl == l:
		if mxl == 2:
			if len(ls) == 3 or len(ls) == 4:
				print(1)
			else:
				print(2)
		else:
			print(l - 1)
	elif mxl > l:
		print(l)
	elif mxl < l:
		print(mxl)


t = int(input(''))
for _ in range(t):
	n = int(input(''))
	a = list(map(int, input('').split(' ')))
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 0
		d[a[i]] = d[a[i]] + 1
	mx = 0
	l = len(d)
	for k in d:
		if min(d[k], l - 1) > mx:
			mx = min(d[k], l - 1)
		if min(d[k] - 1, l) > mx:
			mx = min(d[k] - 1, l)
	print(mx)


import sys
for _ in range(int(sys.stdin.readline().rstrip())):
	n = int(sys.stdin.readline().rstrip())
	num = list(map(int, sys.stdin.readline().rstrip().split()))
	chk = [0] * (n + 1)
	cntx = 0
	cnty = 0
	for i in num:
		if chk[i] == 0:
			cntx += 1
		chk[i] += 1
		cnty = max(cnty, chk[i])
	re = max(min(cnty, cntx - 1), min(cnty - 1, cntx))
	print(re)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	check = [0 for i in range(n + 1)]
	(m, d) = (0, 0)
	for i in a:
		if check[i] == 0:
			d += 1
		check[i] += 1
		m = max(m, check[i])
	if m > d:
		print(d)
	elif m == d:
		print(d - 1)
	else:
		print(m)


import statistics
from collections import Counter
t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	totaldistinct = len(set(a))
	if totaldistinct == n:
		print(1)
	else:
		a = sorted(a)
		cnt = 0
		mx = 0
		md = a[0]
		for i in range(1, n):
			if a[i] == a[i - 1]:
				cnt += 1
			else:
				if cnt + 1 > mx:
					mx = cnt + 1
					md = a[i - 1]
				cnt = 0
			if i == n - 1:
				if cnt + 1 > mx:
					mx = cnt + 1
					md = a[i - 1]
		modee = md
		countt = mx
		totaldistinct -= 1
		if totaldistinct >= countt:
			print(countt)
		elif countt - totaldistinct > 1:
			print(totaldistinct + 1)
		else:
			print(totaldistinct)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys
from collections import defaultdict

def solve(n, A):
	if n == 1:
		return 0
	D = defaultdict(int)
	for a in A:
		D[a] += 1
	l = len(D)
	if l == 1:
		return 1
	m = 0
	for v in D.values():
		m = max(m, v)
	if l == m:
		return l - 1
	return min(l, m)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = tuple(map(int, input().split()))
		ans = solve(n, A)
		sys.stdout.write(str(ans) + '\n')
main()


from collections import Counter
tc = int(input())
for _ in range(tc):
	n = int(input())
	(*nums,) = map(int, input().split())
	cnt = Counter(nums)
	distinct = len(set(cnt))
	same_key = max(cnt, key=lambda k: cnt[k])
	ans = min(distinct, cnt[same_key])
	if distinct == cnt[same_key]:
		ans -= 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	c = dict()
	for j in b:
		if j in c.keys():
			c[j] += 1
		else:
			c[j] = 1
	m = -1
	for j in b:
		if c[j] > m:
			m = c[j]
	p = len(set(b)) - 1
	if p >= m:
		print(m)
	elif m == p + 1:
		print(p)
	else:
		print(p + 1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = {}
	for s in a:
		try:
			c[s] += 1
		except:
			c[s] = 1
	m = max(c.values())
	if m - 1 >= len(c):
		print(len(c))
	elif m == len(c):
		print(len(c) - 1)
	elif m == len(c) - 1:
		print(len(c) - 1)
	else:
		print(m)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	a = len(set(arr))
	arr.sort()
	count = 0
	b = 0
	i = 0
	if n == 1:
		print(0)
	elif a == n:
		print(1)
	else:
		while i < n - 1:
			if arr[i + 1] == arr[i]:
				b += 1
			else:
				b = 0
			count = max(count, b)
			i += 1
		print(max(min(a - 1, count + 1), min(a, count)))


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	d = {}
	for i in l:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	m = 0
	z = 0
	for (i, j) in d.items():
		if j > m:
			m = j
		z += 1
	if z == m:
		print(z - 1)
	elif m > z:
		print(z)
	else:
		print(m)


t = int(input())
for i in range(0, t):
	n = int(input())
	a = input().split(' ')
	d = dict()
	s = set()
	k = 0
	v = 0
	for j in range(0, n):
		a[j] = int(a[j])
		s.add(a[j])
		if a[j] not in d:
			d[a[j]] = 0
		d[a[j]] += 1
		if v < d[a[j]]:
			v = d[a[j]]
			k = a[j]
	s.remove(k)
	x = min(v, len(s))
	y = min(v - 1, len(s) + 1)
	print(max(x, y))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = defaultdict(int)
	d = 0
	for i in a:
		if s[i] == 0:
			d += 1
		s[i] += 1
	t = max(s.values())
	out = min(d, t)
	if d == t:
		out -= 1
	print(out)


from collections import Counter
t = int(input())
for test in range(t):
	n = int(input())
	nt = list(map(int, input().split()))
	a = Counter(nt)
	b = len(a)
	c = max(a.values())
	if b != c:
		print(min(b, c))
	else:
		print(b - 1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for j in range(n):
		if l[j] not in d:
			d[l[j]] = 0
		d[l[j]] += 1
	a = len(d)
	m = 0
	for j in d:
		m = max(m, d[j])
	if m >= a + 1:
		print(a)
	elif m == a:
		print(a - 1)
	else:
		print(m)


import collections
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(1)
		continue
	d = collections.Counter(a)
	greatest = 0
	for key in d:
		greatest = max(greatest, d[key])
	if greatest == len(d):
		print(greatest - 1)
	else:
		print(min(greatest, len(d)))


tc = int(input())
for i in range(tc):
	num = int(input())
	array = [int(x) for x in input().split()]
	mydict = {}
	unique = 0
	same = 0
	for ele in array:
		if ele in mydict:
			mydict[ele] += 1
			if mydict[ele] > same:
				same = mydict[ele]
		else:
			unique += 1
			mydict[ele] = 1
			if mydict[ele] > same:
				same = mydict[ele]
	if num == 1:
		print(0)
	elif num == 2:
		print(1)
	elif unique == same:
		print(same - 1)
	else:
		print(min(unique, same))


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	D = {}
	for i in range(n):
		if A[i] in D:
			D[A[i]] += 1
		else:
			D[A[i]] = 1
	m = max((D[i] for i in D))
	print(max(min(m - 1, len(D)), min(m, len(D) - 1)))


from sys import stdin
import math
inp = stdin.readline
t = int(inp().strip())
while t:
	t -= 1
	n = int(inp().strip())
	a = [int(k) for k in inp().strip().split()]
	if n == 1:
		print(0)
		continue
	freq = {}
	for num in a:
		freq[num] = freq.get(num, 0) + 1
	maxFreq = max(freq.values())
	values = len(freq.values()) - 1
	print(min(maxFreq, values + (1 if values + 1 < maxFreq else 0)))


from collections import Counter
i = int(input())
for _ in range(i):
	n = int(input())
	c = list(map(int, input().split()))
	h = Counter(c)
	tr = 0
	for key in h.keys():
		if int(h[key]) >= tr:
			tr = int(h[key])
	if tr > len(h):
		print(len(h))
	elif tr == len(h):
		print(tr - 1)
	else:
		print(tr)


st = input()
t = int(st)
while t > 0:
	t = t - 1
	st = input()
	n = int(st)
	st = input()
	ls = st.split()
	m = n
	while n > 0:
		n = n - 1
		ls[n] = int(ls[n])
	di = dict()
	n = m
	while m > 0:
		m = m - 1
		if ls[m] in di:
			di[ls[m]] = di[ls[m]] + 1
		else:
			di[ls[m]] = 1
	maxoc = 0
	cnt = 0
	for x in di:
		cnt = cnt + 1
		if maxoc < di[x]:
			maxoc = di[x]
	if maxoc == cnt - 1:
		print(maxoc)
	elif maxoc == cnt:
		print(cnt - 1)
	elif maxoc > cnt:
		print(cnt)
	else:
		print(maxoc)


from collections import Counter, defaultdict
import math
import pdb
testcases = int(input())
for _ in range(testcases):
	n = int(input())
	ls = list(map(int, input().strip().split()))
	freq_dic = Counter(ls)
	a = len(freq_dic)
	b = freq_dic.most_common(1)[0][1]
	if a != b:
		print(min(a, b))
	else:
		print(a - 1)


from collections import Counter

def f(n, a):
	s = set(a)
	cnt = Counter(a)
	m = cnt.most_common(1)[0][1]
	if len(s) > m:
		return m
	return min(m - 1, len(s))

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		print(f(n, a))
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = {}
	for i in range(n):
		b[a[i]] = 0
	for i in range(n):
		b[a[i]] += 1
	c = []
	d = []
	for i in b:
		c.append(i)
		d.append(b[i])
	e = len(c)
	f = max(d)
	if e >= f + 1:
		print(f)
	elif e == f:
		print(f - 1)
	else:
		print(e)


t = int(input())
while t:
	n = int(input())
	a = sorted(list(map(int, input().split())))
	dis = 1
	cont = 1
	ch = 1
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			dis += 1
			if cont > ch:
				ch = cont
			cont = 1
		else:
			cont += 1
	ch = max(cont, ch, 1)
	if ch > dis:
		print(min(n // 2, dis))
	elif dis > ch:
		print(min(n // 2, ch))
	else:
		print(min(n // 2, dis - 1))
	t -= 1


from collections import Counter
T = int(input())
for i in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	x = len(set(arr))
	dict = Counter(arr)
	y = max(dict.values())
	if x == y:
		print(x - 1)
	else:
		print(min(x, y))


import sys
T = int(input())
for t in range(T):
	a = {}
	n = int(input())
	cstb = 0
	csta = 0
	maxa = 0
	lst = sys.stdin.readline().split()
	for i in lst:
		index = int(i)
		if not a.get(index):
			cstb += 1
			a[index] = 0
		a[index] += 1
		if a[index] > csta:
			csta = a[index]
			maxa = index
	if cstb == csta:
		print(cstb - 1)
	else:
		print(min(cstb, csta))


R1 = lambda : list(map(int, input().split()))
R2 = lambda : int(input())
for _ in range(R2()):
	n = R2()
	A = R1()
	T = {}
	m = 0
	c = 0
	for a in A:
		if T.get(a):
			T[a] += 1
		else:
			T[a] = 1
			c += 1
		m = max(m, T[a])
	print(max(min(c - 1, m), min(c, m - 1)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * (max(a) + 1)
	for ele in a:
		b[ele] += 1
	ma = 0
	count = 0
	for i in range(len(b)):
		if b[i] > 0:
			count += 1
			if b[i] > ma:
				ma = b[i]
	if ma >= count + 1:
		print(min(ma, count - 1) + 1)
	else:
		print(min(ma, count - 1))


import collections as co
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = co.Counter(a)
	s = set(a)
	x = min(len(s) - 1, max(c.values()))
	y = min(len(s), max(c.values()) - 1)
	ans = max(x, y)
	print(ans)


T = int(input())
from collections import Counter
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	num_unique = len(set(a))
	num_same = Counter(a).most_common()[0][1]
	p1 = min(num_unique - 1, num_same)
	p2 = min(num_unique, num_same - 1)
	res = max(0, max(p1, p2))
	print(res)


for _ in range(int(input())):
	int(input())
	A = list(map(int, input().split()))
	cnt = [0] * (len(A) + 1)
	for x in A:
		cnt[x] += 1
	l = len(set(A))
	m = max(cnt)
	if l == m:
		print(m - 1)
	else:
		print(min(l, m))


def mostFrequent(arr, n):
	arr.sort()
	max_count = 1
	res = arr[0]
	curr_count = 1
	for i in range(1, n):
		if arr[i] == arr[i - 1]:
			curr_count += 1
		else:
			if curr_count > max_count:
				max_count = curr_count
				res = arr[i - 1]
			curr_count = 1
	if curr_count > max_count:
		max_count = curr_count
		res = arr[n - 1]
	return res
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	myset = set(arr)
	diff = []
	for each in myset:
		diff.append(each)
	same = arr.count(mostFrequent(arr, len(arr))) - 1
	print(max(min(len(diff) - 1, same + 1), min(len(diff), same)))


from itertools import groupby
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = list(set(l))
	l.sort()
	m = 0
	for (i, j) in groupby(l):
		x = len(list(j))
		if x > m:
			m = x
	x = len(l1)
	if n == 1:
		print(0)
	elif x == m:
		print(x - 1)
	elif x > m:
		print(m)
	else:
		print(x)


t = int(input())
for rep in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = [0] * (n + 1)
	d = {}
	for i in range(n):
		l[a[i]] += 1
		if a[i] not in d:
			d[a[i]] = 1
	t1 = len(d)
	t2 = max(l)
	if t1 < t2:
		print(t1)
	elif t1 == t2:
		print(t1 - 1)
	else:
		print(t2)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = Counter(l)
	s = len(set(l))
	i = c.most_common()[0][1]
	ans = max(min(i, s - 1), min(i - 1, s))
	print(ans)


import sys
from collections import Counter
input = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

def read_int_n():
	return list(map(int, input().split()))

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def slv(N, A):
	C = Counter(A)
	(_, sc) = C.most_common(1)[0]
	fc = len(C) - 1
	return max(min(sc, fc), min(sc - 1, fc + 1))

def main():
	T = read_int()
	for _ in range(T):
		N = read_int()
		A = read_int_n()
		print(slv(N, A))
main()


import sys

def read_line():
	return sys.stdin.readline()

def solve(lst):
	d = {}
	maxx = 0
	for i in lst:
		d[i] = d.get(i, 0) + 1
		maxx = max(maxx, d[i])
	first_size = len(d)
	sec = maxx - 1
	if min(first_size, sec) == 0 and first_size > 2:
		print(1)
	else:
		print(max(min(first_size, sec), min(first_size - 1, sec + 1)))
n = int(read_line())
for i in range(n):
	k = int(read_line())
	line = read_line().split(' ')
	solve([int(i) for i in line])


for i1 in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	for i in a:
		dic[i] = 0
	for i in a:
		dic[i] += 1
	maxkey = max(dic.values())
	count = len(dic)
	if count > maxkey:
		print(maxkey)
	elif count == maxkey:
		print(count - 1)
	else:
		print(count)


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input().strip('\n')
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
let = 'abcdefghijklmnopqrstuvwxyz'
from collections import Counter
for _ in range(ii()):
	n = ii()
	l = il()
	cntr = Counter()
	for i in range(n):
		cntr[l[i]] += 1
	x = len(cntr)
	x -= 1
	y = cntr.most_common(1)[0][1]
	if y - 1 >= x + 1:
		print(x + 1)
	else:
		print(min(x, y))


import math
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		d1 = defaultdict(int)
		for i in range(n):
			d1[arr[i]] += 1
		max1 = 0
		count = 0
		for i in d1:
			if d1[i] > max1:
				max1 = d1[i]
		print(max(min(len(d1) - 1, max1), min(len(d1), max1 - 1)))


inps = int(input())
for inp in range(inps):
	n = int(input())
	arr = n * [0]
	for x in input().split():
		arr[int(x) - 1] += 1
	arr = list(filter(0 .__ne__, arr))
	first = max(arr)
	second = len(arr) - 1
	if first == 0:
		print(0)
	elif first < second:
		print(first)
	elif first > second:
		print(min(first - 1, second + 1))
	else:
		print(second)


T = int(input())
for t in range(T):
	n = int(input())
	A = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		continue
	s = len(set(A))
	d = {}
	rep = 0
	for e in A:
		if e in d:
			d[e] += 1
			if d[e] > rep:
				rep = d[e]
		else:
			d[e] = 1
			if d[e] > rep:
				rep = d[e]
	if rep == 1 and n > 1:
		print(1)
		continue
	if s == rep:
		print(s - 1)
		continue
	if s < rep:
		print(s)
		continue
	if s > rep:
		print(rep)
		continue


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	for i in range(n):
		if a[i] in dic:
			dic[a[i]] += 1
		else:
			dic[a[i]] = 1
	maxPiv = 0
	maxIdx = 0
	for d in dic.items():
		if d[1] > maxPiv:
			maxIdx = d[0]
			maxPiv = d[1]
	s = []
	for i in range(n):
		if a[i] != maxIdx:
			s.append(a[i])
	s = list(set(s))
	if maxPiv - 2 >= len(s) and maxIdx not in s:
		print(min(len(s), maxPiv) + 1)
	else:
		print(min(len(s), maxPiv))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * n
	freq = 0
	count = 0
	for x in a:
		if not b[x - 1]:
			count += 1
		b[x - 1] += 1
		freq = max(freq, b[x - 1])
	if freq > count:
		print(count)
	elif freq == count:
		print(count - 1)
	else:
		print(freq)


t = int(input())
for i in range(t):
	count = {}
	n = int(input())
	arr = map(int, input().split(' '))
	for a in arr:
		if a in count.keys():
			count[a] += 1
		else:
			count[a] = 1
	mx = max(count.values())
	diff = len(count.keys()) - 1
	if mx - 1 >= diff + 1:
		print(diff + 1)
	else:
		print(min(mx, diff))


case = int(input())
for i in range(case):
	num = int(input())
	list1 = [int(v) for v in input().split()]
	if num == 1:
		print(0)
	elif num == 2:
		print(1)
	else:
		list2 = [0] * num
		for j in range(num):
			list2[list1[j] - 1] += 1
		x = len(list2) - list2.count(0)
		y = max(list2)
		if x == y:
			print(x - 1)
		else:
			print(min(x, y))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	l = 0
	c = 0
	ind = -1
	tf = False
	for i in d:
		if ind == -1:
			ind = i
		elif d[i] > d[ind]:
			ind = i
	if d[ind] == 1:
		if len(a) == 1:
			print(0)
		else:
			print(1)
	else:
		l = len(d)
		print(max(min(l, d[ind] - 1), min(l - 1, d[ind])))


t = int(input())
for x in range(t):
	a = int(input())
	kx = set()
	lp = list(map(int, input().split()))
	k = sorted(lp)
	count = 1
	r = 1
	kx.add(k[0])
	for x in range(1, a):
		kx.add(k[x])
		if k[x] == k[x - 1]:
			count += 1
		else:
			if r < count:
				r = count
			count = 1
	M = max(r, count)
	cs = len(kx) - 1
	if M - cs >= 2:
		print(cs + 1)
	elif M > cs:
		print(cs)
	elif cs > M:
		print(M)
	elif cs == M:
		print(cs)


def main():
	from collections import Counter
	for __ in [0] * int(input()):
		N = int(input())
		(*A,) = map(int, input().split())
		C = Counter(A)
		k = len(C)
		maxv = 0
		for v in C.values():
			if maxv < v:
				maxv = v
		if maxv > k:
			print(k)
		elif maxv < k:
			print(maxv)
		else:
			print(k - 1)
main()


def ans(a):
	dic = {}
	for i in a:
		if i not in dic:
			dic[i] = 1
		else:
			dic[i] += 1
	e = dic.values()
	f = max(e)
	l = len(e) - 1
	if f - l >= 2:
		return l + 1
	if f - l == 0 or f - l == 1:
		return l
	return f
n = int(input())
for i in range(n):
	o = int(input())
	m = input().split()
	a = []
	for i in m:
		a.append(int(i))
	print(ans(a))


T = int(input())
for test in range(T):
	N = int(input())
	skillcount = [0 for i in range(N + 1)]
	skillarray = list(map(int, input().split()))
	unique = 0
	for student in skillarray:
		if skillcount[student] == 0:
			unique += 1
		skillcount[student] += 1
	same = max(skillcount)
	nooverlap = min(same, unique - 1)
	overlap = min(same - 1, unique)
	print(max(nooverlap, overlap))


a = [4, 2, 4, 1, 4, 3, 4]

def test(a):
	a = [int(i) for i in a]
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	unique = len(d)
	highRep = max(d.values())
	if unique == highRep:
		print(unique - 1)
	else:
		print(min(highRep, unique))
t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	nn = input().strip().split(' ')
	test(nn)


from math import *
from collections import *
t = int(input())
for y in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = Counter(a)
	diff = len(d)
	same = 0
	for i in d.values():
		same = max(i, same)
	if diff < same:
		print(diff)
	elif diff == same:
		print(diff - 1)
	else:
		print(same)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	d = {i: 0 for i in a}
	for i in a:
		d[i] += 1
	r1 = min(max(d.values()), len(d) - 1)
	r2 = min(max(d.values()) - 1, len(d))
	print(max(r1, r2))


testCases = int(input())
for _ in range(testCases):
	count = {}
	length = int(input())
	sequence = list(map(int, input().split()))
	for i1 in sequence:
		if i1 in count.keys():
			count[i1] += 1
		else:
			count[i1] = 1
	num_distinct = len(count.keys())
	num_same = max(count.values())
	if num_distinct < num_same:
		print(num_distinct)
	elif num_distinct == num_same:
		print(num_distinct - 1)
	else:
		print(num_same)


from sys import stdin
from collections import Counter
input = stdin.buffer.readline
T = int(input())
for _ in range(T):
	n = int(input())
	ls = list(map(int, input().split()))
	cc = Counter(ls)
	(mx, dcc) = (max(cc.values()), len(cc.keys()))
	if mx > dcc:
		print(dcc)
	elif mx == dcc:
		print(dcc - 1)
	else:
		print(mx)


t = int(input())
for i in range(t):
	n = int(input())
	L = list(map(int, input().split(' ')))
	c = [0 for i in range(n + 1)]
	for i in L:
		c[i] += 1
	diff = len(set(L))
	same = max(c)
	if same == diff:
		print(same - 1)
	elif same > diff:
		print(diff)
	else:
		print(same)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	h = defaultdict(lambda : 0)
	for i in l:
		h[i] += 1
	mp = 0
	mf = 0
	for (i, j) in h.items():
		if i >= 1:
			mp += 1
		if j > mf:
			mf = h[i]
	if mf >= mp:
		mf -= 1
	else:
		mp -= 1
	ans = min(mf, mp)
	print(ans)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def bool_to_str(flag):
	if flag:
		return 'YES'
	else:
		return 'NO'
tests = inp()
for test in range(tests):
	pop = inp()
	people = inlt()
	if pop == 1:
		print(0)
	else:
		people_dict = {}
		distinct = 0
		biggest = 1
		for person in people:
			if person not in people_dict:
				people_dict[person] = 1
				distinct += 1
			else:
				people_dict[person] += 1
				biggest = max(biggest, people_dict[person])
		if distinct == biggest:
			distinct -= 1
		print(min(distinct, biggest, int(pop / 2)))


T = int(input())
for t in range(T):
	n = int(input())
	students = [int(i) for i in input().split(' ')]
	sDict = {}
	uniqueMax = 0
	sameMax = 1
	for s in students:
		if sDict.get(s):
			sDict[s] += 1
			sameMax = max(sameMax, sDict[s])
		else:
			sDict[s] = 1
			uniqueMax += 1
	if uniqueMax == sameMax:
		print(uniqueMax - 1)
	else:
		print(min(uniqueMax, sameMax))


import math
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(lambda x: int(x), input().split()))
	d = {}
	for i in ar:
		if i in d.keys():
			d[i] += 1
		else:
			d[i] = 1
	m = 0
	s = len(d.keys())
	for i in d.keys():
		size = max(min(d[i], s - 1), min(d[i] - 1, s))
		m = max(size, m)
	print(m)


for i in range(int(input())):
	q = int(input())
	n = list(map(int, input().split()))
	a = [0] * (q + 1)
	if q == 1:
		print(0)
	else:
		for i in range(q):
			a[n[i]] += 1
		e = max(a)
		w = len(set(n))
		if e == w:
			print(e - 1)
		else:
			print(min(w, e))


from collections import Counter
t = int(input())
for case_num in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	c = Counter(a)
	distinct = len(c)
	hi = 0
	for key in c:
		hi = max(hi, c[key])
	if distinct > hi:
		print(hi)
	elif distinct == hi:
		print(hi - 1)
	else:
		print(distinct)


from collections import Counter

def solve(a, n):
	d = Counter(a)
	if n == 0 or n == 1:
		return 0
	key = 0
	mx = 1
	for i in d.keys():
		mx = max(mx, d[i])
		key += 1
	if key - 1 >= mx:
		return mx
	else:
		return min(key, mx - 1)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a, n))


from collections import defaultdict

def soln(arr):
	ctr = defaultdict(int)
	for n in arr:
		ctr[n] += 1
	maxCount = max((val for val in ctr.values()))
	numVals = len(ctr)
	if numVals == 1:
		if maxCount == 1:
			return 0
		else:
			return 1
	else:
		candidates = [min(numVals - 1, maxCount), min(numVals, maxCount - 1)]
		return max(candidates)
	return 0

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = [int(x) for x in input().split(' ')]
		print(soln(arr))

def test():
	arr = [2, 1, 5, 4, 3]
	print(soln(arr))
main()


import random, math
from copy import deepcopy as dc

def solution(li):
	freq = {}
	maxi = 0
	for i in range(len(li)):
		freq[li[i]] = freq.get(li[i], 0) + 1
		maxi = max(freq[li[i]], maxi)
	if len(freq) == maxi:
		return maxi - 1
	elif len(freq) > maxi:
		return maxi
	else:
		return len(freq)

def input_test():
	for _ in range(int(input())):
		n = int(input())
		li = list(map(int, input().strip().split(' ')))
		out = solution(li)
		print(out)

def test():
	pass
input_test()


import math
from collections import defaultdict

def nCr(n, r):
	f = math.factorial
	return f(n) / f(r) / f(n - r)
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	d = defaultdict(int)
	md = -1
	for a in A:
		d[a] += 1
		md = max(md, d[a])
	res = 0
	for size in range(n // 2, -1, -1):
		if md == size:
			if size <= len(d) - 1:
				res = size
				break
		if md > size:
			if size <= len(d):
				res = size
				break
	print(int(res))


for _ in range(int(input())):
	n = int(input())
	m = [*map(int, input().split())]
	l3 = [0 for i in range(n)]
	for i in m:
		l3[i - 1] += 1
	l2 = max(l3)
	l1 = len(l3) - l3.count(0) - 1
	print(max(0, min(l1, l2), min(l1 + 1, l2 - 1)))


t = int(input())
for ii in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	p = dict()
	if len(a) == 1:
		print(0)
		continue
	for i in a:
		if i in p:
			p[i] += 1
		else:
			p[i] = 1
	ans = []
	for i in p:
		ans.append(p[i])
	if len(ans) == 1:
		print(1)
		continue
	ronta = min(max(ans), len(ans) - 1)
	ronta = max(ronta, min(max(ans) - 1, len(ans)))
	print(ronta)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	ls = [int(a) for a in input().split()]
	(ls1, ls2) = ([], [])
	st = set(ls)
	mx = 0
	ind = 0
	ls1 = st
	ls2 = [item for (items, c) in Counter(ls).most_common() for item in [items] * c]
	mx = ls.count(ls2[0])
	ind = ls.index(ls2[0])
	sz = min(mx, len(ls1))
	if ls[ind] in ls1 and len(ls1) == mx:
		sz -= 1
	print(sz)


import sys
from collections import Counter
readLine = lambda : sys.stdin.readline()
readInt = lambda : int(sys.stdin.readline())
readInts = lambda : [int(x) for x in sys.stdin.readline().split(' ')]

def main():
	solns = []
	t = readInt()
	for _ in range(t):
		n = readInt()
		a = readInts()
		counter = Counter(a)
		unique = set(a)
		two = max(((k, count) for (k, count) in counter.items()), key=lambda x: x[1])
		twoCount = two[1]
		twoVal = two[0]
		if twoCount > len(unique):
			solns.append(len(unique))
		elif twoCount == len(unique):
			solns.append(twoCount - 1)
		else:
			x = min(twoCount, len(unique))
			solns.append(x)
	print('\n'.join((str(x) for x in solns)))
main()


def solve(n, a):
	s = set()
	for i in a:
		s.add(i)
	n_u = len(s) - 1
	a.sort()
	m_el = 0
	m_el_n = 0
	melns = 0
	for i in a:
		if i != m_el:
			melns = max(m_el_n, melns)
			m_el = i
			m_el_n = 1
		else:
			m_el_n += 1
	melns = max(m_el_n, melns)
	if melns - n_u >= 2:
		return n_u + 1
	return min(n_u, melns)
[print(solve(int(input()), list(map(int, input().split())))) for i in range(int(input()))]


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	c = Counter(list(map(int, input().split())))
	ans = 0
	for v in c.values():
		ans = max(ans, min(len(c), v - 1), min(len(c) - 1, v))
	print(ans)


counter = int(input())
for i in range(counter):
	students = int(input())
	skills = [int(x) for x in input().split()]
	if students == 1:
		print(0)
	else:
		memo = {}
		maxcount = 1
		for i in skills:
			if i in memo:
				memo[i] += 1
				if memo[i] > maxcount:
					maxcount = memo[i]
			else:
				memo[i] = 1
		notmax = len(memo) - 1
		if maxcount - notmax >= 2:
			print(notmax + 1)
		elif maxcount >= notmax:
			print(notmax)
		elif maxcount < notmax:
			print(maxcount)
		elif maxcount == 1:
			print(1)


t = int(input())
for i in range(t):
	pos = 0
	diff = 0
	max = 0
	n = int(input())
	a = [int(x) for x in input().strip().split()]
	b = [0] * (len(a) + 1)
	for j in range(len(a)):
		if b[a[j]] == 0:
			diff += 1
		if b[a[j]] > max:
			max = b[a[j]]
			pos = j
		b[a[j]] += 1
	if len(a) == 1:
		print('0')
	elif diff == len(a):
		print('1')
	elif max + 1 > diff:
		print(diff)
	elif max + 1 == diff:
		print(max)
	elif max + 1 < diff:
		print(max + 1)


import sys
input = sys.stdin.buffer.readline
T = int(input())
for _ in range(1, T + 1):
	n = int(input())
	hashMap = {}
	sameNumber = 0
	for x in [int(x) for x in input().split()]:
		if x not in hashMap:
			hashMap[x] = 0
		hashMap[x] += 1
		if sameNumber < hashMap[x]:
			sameNumber = hashMap[x]
	distincNumber = len(hashMap)
	if distincNumber > sameNumber:
		print(sameNumber)
	elif distincNumber < sameNumber:
		print(distincNumber)
	else:
		print(sameNumber - 1)


test_cases = int(input())
for i in range(test_cases):
	students = int(input())
	skills = list(map(int, input().split()))[:students]
	count_dict = dict.fromkeys(skills, 0)
	for i in skills:
		count_dict[i] += 1
	most_frequent = max(count_dict.values())
	different_skills = len(count_dict)
	print(max(min(different_skills - 1, most_frequent), min(different_skills, most_frequent - 1)))


from collections import Counter
for _ in range(int(input())):
	N = int(input())
	c = Counter(map(int, input().split()))
	k = max(c.values())
	m = c.keys()
	if k >= len(m):
		k = min(k - 1, len(m))
	print(k)


t = int(input())
for t in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	temp = set(a)
	Dict = {}
	for i in a:
		if i in Dict:
			Dict[i] += 1
		else:
			Dict[i] = 1
	maxfreq = 0
	for i in Dict:
		if maxfreq < Dict[i]:
			maxfreq = Dict[i]
	if len(temp) == n:
		if n == 1:
			print(0)
		else:
			print(1)
	elif maxfreq == len(temp):
		print(maxfreq - 1)
	else:
		print(min(maxfreq, len(temp)))


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	arr = str(input())
	arr = arr.split()
	dicts = {}
	nonunique = {}
	unique = {}
	if n == 1:
		print(0)
		continue
	for i in arr:
		i = int(i)
		if i not in dicts:
			dicts[i] = 1
		else:
			dicts[i] += 1
			if i not in nonunique:
				nonunique[i] = 2
			else:
				nonunique[i] += 1
	if len(nonunique) == 0:
		print(1)
		continue
	getunique = len(dicts) - len(nonunique)
	nonuniquearr = []
	for (key, value) in nonunique.items():
		nonuniquearr.append(value)
	nonuniquearr = sorted(nonuniquearr)
	maxx = 0
	for i in nonuniquearr:
		if getunique >= i:
			maxx = max(maxx, i)
		else:
			maxx = max(maxx, min(getunique + 1, i - 1))
		getunique += 1
	print(maxx)


t = int(input())
for turn in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	lis.sort()
	num = []
	cnt = 1
	for i in range(n - 1):
		if lis[i] != lis[i + 1]:
			num.append(cnt)
			cnt = 1
		else:
			cnt += 1
	num.append(cnt)
	num.sort()
	if max(num) > len(num):
		print(len(num))
	elif max(num) == len(num):
		print(max(num) - 1)
	else:
		print(max(num))


from collections import Counter
for _ in range(int(input())):
	N = int(input())
	X = list(map(int, input().split()))
	Set = set(X)
	Number = Counter(X)
	MaxValue = max(Number.values())
	LenSet = len(Set)
	if LenSet > MaxValue:
		print(MaxValue)
	elif LenSet == MaxValue:
		print(LenSet - 1)
	else:
		print(LenSet)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	ll = dict(cc(zzz()))
	l = sorted(list(ll.values()))
	ans = len(l) - 1
	if l[-1] > ans:
		if l[-1] >= ans + 2:
			ans += 1
	else:
		ans = l[-1]
	print(ans)


def solution():
	from sys import stdout, stdin
	(_input, _print) = (stdin.readline, stdout.write)
	(_int, _range) = (int, range)
	from collections import Counter
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		c = Counter(arr)
		m_key = max(c, key=c.get)
		m = c[m_key]
		l_c = len(c)
		if l_c - 1 < m:
			if l_c <= m - 1:
				print(l_c)
			else:
				print(l_c - 1)
		else:
			print(m)
solution()


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	mc = 0
	h = 0
	for i in range(1, n):
		if l[i] == l[i - 1]:
			mc += 1
		else:
			mc = 0
		if mc > h:
			h = mc
	f = h + 1
	s = len(list(set(l))) - 1
	if f > s + 1:
		print(s + 1)
	elif f == s:
		print(f)
	elif f == s + 1:
		print(s)
	else:
		print(f)


import sys
import math
msg = sys.stdin.readlines()
l = []
for i in msg:
	l.append(list(map(int, i.split())))

def size(l):
	st = set(l)
	s = {i: 0 for i in st}
	for i in l:
		s[i] += 1
	m = -1
	for i in s:
		if s[i] > m:
			m = s[i]
	lf = len(s) - 1
	if m <= lf:
		ans = m
	elif m == lf + 1:
		ans = lf
	else:
		ans = lf + 1
	return ans
for i in range(len(l)):
	if i > 0 and i % 2 == 0:
		ls = l[i]
		print(size(ls))


import collections
t = input()
for _ in range(int(t)):
	n = int(input())
	arr = input().split()
	count = collections.Counter(arr)
	if n == 1:
		ans = 0
	else:
		maxfreq = 0
		unique = 0
		normal = len(set(arr))
		for i in count.values():
			maxfreq = max(maxfreq, i)
			if i == 1:
				unique += 1
		if maxfreq == 1:
			ans = 1
		elif abs(normal - maxfreq) == 1:
			ans = min(normal, maxfreq)
		elif normal == maxfreq:
			ans = maxfreq - 1
		elif maxfreq > normal:
			ans = min(normal, maxfreq - 1)
		else:
			ans = min(normal + 1, maxfreq)
	print(ans)


import collections
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = collections.Counter(l)
	un = 0
	ma = 0
	c1 = 0
	for i in d.values():
		un = un + 1
		if i == 1:
			c1 = c1 + 1
		if i > ma:
			ma = i
	val1 = min(un - 1, ma)
	val2 = min(un, ma - 1)
	if c1 == un and n != 1:
		print(1)
	else:
		print(max(val1, val2))


a = int(input())
for _ in range(a):
	x = int(input())
	li = [0] * (x + 1)
	li1 = list(map(int, input().split()))
	for i in li1:
		li[i] += 1
	b = max(li)
	j = len(set(li1))
	if j == b:
		j -= 1
	print(min(b, j))


t = int(input())
while t > 0:
	n = int(input())
	a = input().split(' ')
	counts = dict()
	for i in a:
		counts[i] = counts.get(i, 0) + 1
	same_max = 0
	unique = 0
	for k in counts.values():
		unique += 1
		if same_max < k:
			same_max = k
	if same_max < unique:
		print(same_max)
	elif same_max > unique:
		print(unique)
	else:
		print(same_max - 1)
	t -= 1


import sys

def data():
	return sys.stdin.buffer.readline().strip()
out = sys.stdout.write

def mdata():
	return map(int, data().split())
from collections import defaultdict as dd
for i in range(int(data())):
	n = int(data())
	a = list(mdata())
	d = dd(int)
	for i in a:
		d[i] += 1
	m = max(d.values())
	l = len(d.keys())
	if m == l:
		ans = m - 1
	elif m > l:
		ans = l
	else:
		ans = m
	out(str(ans) + '\n')


import sys
import io, os

def main():
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
	t = int(input())
	for i in range(t):
		n = int(input())
		freq = [0] * (n + 1)
		freq2 = [0] * (n + 1)
		a = [int(i) for i in input().split()]
		count = 0
		for i in a:
			freq[i] += 1
			if freq2[i] == 0:
				count += 1
			freq2[i] = 1
		pq = max(freq)
		if count == pq:
			sys.stdout.write(str(min(count, pq - 1)) + '\n')
		else:
			sys.stdout.write(str(min(count, pq)) + '\n')
main()


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	a_count = [0 for _ in range(max(a) + 1)]
	for v in a:
		a_count[v] += 1
	m_v = max(a_count)
	m_v1 = -1
	for v in a_count:
		if v != 0:
			m_v1 += 1
	if m_v > m_v1 + 1:
		m_v1 += 1
	print(min(m_v, m_v1))
T = int(input())
while T > 0:
	solve()
	T -= 1


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	count = 0
	for elem in arr:
		if elem in dic:
			dic[elem] += 1
		else:
			dic[elem] = 1
			count += 1
	cur_max = 0
	for j in dic:
		if dic[j] > count and count > cur_max:
			cur_max = count
		elif min(dic[j], count - 1) > cur_max:
			cur_max = min(dic[j], count - 1)
	print(cur_max)


from sys import stdin, stdout

def f(a, n):
	if n <= 1:
		print(0)
		return
	d = {}
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	m = 0
	for i in d:
		if d[i] > m:
			m = d[i]
	b = set(a)
	l = len(b)
	if l == m:
		print(l - 1)
	elif m < l:
		print(m)
	else:
		print(l)
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	f(a, n)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	a.sort()
	cnt = 0
	mx = 0
	i = 0
	while i < n:
		j = i
		while j + 1 < n and a[j + 1] == a[i]:
			j += 1
		cnt += 1
		mx = max(mx, j - i + 1)
		i = j + 1
	print(min(cnt, mx) - (cnt == mx))
	t -= 1


import sys
from collections import Counter

def solve(arr):
	count = Counter()
	uni = []
	non = []
	maxi = -1
	mval = -1
	for i in arr:
		if i in count:
			non.append(i)
		else:
			uni.append(i)
		count[i] += 1
		if count[i] > maxi:
			maxi = count[i]
			mval = i
	if len(non) == 0 and len(uni) > 1:
		return 1
	if len(uni) == 0 and len(non) > 1:
		return 1
	non = [i for i in non if i == mval]
	if len(non) > 0 and non[0] in uni and (len(uni) > len(non)):
		non.append(uni.remove(non[0]))
	return min(len(non), len(uni))

def main():
	q = []
	for line in sys.stdin.readlines():
		q.append(line)
	for i in range(len(q)):
		q[i] = q[i].rstrip().split(' ')
		q[i] = [int(x) for x in q[i]]
	for i in range(1, len(q), 2):
		sol = solve(q[i + 1])
		print(sol)
main()


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		f = [0] * (max(l) + 1)
		for i in range(n):
			f[l[i]] += 1
		dc = 0
		ndca = [0]
		for i in f:
			if i == 0:
				continue
			elif i == 1:
				dc += 1
			else:
				ndca.append(i - 1)
				dc += 1
		a = max(ndca)
		if a == 0:
			print(1)
		else:
			print(max(min(dc, a), min(dc - 1, a + 1)))


import sys
input = sys.stdin.readline
import heapq
import copy
import math

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

class fen_tree:

	def __init__(self, list):
		self.array = [0] + list
		for idx in range(1, len(self.array)):
			idx2 = idx + (idx & -idx)
			if idx2 < len(self.array):
				self.array[idx2] += self.array[idx]

	def prefix_query(self, idx):
		result = 0
		while idx:
			result += self.array[idx]
			idx -= idx & -idx
		return result

	def prints(self):
		print(self.array)
		return

	def range_query(self, from_idx, to_idx):
		return self.prefix_query(to_idx) - self.prefix_query(from_idx - 1)

	def update(self, idx, add):
		while idx < len(self.array):
			self.array[idx] += add
			idx += idx & -idx

def pre_sum(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def pre_back(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def search_leftmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[l] == val:
			return l
		if r - l <= 1:
			if arr[r] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] >= val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def search_rightmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[r] == val:
			return r
		if r - l <= 1:
			if arr[l] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] > val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	for test in range(tests):
		n = inp()
		a = inlt()
		a.sort()
		unique = 1
		max_freq = 0
		curr = 1
		for i in range(1, n):
			if a[i - 1] != a[i]:
				unique += 1
				curr = 1
			else:
				curr += 1
			max_freq = max(max_freq, curr)
		if max_freq == unique:
			print(max_freq - 1)
		else:
			print(min(max_freq, unique))
main()


from collections import Counter
t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	x = Counter(l)
	m = 0
	p = 0
	for i in x:
		if x[i] > m:
			m = x[i]
			p = i
	z = len(set(l))
	j = min(z, m)
	if z == m:
		print(j - 1)
	else:
		print(j)
	t = t - 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = {}
	for i in a:
		try:
			x[i] += 1
		except:
			x[i] = 1
	max_f = max(x.values())
	len_set_a = len(set(a))
	if len_set_a == max_f:
		print(min(len_set_a - 1, max_f))
	else:
		print(min(len_set_a, max_f))


def u_counter(lst):
	count = 1
	for i in range(1, len(lst)):
		if lst[i] != lst[i - 1]:
			count += 1
	return count

def o_counter(lst):
	count = 1
	max_count = count
	for i in range(len(lst) - 1):
		if lst[i] == lst[i + 1]:
			count += 1
			max_count = max(max_count, count)
		else:
			count = 1
	return max_count
t = int(input())
for i in range(t):
	n = int(input())
	lst = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	else:
		lst.sort()
		ordinar = o_counter(lst)
		unique = u_counter(lst)
		if ordinar == unique:
			print(ordinar - 1)
		else:
			print(min(ordinar, unique))


import sys
import math

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]
t = read_int()
for i in range(t):
	n = read_int()
	a = read_int_line()
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]] += 1
		else:
			d[a[i]] = 1
	ma = -1
	for i in d:
		ma = max(ma, d[i])
	ans = ma
	l = len(list(d.keys()))
	a1 = min(ma, l - 1)
	a2 = min(ma - 1, l)
	ans = max(a1, a2)
	print(ans)


import sys
from collections import deque
alf = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
input = lambda : sys.stdin.readline().strip()
ipnut = input
q = int(input())
for i in range(q):
	n = int(input())
	c = list(map(int, input().split()))
	s = {}
	for i in range(n):
		s[c[i]] = s.get(c[i], 0) + 1
	print(min(len(s), max(s.values()) - int(len(s) == max(s.values())), n // 2))


t = int(input())
for _ in range(t):
	n = int(input())
	num_dict = {}
	num_list = list(map(int, input().split()))
	if len(num_list) <= 1:
		print('0')
		continue
	for i in num_list:
		if i not in num_dict:
			num_dict[i] = 1
		else:
			num_dict[i] += 1
	count_max_dict = max(num_dict.values())
	count_max_set = len(set(num_list))
	print(max(min(count_max_set - 1, count_max_dict), min(count_max_set, count_max_dict - 1)))


t = int(input())
for _ in range(t):
	_ = int(input())
	a = list(map(int, input().split()))
	u = len(set(a))
	cs = {}
	for e in a:
		if e not in cs:
			cs[e] = 1
		else:
			cs[e] += 1
	s = max(cs.values())
	if s == u:
		print(s - 1)
	else:
		print(min(s, u))


from collections import Counter
T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	c = Counter(A)
	if c.most_common()[0][1] - 1 >= len(c):
		ans = len(c)
	elif c.most_common()[0][1] > len(c) - 1:
		ans = len(c) - 1
	else:
		ans = c.most_common()[0][1]
	print(ans)


from collections import Counter

def main():
	t = int(input())
	for i in range(t):
		l = int(input())
		a = input().split(' ')
		a = [int(j) for j in a]
		b = dict(Counter(a))
		c = list(Counter(a).values())
		c.sort()
		ans = int(l / 2)
		if len(c) - 1 >= c[-1]:
			ans = c[-1]
		elif c[-1] >= int(l / 2):
			if len(c) - 1 == int(l / 2):
				ans = int(l / 2)
			elif len(c) - 1 <= c[-1] - 2:
				ans = len(c)
			else:
				ans = len(c) - 1
		elif c[-1] < int(l / 2):
			for j in range(int(l / 2)):
				if c[-1] == int(l / 2) - j:
					if len(c) - 1 == int(l / 2) - j:
						ans = int(l / 2) - j
						break
					elif len(c) - 1 <= c[-1] - 2:
						ans = len(c)
						break
					elif len(c) - 1 < c[-1]:
						ans = len(c) - 1
						break
		print(ans)
main()


def inp_n():
	return int(input())

def inp_list():
	return list(map(int, input().split()))

def inp_mul_num():
	return map(int, input().split())

def is_odd(n):
	if n & 1:
		return True
	return False

def is_prime(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
from collections import defaultdict
import math
for _ in range(inp_n()):
	n = inp_n()
	l = inp_list()
	n1 = len(set(l))
	l.sort()
	c = 1
	m = 1
	for i in range(1, n):
		if l[i] == l[i - 1]:
			c += 1
			m = max(m, c)
		else:
			c = 1
	if m == n1:
		print(m - 1)
	else:
		print(min(m, n1))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	max_1 = 0
	max_2 = 0
	temp_1 = 0
	a.sort()
	for j in range(n):
		if j > 0 and a[j] == a[j - 1]:
			temp_1 += 1
			if temp_1 > max_1:
				max_1 = temp_1
		else:
			max_2 += 1
			temp_1 = 0
	max_1 += 1
	if max_1 < max_2:
		max_2 -= 1
	else:
		max_1 -= 1
	print(min(max_1, max_2))


T = int(input(''))
for _ in range(T):
	n = int(input())
	ar = list(map(int, input().split()))
	mp = {}
	for i in range(n):
		if ar[i] in mp.keys():
			mp[ar[i]] += 1
		else:
			mp[ar[i]] = 1
	count = len(mp)
	z = max(mp.values())
	if z <= count - 1:
		print(z)
	elif z == count:
		print(z - 1)
	else:
		print(count)


T = int(input())
for x in range(T):
	n = int(input())
	a = [int(aa) for aa in input().split()]
	hist = dict()
	for y in a:
		if y in hist:
			hist[y] = hist[y] + 1
		else:
			hist[y] = 1
	diff = len(hist)
	mx = max(hist.values())
	x1 = min(mx, diff - 1)
	x2 = min(mx - 1, diff)
	result = max(x1, x2)
	print(result)


import math
loop_time = int(input())
for time in range(0, loop_time):
	array_len = int(input())
	inp_array = input().split(' ')
	for jj in range(0, len(inp_array)):
		inp_array[jj] = int(inp_array[jj])
	d = (array_len + 1) * [0]
	for i in inp_array:
		d[i] += 1
	max_repeat_unit = max(d)
	if max_repeat_unit == len(set(inp_array)):
		print(len(set(inp_array)) - 1)
	else:
		print(min(len(set(inp_array)), max_repeat_unit))


from collections import Counter, defaultdict
t = int(input())
out = ''
while t > 0:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	c = Counter(a)
	m = 0
	for key in c:
		m = max(m, c[key])
	x = len(c) - 1
	if x > m:
		out += str(m) + '\n'
	elif x + 1 == m or x == m:
		out += str(x) + '\n'
	else:
		out += str(x + 1) + '\n'
print(out)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		d[a[i]] = d.get(a[i], 0) + 1
	l = list(d.values())
	if len(l) - 1 >= max(l):
		print(max(l))
	elif max(l) - 1 >= len(l):
		print(len(l))
	else:
		print(len(l) - 1)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
stdin = sys.stdin
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
t = ni()
for i in range(t):
	n = ni()
	a = na()
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 1
		else:
			d[a[i]] += 1
	m = max(d.values())
	if m > len(d):
		print(len(d))
	else:
		print(min(m, len(d) - 1))


t = int(input())
for _ in range(t):
	n = input()
	arr = list(map(int, input().split()))
	distinct = set(arr)
	hash = {}
	for i in arr:
		if i not in hash:
			hash[i] = 1
		else:
			hash[i] += 1
	key_max = hash[max(hash.keys(), key=lambda k: hash[k])]
	if len(distinct) > 1 and key_max == 1:
		print(1)
	else:
		print(max(min(len(distinct) - 1, key_max), min(len(distinct), key_max - 1)))


size = int(input())
for i in range(size):
	n = int(input())
	dict = {}
	a = input().split(' ')
	for j in a:
		dict[j] = 0
	for j in a:
		dict[j] += 1
	sorted_dict = sorted(dict, key=dict.get)
	popular = sorted_dict[-1]
	print(max(min(dict[popular], len(dict) - 1), min(dict[popular] - 1, len(dict))))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	(most_common, num_most_common) = Counter(l).most_common(1)[0]
	k = len(set(l))
	print(max(min(k - 1, num_most_common), min(k, num_most_common - 1)))


t = int(input())
for i in range(t):
	n = int(input())
	l = str.split(input(), ' ')
	for i in range(n):
		l[i] = int(l[i])
	d = {}
	maxx = 1
	dis = 0
	for i in range(n):
		if l[i] in d:
			d[l[i]] = d[l[i]] + 1
			if d[l[i]] > maxx:
				maxx = d[l[i]]
		else:
			dis = dis + 1
			d[l[i]] = 1
	if dis == maxx:
		print(dis - 1)
	elif dis > maxx:
		print(maxx)
	else:
		print(dis)


for i in range(int(input())):
	a = int(input())
	b = list(map(str, input().split()))
	l = {}
	for j in b:
		if j in l:
			l[j] += 1
		else:
			l[j] = 1
	p = max(l.values())
	if p > len(l):
		print(len(l))
	elif p == len(l):
		print(len(l) - 1)
	else:
		print(p)


import sys

def I():
	return sys.stdin.readline().rstrip()
for _ in range(int(I())):
	n = int(I())
	m = {}
	for i in list(map(int, I().split())):
		if not i in m:
			m[i] = 0
		m[i] += 1
	without = min(len(m) - 1, max(m.values()))
	wth = min(len(m), max(m.values()) - 1)
	print(max(wth, without))


MOD = 10 ** 9 + 7

def main():
	test = int(input())
	for iteration in range(test):
		n = int(input())
		a = [int(i) for i in input().split()]
		if n == 1:
			print(0)
		else:
			d = {}
			for i in a:
				if i in d:
					d[i] += 1
				else:
					d[i] = 1
			max = 0
			maxi = 0
			for i in a:
				if d[i] > max:
					max = d[i]
					maxi = i
			lhs = len(d) - 1
			rhs = d[maxi]
			if lhs > rhs:
				print(rhs)
			elif rhs - lhs >= 2:
				print(lhs + 1)
			else:
				print(lhs)
main()


t = int(input())
for i in range(t):
	n = input()
	a = input().split()
	adir = {}
	freq = -1
	maxlen = 0
	for x in a:
		if not x in adir:
			adir[x] = 0
			maxlen += 1
		adir[x] += 1
		if adir[x] > freq:
			freq = adir[x]
	if freq > maxlen:
		ans = maxlen
	elif freq == maxlen:
		ans = maxlen - 1
	else:
		ans = freq
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mapper = {}
	for i in a:
		if i not in mapper:
			mapper[i] = 1
		else:
			mapper[i] += 1
	changetolist = []
	for (key, value) in mapper.items():
		changetolist.append([key, value])
	changetolist = sorted(changetolist, key=lambda item: item[1], reverse=True)
	total = len(changetolist)
	index = 0
	if n == 1:
		ans = 0
		print(ans)
	else:
		ans = 1
		same = changetolist[index][1]
		if same == total:
			ans = total - 1
		elif same - 1 >= total:
			ans = total
		else:
			ans = min(total - 1, same)
		print(ans)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for j in range(n):
		if d.get(l[j]) != None:
			d[l[j]] += 1
		else:
			d[l[j]] = 1
	m = len(d)
	max = 0
	for j in d:
		if d[j] > max:
			max = d[j]
	if m > max:
		print(max)
	elif m == max:
		print(max - 1)
	else:
		print(m)


from sys import stdin, stdout
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	arr = iia()
	d = {}
	for i in arr:
		d.setdefault(i, [0])[0] += 1
	m = float('-inf')
	for i in d:
		m = max(d[i][0], m)
	dis = len(d) - 1
	if m > dis:
		m -= 1
		dis += 1
	print(min(m, dis))


total = int(input())
for _ in range(total):
	n = list(map(int, input().split()))[0]
	seq = list(map(int, input().split()))
	if len(seq) == 1:
		print(0)
		continue
	store = {}
	for x in seq:
		if x in store:
			store[x] += 1
		else:
			store[x] = 1
	distinct = len(store)
	max_repeat = max(store.values())
	if distinct == max_repeat:
		print(distinct - 1)
		continue
	print(min(distinct, max_repeat))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	count = dict()
	for i in a:
		if count.get(i, 0) == 0:
			count[i] = 1
		else:
			count[i] += 1
	mset = set(a)
	m = 1
	for i in count:
		if count[i] > m:
			m = count[i]
	l = len(set(a))
	if m > l:
		print(l)
	elif m == l:
		print(l - 1)
	else:
		print(m)


from collections import Counter
for t in range(int(input())):
	n = int(input())
	a = Counter(list(map(int, input().split())))
	first = len(a)
	second = a.most_common()[0][1]
	if first == second:
		second -= 1
	print(min(first, second))


from collections import defaultdict
t = int(input())
while t > 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	d = defaultdict(int)
	if n == 1:
		print(0)
	else:
		for i in a:
			d[i] += 1
		dis = 0
		sam = 0
		for i in d:
			if d[i] > sam:
				sam = d[i]
				maxu = i
		for i in d:
			if d[i] == 1 or i != maxu:
				dis += 1
		if sam > dis + 1:
			dis += 1
			sam -= 1
		k = min(dis, sam)
		if k == 0 and n > 1:
			print(1)
		else:
			print(min(dis, sam))
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = [0] * n
	c = 0
	k = 0
	for i in range(n):
		if l1[l[i] - 1] == 0:
			c += 1
		l1[l[i] - 1] += 1
		k = max(k, l1[l[i] - 1])
	if k == c:
		c -= 1
	print(min(c, k))


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	a = set(l)
	a = len(a)
	d = {}
	for i in range(n):
		if l[i] in d:
			d[l[i]] += 1
		else:
			d[l[i]] = 1
	f = 0
	c = 0
	for v in d.values():
		if v > a:
			f = 1
			break
		elif v == a:
			c = 1
	if f == 1:
		print(a)
	elif c == 1:
		print(a - 1)
	else:
		Keymax = max(d, key=d.get)
		print(d[Keymax])


import sys
t = int(input())
for q in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	most = 0
	cur = 1
	a.sort()
	for i in range(1, n):
		if a[i] == a[i - 1]:
			cur += 1
		else:
			most = max(most, cur)
			cur = 1
	most = max(most, cur)
	s = set(a)
	if len(s) == most:
		print(most - 1)
	if len(s) > most:
		print(most)
	if len(s) < most:
		print(len(s))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(k) for k in input().split()]
	d = {}
	for j in a:
		if j in d:
			d[j] += 1
		else:
			d[j] = 1
	r = len(d)
	maximum = max(d.values())
	if r == maximum:
		print(r - 1)
	else:
		print(min(maximum, r))


def g(L, n):
	B = n
	A = [0] * n
	s = A[0]
	c = 0
	for i in range(n):
		A[L[i] - 1] += 1
	for j in range(n):
		if A[j] > s:
			s = A[j]
	for t in range(n):
		while A[t] > 1:
			A[t] -= 1
			c += 1
	c = n - c - 1
	if s > c + 1:
		return c + 1
	if s == c + 1:
		return c
	if s <= c:
		return s
t = int(input())
while t > 0:
	n = int(input())
	L = list(map(int, input().split()))
	print(g(L, n))
	t -= 1


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i not in d:
			d[i] = 0
		d[i] += 1
	same = max(d.values())
	dif = len(d) - 1
	print(min(same, dif + (same > dif + 1)))


from functools import reduce
import sys

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(delimiter=' '):
		return input().split(delimiter)

	@staticmethod
	def list_int(delimiter=' '):
		return list(map(int, input().split(delimiter)))

def get_key(d, value):
	for (k, v) in d.items():
		if v == value:
			return k
import collections

def main():
	t = Read.int()
	for _ in range(t):
		n = Read.int()
		a = Read.list_int()
		c = collections.Counter(a)
		b_max_val = max(c.values())
		am = set(a)
		a_max_val = len(am)
		if a_max_val == b_max_val:
			print(a_max_val - 1)
		else:
			print(min(a_max_val, b_max_val))
main()


from collections import Counter
import operator
for _ in range(int(input())):
	n = int(input())
	a = input().split()
	arr1 = []
	arr2 = []
	b = Counter(a)
	c = {}
	for (key, value) in b.items():
		c[key] = value
	most = max(c.items(), key=operator.itemgetter(1))[0]
	arr2 = [most] * c.get(most)
	for el in c.keys():
		if el is not most:
			arr1.append(el)
	if len(arr1) + 2 <= len(arr2):
		print(str(len(arr1) + 1))
	elif len(arr1) < len(arr2):
		print(str(len(arr1)))
	else:
		print(str(len(arr2)))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	t = 0
	c = []
	b = a[0]
	a.sort()
	if n == 1:
		print(0)
	else:
		for i in range(n):
			if i == n - 1:
				if a[i] == a[i - 1]:
					c.append(t + 1)
				else:
					c.append(t)
					c.append(1)
			elif a[i] == b:
				t += 1
			else:
				c.append(t)
				b = a[i]
				t = 1
		if 0 in c:
			c.remove(0)
		y = max(c)
		d = len(c) - 1
		if d > y:
			print(y)
		elif y - 2 >= d:
			print(d + 1)
		else:
			print(d)


import collections
t = int(input())
for num in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = set(a)
	d = collections.Counter(a)
	ans = 0
	for c in d:
		if d[c] > ans:
			ans = d[c]
	if ans != len(s):
		print(min(ans, len(s)))
	else:
		print(ans - 1)


N = int(input())
for n in range(N):
	T = int(input())
	LISTA = list(map(int, input().strip().split()))
	LISTA.sort()
	a = 1
	b = 0
	c = 1
	for s in range(T - 1):
		if LISTA[s] != LISTA[s + 1]:
			a = a + 1
			c = 1
		else:
			c = c + 1
		if c > b:
			b = c
	if a > b:
		print(b)
	if a == b:
		print(a - 1)
	if a < b:
		print(a)


def solve(skills, n, ans):
	if n == 1:
		ans.append(0)
		return
	dist_skills = {}
	for i in skills:
		if i not in dist_skills.keys():
			dist_skills[i] = 0
		dist_skills[i] += 1
	skills = []
	for i in dist_skills.keys():
		skills.append(dist_skills[i])
	skills.sort(reverse=True)
	if len(skills) == 1:
		ans.append(1)
		return
	if skills[0] > len(skills):
		ans.append(len(skills))
	elif skills[0] < len(skills):
		ans.append(skills[0])
	else:
		ans.append(skills[0] - 1)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		skills = list(map(int, input().split()))
		solve(skills, n, ans)
	for i in ans:
		print(i)
main()


t = int(input())
for tc in range(t):
	n = int(input())
	s = input().split()
	if n < 2:
		print(0)
	else:
		ma = 1
		c = 0
		num = [0 for i in range(n)]
		for i in s:
			k = int(i)
			if num[k - 1] == 0:
				num[k - 1] += 1
				c += 1
			else:
				num[k - 1] += 1
			if num[k - 1] > ma:
				ma = num[k - 1]
		if c == n:
			print(1)
		elif c == ma:
			print(c - 1)
		elif c < ma:
			print(c)
		else:
			print(ma)


import collections
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 2:
		print(0)
	else:
		c = collections.Counter(a).most_common()
		print(max(min(len(c) - 1, c[0][1]), min(len(c), c[0][1] - 1)))


from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	l = len(b)
	d = Counter(a)
	Keymax = max(d, key=d.get)
	m = d[Keymax]
	if l == m:
		print(l - 1)
	elif l > m:
		print(m)
	elif m > l:
		print(l)


import sys
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def bisearch_max(mn, mx, func):
	ok = mn
	ng = mx
	while ok + 1 < ng:
		mid = (ok + ng) // 2
		if func(mid):
			ok = mid
		else:
			ng = mid
	return ok

def check(m):
	if C[0][1] < m:
		return False
	elif C[0][1] == m:
		return len(C) - 1 >= m
	else:
		return len(C) >= m
for _ in range(INT()):
	N = INT()
	A = LIST()
	C = Counter(A).most_common()
	res = bisearch_max(0, N, check)
	print(res)


t = int(input())
while t > 0:
	n = int(input())
	l = input().split(' ')
	l = [int(i) for i in l]
	mp = {}
	maxEle = 0
	maxFreq = 0
	for i in l:
		if i in mp:
			mp[i] += 1
		else:
			mp[i] = 1
		if mp[i] > maxFreq:
			maxEle = i
			maxFreq = mp[i]
	remaining = len(mp) - 1
	if n == 1:
		print(0)
	elif maxFreq == remaining:
		print(maxFreq)
	elif maxFreq > remaining:
		if maxFreq - remaining == 1:
			print(remaining)
		else:
			print(remaining + 1)
	else:
		print(maxFreq)
	t -= 1


t = int(input())
for q in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	c1 = 0
	c2 = 1
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = 1
			c1 += 1
		else:
			d[a[i]] += 1
			if d[a[i]] > c2:
				c2 += 1
	if c1 == c2:
		print(c1 - 1)
	else:
		print(min(c1, c2))


def solve():
	n = int(input())
	l = [int(x) for x in input().split()]
	if n <= 1:
		print(0)
		return
	d = {}
	for i in range(n):
		if l[i] in d:
			d[l[i]] += 1
		else:
			d[l[i]] = 1
	s = list(d.values())
	s.sort(reverse=True)
	if s[0] == 1:
		print(1)
		return
	else:
		a = s[0]
		b = len(s) - 1
		if b < a:
			print(min(a - 1, b + 1))
		else:
			print(min(a, b))
		return
try:
	for _ in range(int(input())):
		solve()
except:
	pass


from collections import Counter as C
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = len(set(a))
	c = C(a)
	ans = -1
	for i in a:
		ans = max(ans, min(b - 1, c[i]), min(b, c[i] - 1))
	print(ans)


import sys
from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	students = [int(c) for c in input().split()]
	c = Counter(students)
	(top_value, top_count) = c.most_common(1)[0]
	unique_count = len(c)
	res1 = min(top_count, unique_count - 1)
	res2 = min(top_count - 1, unique_count)
	print(max(res1, res2))


from itertools import groupby
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(v) for v in input().split()]
	a.sort()
	g = [(v, len(list(gr))) for (v, gr) in groupby(a)]
	best_score = None
	for (v, vn) in g:
		option_a = min(len(g) - 1, vn)
		option_b = min(len(g), vn - 1)
		if best_score is None or option_a > best_score:
			best_score = option_a
		if best_score is None or option_b > best_score:
			best_score = option_b
	print(best_score)


from collections import Counter as c

def solve(n, array):
	arr = sorted(c(array).items(), key=lambda x: x[1], reverse=True)
	m = len(arr)
	res = 0
	for (num, count) in arr:
		(a, b) = (count, m - 1)
		if a > b:
			if a - b > 1:
				tmp = b + 1
			else:
				tmp = b
		else:
			tmp = a
		res = max(res, tmp)
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))
assert solve(7, [4, 2, 4, 1, 4, 3, 4]) == 3
assert solve(5, [2, 1, 5, 4, 3]) == 1
assert solve(1, [1]) == 0
assert solve(4, [1, 1, 1, 3]) == 2
assert solve(4, [1, 1, 2, 3]) == 2
assert solve(10, [1, 1, 1, 1, 1, 2, 2, 3, 3, 4]) == 4


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	count = dict()
	sum1 = 0
	for i in range(n):
		try:
			count[ar[i]] += 1
		except:
			count[ar[i]] = 1
		sum1 += count[ar[i]]
	res = 0
	len1 = len(count)
	for i in count.keys():
		if count[i] == len1 - 1:
			if res < len1 - 1:
				res = len1 - 1
		elif count[i] > len1 - 1:
			if count[i] - (len1 - 1) >= 2:
				if res < len1:
					res = len1
			elif res < len1 - 1:
				res = len1 - 1
		elif res < count[i]:
			res = count[i]
	print(res)


from collections import Counter
test = int(input())

def solve():
	n = int(input())
	array = list(map(int, input().split()))
	result = []
	p = Counter(array)
	m = p.most_common(1)[0][1]
	n = len(Counter(array))
	if n > m:
		print(m)
	if m > n:
		print(n)
	if m == n:
		print(m - 1)
for i in range(test):
	solve()


for ii in range(int(input())):
	n = int(input())
	p = [int(i) for i in input().split()]
	dic = {}
	for i in p:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	a = max(list(dic.values()))
	b = len(set(list(dic.keys())))
	if a == b:
		print(a - 1)
	elif b > a:
		print(a)
	else:
		print(b)


T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	m = 0
	for i in d:
		m = max(m, d[i])
	if n == 1:
		print(0)
	elif m > len(d):
		print(len(d))
	else:
		print(min(m, len(d) - 1))


def solve(n, arr):
	arr.sort()
	counts = []
	count = 1
	if len(arr) == 1:
		counts = [1]
	for i in range(1, n):
		if arr[i] == arr[i - 1]:
			count += 1
		else:
			counts.append(count)
			count = 1
		if i == n - 1:
			counts.append(count)
	maxx = max(counts)
	diff = len(counts)
	if maxx > diff:
		return diff
	elif maxx == diff:
		return diff - 1
	else:
		return maxx
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr = map(int, input().split())
	c = Counter(arr)
	li = list(c.values())
	m = max(li)
	m1 = len(li)
	print(max(min(m1 - 1, m), min(m1, m - 1)))


tc = int(input())
for _ in range(tc):
	n = int(input())
	L = list(map(int, input().split()))
	d = {}
	for i in L:
		try:
			d[i] += 1
		except:
			d[i] = 1
	cnt = len(d.items())
	L2 = list(d.items())
	ans = 0
	for i in L2:
		current = min(i[1], cnt - 1)
		ans = max(ans, current)
		current = min(i[1] - 1, cnt)
		ans = max(ans, current)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	count = [0 for i in range(n + 1)]
	for i in range(n):
		count[a[i]] += 1
	max1 = count[0]
	index = 0
	flag = 1
	one = 0
	multiple = 0
	r = max(count)
	counter1 = [0 for i in range(r + 1)]
	for i in range(n + 1):
		counter1[count[i]] += 1
	d = r
	ans = counter1[1]
	c = r
	for i in range(2, r):
		if counter1[i] != 0:
			ans += counter1[i]
	ans += counter1[-1] - 1
	if len(counter1) == 2:
		if counter1[-1] > 1:
			print(1)
		else:
			print(0)
	elif c == ans:
		print(ans)
	elif c - 1 == ans + 1:
		print(ans + 1)
	elif ans > c:
		print(c)
	elif ans + 1 <= c - 1:
		print(ans + 1)
	else:
		print(ans)


from collections import defaultdict

def main():
	tc_count = int(input())
	for tc in range(tc_count):
		n = int(input())
		lst = map(int, input().split())
		mp = defaultdict(int)
		max_elem = 0
		for x in lst:
			mp[x] += 1
			if mp[x] > max_elem:
				max_elem = mp[x]
		sol = None
		distinct = len(mp)
		if distinct == max_elem:
			sol = distinct - 1
		elif distinct < max_elem:
			sol = distinct
		else:
			sol = max_elem
		print('{}'.format(sol))
main()


from collections import Counter
for _ in range(int(input())):
	i_cyfr = int(input())
	odp = 0
	count = Counter([int(x) for x in input().split()])
	for x in count.values():
		odp = max(odp, min(len(count), x - 1), min(len(count) - 1, x))
	print(odp)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	(s1, s2) = (set(), set())
	d = dict()
	for i in l:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	nd = sorted(d.items(), key=lambda x: {x[1], x[0]})
	for i in range(len(nd)):
		if nd[i][1] == 1:
			s2.add(nd[i][0])
		else:
			s1.add(nd[i][0])
	Size = len(s2)
	if len(s1) == 0:
		if len(s2) > 1:
			print(1)
		else:
			print(0)
		continue
	maxi = 0
	num = 0
	for i in s1:
		if d[i] > maxi:
			maxi = d[i]
			num = i
	if maxi <= Size:
		print(maxi)
		continue
	else:
		factor = maxi - Size
		mini = min(len(s1) - 1, factor)
		Size += mini
		if maxi >= Size + 2:
			Size += 1
		print(Size)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = {}
	for elem in a:
		if elem in cnt:
			cnt[elem] += 1
		else:
			cnt[elem] = 1
	same = max(cnt.values())
	diff = len(cnt)
	if same == diff:
		print(same - 1)
	else:
		print(min(same, diff))


import sys
import math
from collections import Counter
input = sys.stdin.readline
from functools import lru_cache
import bisect

def solve():
	n = int(input())
	arr = list(map(int, input().split(' ')))
	count = Counter(arr)
	maxKey = max(count, key=count.get)
	others = len(count.keys()) - 1
	print(max(min(others, count[maxKey]), min(others + 1, count[maxKey] - 1)))
t = int(input())
for _ in range(t):
	solve()


import math
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = [0 for j in range(max(a))]
	for j in a:
		l[j - 1] += 1
	l1 = len(l) - l.count(0)
	if n < 2:
		print(0)
	elif max(l) < l1:
		print(max(l))
	elif max(l) == l1:
		print(max(l) - 1)
	else:
		print(l1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	dic = {}
	dis = 0
	for val in arr:
		if val in dic:
			dic[val] = dic[val] + 1
		else:
			dis += 1
			dic[val] = 1
	value = sorted(dic.values(), reverse=True)
	if value[0] > dis:
		print(dis)
	elif value[0] == dis:
		print(dis - 1)
	else:
		print(value[0])


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	from collections import defaultdict
	d = defaultdict(int)
	for i in arr:
		d[i] += 1
	mx = max(d.values())
	print(min(len(d), mx) - int(len(d) == mx))
	t = t - 1


from sys import stdin, stdout
import traceback
import math
import bisect
from typing import List, Tuple
import itertools as it
from collections import Counter
read = lambda : stdin.readline().strip()
write = stdout.write
writeln = lambda x: write(str(x) + '\n')

def reads(typ=int):
	return list(map(typ, read().split()))

def enumerate_sorted(iterable):
	return sorted(zip(it.count(), iterable), key=lambda x: x[1])

class Sol:

	def __init__(self, n, aa):
		self.n = n
		self.aa = aa

	@classmethod
	def input(cls):
		n = int(read())
		aa = reads()
		return cls(n, aa)

	def solve(self):
		counts = Counter(self.aa).most_common()
		num_unique = len(counts)
		max_same = counts[0][1]
		s1 = min(max_same - 1, num_unique, self.n // 2)
		s2 = min(max_same, num_unique - 1, self.n // 2)
		return max(s1, s2)

def mytest():
	print(Sol(6, [4, 4, 4, 4, 2, 2]).solve())
	print(Sol(6, [4, 4, 4, 3, 3, 2, 2]).solve())
	print(Sol(6, [4, 4, 4, 3, 3, 4, 2, 2]).solve())
	print(Sol(6, [4, 1, 4, 3, 3, 4, 2, 2]).solve())
	print(Sol(6, [4, 1, 4, 3, 3, 1, 2, 2]).solve())

def main():
	try:
		t = int(read())
		for _ in range(t):
			res = Sol.input().solve()
			writeln(res)
	except Exception as e:
		print('Got exception:', repr(e))
		print(traceback.format_exc())
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = [0] * (n + 1)
	t1 = len(list(set(l)))
	for i in range(n):
		l1[l[i]] += 1
	ans = 0
	for i in range(len(l1)):
		if l1[i] > ans:
			if l1[i] == t1 - 1:
				ans = l1[i]
			elif l1[i] - 1 == t1:
				ans = l1[i] - 1
			elif l1[i] == t1:
				ans = t1 - 1
			elif l1[i] > t1:
				ans = t1
			else:
				ans = l1[i]
	print(ans)


def countFreq(arr, n):
	mp = {}
	for i in range(n):
		if arr[i] not in mp:
			mp[arr[i]] = 0
		mp[arr[i]] += 1
	arr1 = []
	for i in range(n):
		if mp[arr[i]] != -1:
			arr1.append(mp[arr[i]])
		mp[arr[i]] = -1
	return arr1
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		arr1 = countFreq(arr, n)
		a = max(arr1)
		b = len(arr1) - 1
		if a - b > 1:
			print(b + 1)
		else:
			print(min(a, b))


from sys import stdin, stdout
from math import sqrt, ceil, gcd, log, log10
from collections import defaultdict

def rl(f=int):
	return [f(x) for x in stdin.readline().split()]

def rls(f=int):
	return [f(x) for x in stdin.readlines()]
t = rl()[0]
for i in range(t):
	n = rl()[0]
	l = rl()
	d = defaultdict(int)
	for x in l:
		d[x] += 1
	distinct = len(d)
	largest = max(d.values())
	if largest > distinct:
		print(distinct)
	elif largest == distinct:
		print(distinct - 1)
	else:
		print(largest)


rw = int(input())
for ewqr in range(rw):
	n = int(input())
	a = list(map(int, input().split()))
	ls = len(set(a))
	b = {}
	for i in range(n):
		b[a[i]] = 0
	for i in range(n):
		b[a[i]] += 1
	m = 0
	for i in range(n):
		m = max(m, b[a[i]])
	x1 = min(ls - 1, m)
	x2 = min(ls, m - 1)
	x = max(x1, x2)
	print(x)


from collections import Counter
for _ in range(int(input())):
	size = input()
	teams = Counter(map(int, input().split()))
	com = teams.most_common(1)[0][1]
	distinct = len(teams)
	if com - 1 >= distinct:
		print(distinct)
	else:
		print(min(com, distinct - 1))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = Counter(l)
	ma = l[0]
	for i in list(d.keys()):
		if d[i] > d[ma]:
			ma = i
	a = len(list(d.keys())) - 1
	if a >= d[ma]:
		print(d[ma])
	elif a + 1 == d[ma]:
		print(a)
	else:
		print(a + 1)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	liste = list(map(int, sys.stdin.readline().split()))
	diff = 0
	maxi = 0
	seen = [0] * (n + 3)
	for i in range(n):
		if seen[liste[i]] == 0:
			diff += 1
		seen[liste[i]] += 1
		maxi = max(maxi, seen[liste[i]])
	if maxi == diff:
		print(maxi - 1)
	if maxi > diff:
		print(diff)
	if diff > maxi:
		print(maxi)


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	v = map(int, input().split())
	freq = {}
	for i in v:
		try:
			freq[i] += 1
		except:
			freq[i] = 1
	dist = len(freq)
	ms = max(freq.values())
	if ms == dist:
		print(ms - 1)
	else:
		print(min(dist, ms))


from collections import Counter
T = int(input())
i = 0
while i < T:
	i += 1
	n = int(input())
	d = Counter([int(i) for i in input().split()])
	if d.most_common(1)[0][1] > len(d) - 1:
		print(min(d.most_common(1)[0][1] - 1, len(d)))
	else:
		print(d.most_common(1)[0][1])


from collections import Counter
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	freq = Counter(a)
	max = 0
	for i in freq:
		if freq[i] > max:
			max = freq[i]
	dist = len(set(a)) - 1
	if dist == max:
		print(max)
	elif dist > max:
		print(max)
	else:
		print(min(max - 1, dist + 1))


from bisect import bisect_left

def solve():
	n = int(input())
	sk = list(map(int, input().split()))
	if n == 1:
		print(0)
		return
	skillmap = {}
	for i in sk:
		if i in skillmap:
			skillmap[i] += 1
		else:
			skillmap[i] = 1
	skc = len(skillmap.keys())
	bsk = max(skillmap.values())
	if skc > bsk:
		print(bsk)
	else:
		print(min(skc, bsk - 1))
t = int(input())
for _ in range(t):
	solve()


from collections import Counter
for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	cnt = Counter(arr)
	dist = len(cnt) - 1
	ans = 0
	for i in cnt:
		val = cnt[i]
		if val > dist:
			ans = max(ans, min(dist + 1, val - 1))
		elif val == dist:
			ans = max(ans, dist)
		else:
			ans = max(ans, val)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ar = [int(x) for x in input().split()]
	d = {}
	for i in ar:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	dis = len(d) - 1
	mx = 0
	for i in d.keys():
		mx = max(mx, d[i])
	if dis > mx:
		print(mx)
	elif dis == mx:
		print(mx)
	elif mx > dis + 1:
		print(dis + 1)
	elif mx > dis:
		print(dis)


def can_compose(n, A, i, d, max_num):
	if i > n / 2:
		return False
	if not max_num >= i:
		return False
	return len(d) >= i if max_num > i else len(d) - 1 >= i

def solve(n, A):
	d = dict()
	max_num = float('-inf')
	for a in A:
		if a in d:
			d[a] += 1
		else:
			d[a] = 1
		max_num = max(d[a], max_num)
	i = len(d)
	while not can_compose(n, A, i, d, max_num) and i > 0:
		i -= 1
	return i
T = int(input())
for _ in range(T):
	n = int(input())
	A = [int(i) for i in input().split()]
	print(solve(n, A))


from sys import stdin
input = stdin.readline

def two(arr):
	cnter = [0] * (len(arr) + 1)
	for i in arr:
		cnter[i] += 1
	maxi = cnter.index(max(cnter))
	if len(arr) == 1:
		return 0
	temp = [maxi] * arr.count(maxi)
	lst = list(set(arr))
	lst.remove(maxi)
	t = len(temp)
	a = len(lst)
	if t > a:
		a += 1
		t -= 1
	return min(a, t)
t = int(input())
for i in range(t):
	a = input()
	lst = list(map(int, input().strip().split()))
	print(two(lst))


t = int(input())
from collections import Counter

def most_frequent(List):
	occurence_count = Counter(List)
	return occurence_count[occurence_count.most_common(1)[0][0]]
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if len(set(a)) == len(a):
		if n == 1:
			print(0)
		else:
			print(1)
	else:
		d = most_frequent(a)
		j = len(set(a))
		if j > d:
			print(min(j - 1, d))
		else:
			print(min(j, d - 1))


from math import *
import sys
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	LL = []
	for j in range(n + 1):
		LL.append([j, 0])
	MAX = 0
	for j in range(len(l)):
		LL[l[j]][1] += 1
		if LL[l[j]][1] >= MAX:
			MAX = LL[l[j]][1]
			ELE = LL[l[j]][0]
	if MAX == 1:
		print(1)
		continue
	TEMP = []
	for j in range(len(l)):
		if l[j] != ELE:
			TEMP.append(l[j])
	h = list(set(TEMP))
	leftover = len(h)
	if leftover >= MAX:
		print(MAX)
	elif MAX - leftover == 1:
		print(leftover)
	else:
		print(leftover + 1)


from collections import Counter
import operator
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	c = dict(Counter(s))
	c = sorted(c.items(), key=operator.itemgetter(1))
	diff = len(c)
	maxi = c[-1][1]
	if diff == maxi:
		print(maxi - 1)
	elif diff < maxi:
		print(diff)
	else:
		print(maxi)


for k in range(int(input())):
	n = int(input())
	t = list(map(int, input().split()))
	f = {}
	for j in t:
		if j not in f:
			f[j] = 1
		else:
			f[j] += 1
	b = list(f.values())
	x = len(b)
	u = max(b)
	print(max(min(u - 1, x), min(x - 1, u)))


t = input()
i = 0
for i in range(0, int(t)):
	a = []
	d = {}
	try:
		n = input()
	except EOFError:
		pass
	try:
		k = input()
	except EOFError:
		pass
	a = k.split(' ')
	for x in a:
		if x in d.keys():
			d[x] += 1
		else:
			d[x] = 1
	p = 0
	for j in d.values():
		if j > p:
			p = j
	if len(d.keys()) > p:
		print(p)
	elif len(d.keys()) < p:
		print(len(d.keys()))
	else:
		print(p - 1)


t = int(input())
for i in range(t):
	n = int(input())
	numd = {}
	for j in [int(z) for z in input().split()]:
		try:
			numd[j] += 1
		except KeyError:
			numd[j] = 1
	l = 0
	for j in numd.keys():
		if l < numd[j]:
			l = numd[j]
	d = len(numd)
	x = n // 2
	ans1 = min(l, d - 1, x)
	ans2 = min(l - 1, d, x)
	print(max(ans1, ans2))


test = int(input())
sol = []
while test:
	test -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	if n == 1:
		sol.append(0)
	else:
		dic = {}
		for i in a:
			if not dic.get(i):
				dic[i] = 0
			dic[i] += 1
		maxi = 0
		h = len(dic)
		for i in dic.values():
			if i > h:
				maxi = max(h, maxi)
			elif i == h:
				maxi = max(i - 1, maxi)
			else:
				maxi = max(i, maxi)
		sol.append(maxi)
for i in sol:
	print(i)


for _ in range(int(input())):
	x = int(input())
	arr = [0] * (x + 1)
	lst = list(map(int, input().split()))
	for item in lst:
		arr[item] += 1
	k = max(arr)
	p = x + 1 - arr.count(0)
	l1 = min(k - 1, p)
	l2 = min(k, p - 1)
	print(max(l1, l2))


from collections import Counter

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		d = dict(Counter(a))
		a1 = max(list(d.values()))
		b1 = len(set(a))
		if b1 - 1 >= a1:
			print(a1)
		elif b1 >= a1 - 1:
			print(a1 - 1)
		elif a1 - 1 > b1:
			print(b1)
		elif a1 > b1 - 1:
			print(b1 - 1)
		else:
			print(1)
main()


import itertools
n = int(input())
for i in range(n):
	a = int(input())
	b = sorted(list(map(int, input().split())))
	z = itertools.groupby(b)
	ans = []
	for (key, group) in z:
		ans.append(len(list(group)))
	if len(b) == 1:
		print(0)
	elif len(set(b)) == len(b):
		print(1)
	elif len(set(b)) == max(ans):
		print(max(ans) - 1)
	else:
		print(min(len(set(b)), max(ans)))


for _ in range(int(input())):
	n = int(input())
	a = map(int, input().split())
	b = [0] * n
	for i in a:
		b[i - 1] += 1
	cnt = b.count(0)
	cnt = n - cnt
	maxm = max(b)
	if maxm < cnt:
		print(maxm)
	elif maxm == cnt:
		print(maxm - 1)
	else:
		print(cnt)


import sys
import collections
import threading
import string

def main():
	testn = int(input())
	for _ in range(testn):
		n = int(input())
		a = list(map(int, input().split()))
		d = collections.Counter(a)
		m1 = d.most_common(1)[0][1]
		m2 = len(d) - 1
		print(max(min(m1, m2), min(m1 - 1, m2 + 1)))
input = sys.stdin.readline
main()


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	ls = [0 for i in range(n + 1)]
	for i in range(n):
		ls[a[i]] += 1
	dis = len(set(a))
	sam = max(ls)
	print(max(min(dis - 1, sam), min(dis, sam - 1)))
	t -= 1


from collections import Counter

def main():
	test = int(input())
	for t in range(test):
		n = int(input())
		l = [int(i) for i in input().split(' ')]
		d = dict(Counter(l))
		m = 0
		x = 0
		for i in d.keys():
			m = max(m, d.get(i))
		if n % 2 == 0:
			if m - len(d.keys()) > 1:
				x = len(d.keys())
			elif m == len(d.keys()) - 1:
				x = m
			elif m < len(d.keys()) - 1:
				x = m
			else:
				x = m - 1
		elif m > len(d.keys()):
			x = len(d.keys())
		elif m == len(d.keys()):
			x = m - 1
		else:
			x = m
		print(x)
main()


import sys
from collections import Counter
INT_MAX = sys.maxsize
INT_MIN = -sys.maxsize
sys.setrecursionlimit(10 ** 7)
mod = 1000000007

def IOE():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	t = int(sys.stdin.readline())
	for _ in range(t):
		n = int(sys.stdin.readline())
		a = list(map(int, sys.stdin.readline().split()))
		if n == 1:
			print(0)
		else:
			s = set(a)
			d = Counter(a)
			max_freq = 0
			for (k, v) in d.items():
				max_freq = max(v, max_freq)
			if len(s) == max_freq:
				print(len(s) - 1)
			else:
				print(min(len(s), max_freq))
try:
	IOE()
except:
	pass
main()


from collections import Counter
import sys
readline = sys.stdin.readline
for _ in range(int(input())):
	n = int(readline())
	cnt = Counter(map(int, readline().split()))
	key_n = len(list(cnt.keys())) - 1
	val_n = cnt.most_common(1)[0][1]
	print(max(min(key_n, val_n), min(key_n + 1, val_n - 1)))


t = int(input())
for i in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	m = [0 for i in range(n)]
	for i in lis:
		m[i - 1] += 1
	maxy = 0
	diff = 0
	for i in m:
		if i > maxy:
			maxy = i
		if i:
			diff += 1
	if n == 1:
		print(0)
	elif diff > 0 and maxy == 1:
		print(1)
	elif maxy - 1 >= diff:
		print(diff)
	elif maxy == diff:
		print(maxy - 1)
	else:
		print(min(maxy, diff))


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	d = len(set(A))
	C = {}
	for i in range(n):
		if A[i] not in C:
			C[A[i]] = 1
		else:
			C[A[i]] += 1
	max_ = 0
	for v in C.values():
		if v >= max_:
			max_ = v
	ans = 0
	for x in range(0, max_ + 1):
		if x != max_:
			if x <= d:
				ans = x
		elif x <= d - 1:
			ans = x
	print(ans)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	au = len(set(a))
	(val, ad) = Counter(a).most_common(1)[0]
	if au < ad:
		print(min(au, n // 2))
	elif ad < au:
		print(min(ad, n // 2))
	else:
		print(min(au - 1, n // 2))


from collections import Counter
from itertools import repeat, chain
cases = int(input())
ans = []
while cases:
	cases -= 1
	n = int(input())
	students = [int(x) for x in input().split(' ')]
	cpy = students.copy()
	students = list(chain.from_iterable((repeat(i, c) for (i, c) in Counter(cpy).most_common())))
	m = students[0]
	count = 0
	while count < len(students) and m == students[count]:
		count += 1
	unique = set(students[count:])
	max_diff = len(unique)
	res = min(count, max_diff)
	if count - max_diff > 1:
		res += 1
	ans.append(res)
for a in ans:
	print(a)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dict1 = dict(Counter(a))
	key = list(dict1.keys())
	arr = []
	for x in dict1:
		arr.append([dict1[x], x])
	arr.sort()
	arr.reverse()
	if arr[0][0] == len(key) - 1:
		print(len(key) - 1)
	elif arr[0][0] == len(key):
		print(len(key) - 1)
	elif arr[0][0] > len(key):
		print(len(key))
	else:
		print(arr[0][0])


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()
res = ['YES', 'NO']
let = [chr(i) for i in range(97, 123)]

def main():
	n = ii()
	a = li()
	if n == 1:
		print(0)
		return
	cnt = [0] * (n + 1)
	dis = 0
	for i in a:
		cnt[i] += 1
		if cnt[i] == 1:
			dis += 1
	mxc = max(cnt)
	ans = min(dis, mxc)
	if mxc == dis:
		ans -= 1
	print(ans)
	return
for _ in range(ii()):
	main()


from collections import Counter as cc
for _ in range(int(input())):
	n = input()
	a = list(map(int, input().split()))
	x = len(set(a))
	d = cc(a)
	b = list(d.values())
	y = max(b)
	ans = 0
	if y > x:
		ans = x
	elif y == x:
		ans = x - 1
	else:
		ans = y
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	s = -357
	c = 357
	sk = -357
	for i in range(n - 1):
		if a[i] == a[i + 1] and c == 357:
			hi = i
			c = 0
			sk = 2
		elif a[i] == a[i + 1]:
			sk += 1
			if sk > s:
				s = sk
		elif a[i] != a[i + 1]:
			c = 357
			if sk > s:
				s = sk
			sk = 0
	k = len(list(set(a)))
	if sk > s:
		s = sk
	if k == s:
		k -= 1
	if s == -357 or s == 0:
		if n > 1:
			print(1)
		else:
			print(0)
	elif min(k, s) <= len(a) // 2:
		print(min(k, s))
	else:
		print(min(k, s) - 1)


t = int(input())
for _ in range(t):
	n = int(input())
	d = dict()
	l = list(map(int, input().split()))
	for i in l:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	dis = 0
	sam = 0
	c = 0
	for i in d.keys():
		if d[i] > sam:
			sam = d[i]
			dis += 1
		else:
			dis += 1
	print(max(min(dis - 1, sam), min(dis, sam - 1)))


t = int(input())
from collections import Counter
for i in range(t):
	n = int(input())
	s = [int(i) for i in input().split()]
	s1 = list(set(s))
	x = Counter(s).most_common(1)
	m = x[0][0]
	w = x[0][1]
	if w > len(s1):
		print(len(s1))
	elif w == len(s1):
		if not m in s1:
			print(w)
		else:
			print(w - 1)
	else:
		print(w)


import math
from collections import Counter
n = int(input())
for i in range(n):
	c = int(input())
	students = [int(x) for x in input().split()]
	cs = Counter(students)
	diff_symbols = int(len(set(students)))
	result = max(min(diff_symbols - 1, cs.most_common()[0][1]), min(diff_symbols, cs.most_common()[0][1] - 1))
	print(result)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = {}
	for i in range(len(a)):
		if a[i] in b:
			b[a[i]] += 1
		else:
			b[a[i]] = 1
	t = len(b)
	c = []
	d = sorted(list(b.items()), key=lambda x: x[1], reverse=True)
	(r, s) = d[0]
	if s > t - 1:
		if s - 1 == t - 1:
			print(t - 1)
		else:
			print(t)
	elif s == t - 1:
		print(t - 1)
	elif s < t - 1:
		print(s)


from collections import Counter
t = int(input())
s = []
for jfnjf in range(0, t):
	n = int(input())
	v = list(map(int, input().split()))
	p = Counter(v)
	o = len(p)
	y = -10
	for i in p:
		if p[i] > y:
			y = p[i]
	s.append(max(min(y, o - 1), min(y - 1, o)))
for i in s:
	print(i)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	d = dict(Counter(a))
	x = max(min(len(d.keys()), max(d.values()) - 1), min(len(d.keys()) - 1, max(d.values())))
	if n == 1:
		print(0)
	elif n > 1 and x <= 0:
		print(1)
	else:
		print(x)


def find_max(a, n):
	if n == 1:
		return 0
	elif n == 2:
		return 1
	x = len(set(a))
	c = [0] * (n + 1)
	for i in a:
		c[i] += 1
	y = max(c)
	if x == y:
		return x - 1
	else:
		return min(x, y)
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	res = find_max(a, n)
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	skills = list(map(int, input().split()))
	skill_set = set(skills)
	counts = {s: 0 for s in skill_set}
	mx = -1
	mx_skill = 0
	for s in skills:
		counts[s] += 1
		if mx < counts[s]:
			mx = counts[s]
			mx_skill = s
	res = min(len(skill_set) - 1, mx)
	if mx > len(skill_set):
		res += 1
	print(res)


T = int(input())
for tc in range(T):
	n = int(input())
	MAP = list(map(int, input().split()))
	d = dict()
	ma = 1
	ln = 0
	qq = n // 2
	for m in MAP:
		if d.get(m):
			d[m] += 1
			ma = max(ma, d[m])
		else:
			d[m] = 1
			ln += 1
	if ln == ma:
		ans = ma - 1
	else:
		ans = min(ma, ln)
	print(ans)


for f in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	pr = -1
	cur = 0
	diff = 0
	ma = 0
	for i in range(n):
		if a[i] == pr:
			cur += 1
		else:
			diff += 1
			ma = max(ma, cur)
			cur = 1
			pr = a[i]
	ma = max(ma, cur)
	if ma > diff:
		print(diff)
	if diff > ma:
		print(ma)
	if diff == ma:
		print(ma - 1)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	x = Counter(map(int, input().split()))
	mx = -1
	for i in x:
		mx = max(mx, x[i])
	l = x.__len__()
	size = min(l - 1, mx)
	diff = mx - l + 1
	if diff <= 1:
		print(size)
	else:
		print(size + 1)


T = int(input())
for t in range(T):
	P = int(input())
	LISTA = list(map(int, input().strip().split()))
	LISTA.sort()
	a = 1
	b = 0
	c = 1
	for s in range(P - 1):
		if LISTA[s] != LISTA[s + 1]:
			a = a + 1
			c = 1
		else:
			c = c + 1
		if c > b:
			b = c
	if a > b:
		print(b)
	if a == b:
		print(a - 1)
	if a < b:
		print(a)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def solution():
	for i in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		d = Counter(a)
		m = max(d.values())
		l = len(set(a))
		if m > l:
			print(l)
		elif m == l:
			print(l - 1)
		else:
			print(m)
solution()


from math import ceil
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	uniques = list(set(a))
	freq = {}
	for i in range(len(a)):
		if a[i] not in freq:
			freq[a[i]] = 1
		else:
			freq[a[i]] += 1
	maxfreq = max(list(freq.values()))
	unique_len = len(uniques)
	ans_case1 = min(maxfreq, unique_len - 1)
	ans_case2 = min(maxfreq - 1, unique_len)
	print(max(ans_case1, ans_case2))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [0] * n
	k = 0
	c = 0
	for j in a:
		if b[j - 1] == 0:
			k += 1
		b[j - 1] += 1
		if b[j - 1] > c:
			c = b[j - 1]
	if k == c:
		print(c - 1)
	else:
		print(min(k, c))


from collections import Counter

def teams(arr):
	counts = Counter(arr)
	b = max(counts.values())
	d = len(counts.keys())
	return max(min(d - 1, b), min(b - 1, d))
tests = int(input())
for i in range(tests):
	inp = int(input())
	arr = list(map(int, input().split()))
	result = teams(arr)
	print(result)


from collections import Counter
q = int(input())
while q != 0:
	q -= 1
	n = int(input())
	a = list(map(int, input().split()))
	d = dict(Counter(a))
	if max(d.values()) > len(d):
		print(min(max(d.values()), len(d)))
	else:
		print(min(max(d.values()), len(d) - 1))


from collections import Counter

def solve(arr, n):
	unique = list(set(arr))
	aa = Counter(arr)
	max_count = max(aa.values())
	if max_count < len(unique):
		return max_count
	elif max_count == len(unique):
		return max_count - 1
	return len(unique)
t = int(input())
ans = []
for s in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans.append(solve(arr, n))
for it in ans:
	print(it)


t = int(input())
for i in range(t):
	n = int(input())
	ai = [int(i) for i in input().split()]
	li = [0] * (n + 1)
	for j in range(n):
		li[ai[j]] += 1
	if max(li) == len(set(ai)):
		print(min(max(li) - 1, n // 2, len(set(ai)) - 1))
	else:
		print(min(max(li), n // 2, len(set(ai))))


def r(t=int):
	return list(map(t, input().split()))

def ri(t=int):
	return list(map(t, input().split()))[0]

def cd(it):
	ret_val = dict()
	for v in it:
		ret_val[v] = ret_val.get(v, 0) + 1
	return ret_val
for _ in range(ri()):
	n = ri()
	a = r()
	f = cd(a)
	first = len(f.keys())
	second = max(f.values())
	if first < second:
		print(first)
	elif second < first:
		print(second)
	else:
		print(second - 1)


def help():
	n = int(input())
	arr = list(map(int, input().split(' ')))
	dicti = {}
	for i in range(n):
		dicti[arr[i]] = dicti.get(arr[i], 0) + 1
	maxo = 0
	maxc = ''
	for i in dicti:
		if dicti[i] > maxo:
			maxc = i
			maxo = dicti[i]
	ll = len(dicti) - 1
	if maxo == ll or maxo == ll + 1:
		print(ll)
	elif maxo > ll + 1:
		print(ll + 1)
	else:
		print(maxo)
for qw in range(int(input())):
	help()


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = Counter(arr)
	same = 0
	dis = 0
	rep = -1
	for i in dic:
		if same < dic[i]:
			same = dic[i]
			rep = i
	ans2 = set([])
	if same == 1 and n > 1:
		print(1)
	else:
		if n == 1:
			print(0)
			continue
		for i in dic:
			if i != rep:
				ans2.add(i)
		if same - len(ans2) >= 2:
			print(len(ans2) + 1)
		else:
			print(min(same, len(ans2)))


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = set(a)
	c = [0 for i in range(n + 1)]
	for i in range(n):
		c[a[i]] += 1
	ma = max(c)
	if len(b) == ma:
		print(ma - 1)
	else:
		print(min(len(b), ma))


from collections import Counter as co
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		d = co(a)
		m = max(d.values())
		l = len(d)
		if m == l:
			print(min(m, l) - 1)
		else:
			print(min(m, l))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k1 = []
	s = set()
	f = Counter(l)
	k2 = list(f.values())
	k2.sort(reverse=True)
	for x in l:
		if x not in s:
			k1.append(x)
			s.add(x)
	if n == 1:
		print(0)
	elif len(k1) == k2[0]:
		print(k2[0] - 1)
	else:
		print(min(len(k1), k2[0]))


import collections
test = int(input())
while test:
	test -= 1
	n = int(input())
	ls = list(map(int, input().split()))
	u = len(set(ls))
	dic = collections.Counter(ls)
	m = max(dic.values())
	if u > m:
		print(m)
	elif m > u:
		print(u)
	else:
		print(u - 1)


for t in range(int(input())):
	n = int(input())
	abc = list(map(int, input().split()))
	sett = {ele for ele in abc}
	cnt = [0] * (n + 1)
	for i in abc:
		cnt[i] += 1
	print(max(min(len(sett), max(cnt) - 1), min(len(sett) - 1, max(cnt))))


from collections import Counter

def size_of_team(students):
	skill_count = Counter(students)
	skills_number = len(skill_count)
	max_team_size = 0
	for count in skill_count.values():
		team_size = max(min(count, skills_number - 1), min(count - 1, skills_number))
		max_team_size = max(team_size, max_team_size)
	return max_team_size
test_number = int(input())
for _ in range(test_number):
	input()
	students = [int(x) for x in input().split()]
	result = size_of_team(students)
	print(result)


import collections
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	m = {}
	for i in arr:
		m[i] = m.get(i, 0) + 1
	sorted_m = collections.OrderedDict(sorted(m.items(), reverse=True))
	k = len(sorted_m) - 1
	max_len = 0
	for i in sorted_m.values():
		p = min(i, k)
		if i - 1 > k:
			p += 1
		if p > max_len:
			max_len = p
	print(max_len)


def teams(n, skills):
	nb_of_skilled = [0 for i in range(n)]
	for student in range(n):
		nb_of_skilled[skills[student] - 1] += 1
	maxi_of_skilled = 0
	nb_of_distinct_skills = 0
	for skill in range(n):
		if nb_of_skilled[skill] > maxi_of_skilled:
			maxi_of_skilled = nb_of_skilled[skill]
		if nb_of_skilled[skill] > 0:
			nb_of_distinct_skills += 1
	if maxi_of_skilled == nb_of_distinct_skills:
		return maxi_of_skilled - 1
	else:
		return min(maxi_of_skilled, nb_of_distinct_skills)

def test():
	number_of_tests = int(input())
	for i in range(number_of_tests):
		n = int(input())
		skills_list = []
		line = input().split(' ')
		for skill in range(n):
			skills_list.append(int(line[skill]))
		output = str(teams(n, skills_list))
		print(output)
	return ()
test()


import sys
input = sys.stdin.readline
t = int(input())
from collections import Counter
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = Counter(a)
	ans = 0
	c_keys = c.keys()
	for i in c_keys:
		ans = max(ans, min(len(c_keys) - 1, c[i]))
		if c[i] > 1:
			ans = max(ans, min(len(c_keys), c[i] - 1))
	print(ans)


from collections import Counter
TC = int(input())
for _ in range(TC):
	n = int(input())
	a = list(map(int, input().split()))
	counter = Counter(a)
	freq_max_count = max(counter.values())
	unique = len(counter)
	if unique == freq_max_count:
		print(unique - 1)
	else:
		print(min(unique, freq_max_count))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	rasn = 1
	sim = 1
	now_sim = 1
	for j in range(1, n):
		if a[j] != a[j - 1]:
			sim = max(sim, now_sim)
			rasn += 1
			now_sim = 1
		else:
			now_sim += 1
	sim = max(sim, now_sim)
	if rasn < 2:
		if sim == 1:
			print(0)
		else:
			print(1)
	elif sim > rasn:
		print(rasn)
	elif rasn == sim:
		print(rasn - 1)
	else:
		print(sim)


for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	dic = {}
	for i in l:
		if i not in dic:
			dic[i] = 1
		else:
			dic[i] += 1
	r1 = max(dic.values())
	r2 = len(dic)
	if r1 == r2:
		print(r2 - 1)
	else:
		print(min(r1, r2))


def counter(arr):
	counts = {}
	for el in arr:
		counts[el] = counts.get(el, 0) + 1
	return counts
arr = [4, 2, 4, 1, 4, 3, 4]

def two_teams_coop(arr):
	skills_c = len(set(arr))
	counts = counter(arr)
	max_c = 0
	for c in counts:
		if counts[c] > max_c:
			max_c = counts[c]
	if max_c == skills_c:
		print(max_c - 1)
	elif max_c > skills_c:
		print(skills_c)
	else:
		print(max_c)

def read():
	length = input()
	arr = input()
	arr = [int(d) for d in arr.split()]
	return arr
tests = int(input())
arrs = [read() for _ in range(tests)]
[two_teams_coop(arr) for arr in arrs]


t = int(input())
for i in range(t):
	N = int(input())
	riga = list(map(int, input().strip().split()))
	riga.sort()
	a = 1
	b = 0
	c = 1
	for n in range(N - 1):
		if riga[n] != riga[n + 1]:
			a += 1
			c = 1
		else:
			c += 1
		if c > b:
			b = c
	if a > b:
		print(b)
	if a == b:
		print(a - 1)
	if a < b:
		print(a)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c = Counter(arr)
	arr = set(arr)
	maxe = 0
	for j in arr:
		if c[j] > maxe:
			maxe = c[j]
	leng = len(arr)
	leng -= 1
	if leng == maxe:
		print(leng)
	elif maxe > leng:
		if maxe - leng > 1:
			print(leng + 1)
		else:
			print(leng)
	else:
		print(maxe)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split(' ')))
	x = {}
	x1 = {}
	ma = 0
	for i in a:
		if i not in x:
			x[i] = 1
			if x[i] > ma:
				ma = x[i]
		else:
			x[i] += 1
			if x[i] > ma:
				ma = x[i]
	cnt = len(list(set(a))) - 1
	cnt1 = 0
	if n == 1:
		print(0)
	elif cnt < ma:
		pp = ma - cnt
		if ma - cnt >= 2:
			cnt += 1
		print(cnt)
	else:
		print(ma)


import sys

def main():
	test_cases = int(sys.stdin.readline())
	for test_case in range(test_cases):
		n = int(sys.stdin.readline())
		nums = sys.stdin.readline().split()
		nums = [int(num) for num in nums]
		dict = {}
		for num in nums:
			dict[num] = dict.get(num, 0) + 1
		distinct_numbers = len(dict)
		highest_number_of_repeats = 0
		for key in dict:
			highest_number_of_repeats = max(highest_number_of_repeats, dict[key])
		if distinct_numbers < highest_number_of_repeats:
			print(distinct_numbers)
		elif distinct_numbers > highest_number_of_repeats:
			print(highest_number_of_repeats)
		else:
			print(distinct_numbers - 1)
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(_) for _ in input().split()]
	r = len(set(a))
	p = n // 2
	D = {}
	for s in a:
		D[s] = D.get(s, 0) + 1
	t = max(D.values())
	ans = min(t, r, p)
	if ans == t and ans == r:
		ans = ans - 1
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst = sorted(lst)
	(count, maxx) = (1, 1)
	num = lst[0]
	for i in range(1, n):
		if lst[i] == lst[i - 1]:
			count += 1
		else:
			count = 1
		if maxx < count:
			num = lst[i]
			maxx = count
	a = set()
	count1 = 0
	for i in lst:
		if i != num:
			count1 += 1
			a.add(i)
	if maxx <= len(a):
		print(min(maxx, len(a)))
	elif (maxx - len(a)) // 2 >= 1:
		print(len(a) + 1)
	else:
		print(len(a))


from collections import defaultdict

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = [int(i) for i in input().split()]
		d = defaultdict(int)
		for i in arr:
			d[i] += 1
		s = set(arr)
		di = sorted(d.items(), key=lambda x: x[1], reverse=True)
		ans = 0
		for (j, k) in di:
			ns = s - set([j])
			lns = len(ns)
			if k <= lns:
				ans = max(ans, k)
				break
			else:
				if k >= lns + 2:
					ans = max(ans, lns + 1)
				else:
					ans = max(ans, lns)
				break
		print(ans)
main()


for _ in range(int(input())):
	(l, d) = ([list(map(int, input().split())) for i in '..'][1], dict())
	for s in l:
		d[s] = d.get(s, 0) + 1
	print(max(min(len(d) - 1, max(d.values())), min(len(d), max(d.values()) - 1)))


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = Counter(a)
	print(max((max(min(c[x], len(c) - 1), min(c[x] - 1, len(c))) for x in a)))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()][:n]
	d = Counter(l)
	(tdist, tsame) = (0, 0)
	m = max(d.values())
	tdist = len(set(l))
	if tdist < m:
		print(tdist)
	elif tdist == m:
		print(tdist - 1)
	else:
		print(m)


t = int(input())

def solve(n, nums):
	if n == 1:
		return 0
	mx = 0
	cnts = {}
	st = set()
	for num in nums:
		if num not in cnts:
			cnts[num] = 0
		cnts[num] += 1
		st.add(num)
	cnts = {k: v for (k, v) in sorted(cnts.items(), key=lambda item: -item[1])}
	for k in cnts.keys():
		cur = cnts[k]
		sol1 = min(cur - 1, len(st))
		sol2 = min(cur, len(st) - 1)
		mx = max(mx, max(sol1, sol2))
	return mx
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	print(solve(n, nums))


from collections import Counter as cnt

def count(a, n):
	if n == 1:
		return 0
	d = cnt(a)
	new = list(d.values())
	l = len(new)
	if l == n:
		return 1
	else:
		new.sort()
		if new[-1] > l:
			return min(l, new[-1] - 1)
		else:
			return min(l - 1, new[-1])
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(count(a, n))


from sys import stdin, stdout
for test in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = [int(x) for x in stdin.readline().split()]
	if n == 1:
		stdout.write('0\n')
	elif n == 2 or n == 3:
		stdout.write('1\n')
	else:
		arr.sort()
		dic = {}
		maxi = 1
		for i in arr:
			if i not in dic:
				dic[i] = 1
			else:
				dic[i] += 1
				if dic[i] >= maxi:
					maxi = dic[i]
		uni = len(dic)
		if maxi == uni:
			stdout.write(str(uni - 1) + '\n')
		elif maxi > uni:
			stdout.write(str(uni) + '\n')
		else:
			stdout.write(str(maxi) + '\n')


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = dict(Counter(a))
	ky = list(b.keys())
	l_u = len(ky)
	maxrep = 0
	maxkey = ky[0]
	for i in ky:
		if b[i] > maxrep:
			maxrep = b[i]
			maxkey = i
	if maxrep > l_u:
		print(l_u)
	else:
		print(min(l_u - 1, maxrep))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	k = 0
	for i in l:
		try:
			d[i] += 1
		except:
			d[i] = 1
	if n == 1:
		print(0)
	else:
		c = len(d)
		ans = 1
		for i in d:
			ans = max(ans, min(d[i], c - 1))
			ans = max(ans, min(d[i] - 1, c))
		print(ans)


def main():
	test = int(input())
	for t in range(test):
		n = int(input())
		N = list(map(int, input().split(' ')))
		N.sort()
		mapArray = [[N[0], 1]]
		maxSkillStudentNum = 1
		for i in range(1, n):
			if N[i] == mapArray[len(mapArray) - 1][0]:
				mapArray[len(mapArray) - 1][1] += 1
			else:
				mapArray.append([N[i], 1])
			if maxSkillStudentNum < mapArray[len(mapArray) - 1][1]:
				maxSkillStudentNum = mapArray[len(mapArray) - 1][1]
		if maxSkillStudentNum < len(mapArray):
			print(maxSkillStudentNum)
		elif maxSkillStudentNum == len(mapArray):
			print(maxSkillStudentNum - 1)
		else:
			print(len(mapArray))
main()


from collections import Counter
for i in range(int(input())):
	a = int(input())
	x = [int(x) for x in input().split()]
	diff = len(set(x))
	s = Counter(x)
	(c, same) = s.most_common(1)[0]
	print(max(min(same - 1, diff), min(same, diff - 1)))


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	s = len(set(a))
	u = dict(Counter(a))
	ll = max(u.values())
	print(max(min(s - 1, ll), min(ll - 1, s)))


from collections import Counter

def main():
	T = int(input())
	for _ in range(T):
		N = int(input())
		A = list(map(int, input().split()))
		d = Counter(A)
		highFreq = max(d.keys(), key=lambda x: d[x])
		if len(d) > 1:
			if d[highFreq] > len(d) - 1:
				if d[highFreq] - 1 >= len(d):
					print(len(d))
				else:
					print(len(d) - 1)
			else:
				print(d[highFreq])
		elif N == 1:
			print(0)
		else:
			print(1)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		b = [0 for i in range(n)]
		for i in a:
			b[i - 1] += 1
		no1 = b.count(1)
		nos = max(b)
		if no1 > nos:
			print(nos)
		else:
			b.pop(b.index(nos))
			count = 0
			for i in b:
				if i > 1:
					count += 1
			if no1 + count >= nos:
				print(nos)
			elif nos - no1 - count > 1:
				print(no1 + 1 + count)
			else:
				print(no1 + count)


t = int(input())
for i in range(t):
	n = int(input())
	D = {}
	a = list(map(int, input().split()))
	for j in range(n):
		if a[j] not in D:
			D[a[j]] = 1
		else:
			D[a[j]] += 1
	max_value = 0
	for j in D.values():
		if max_value < j:
			max_value = j
	count = len(D)
	if count == 1:
		if max_value > 1:
			print(1)
		else:
			print(0)
	elif count == 2:
		if max_value > 2:
			print(2)
		else:
			print(1)
	elif max_value > count - 1:
		if max_value - count + 1 >= 2:
			print(count)
		else:
			print(count - 1)
	else:
		print(max_value)


t = int(input())
for _ in range(0, t):
	n = int(input())
	skills = list(map(int, input().split(' ')))
	people_per_skill = {}
	for i in range(0, len(skills)):
		if skills[i] in people_per_skill.keys():
			people_per_skill[skills[i]] += 1
		else:
			people_per_skill[skills[i]] = 1
	people_with_highest_skill = 0
	for i in people_per_skill.keys():
		if people_per_skill[i] > people_with_highest_skill:
			people_with_highest_skill = people_per_skill[i]
	len_team_1 = len(people_per_skill) - 1
	len_team_2 = people_with_highest_skill
	if len_team_1 == len_team_2:
		print(len_team_1)
	elif len_team_1 > len_team_2:
		print(len_team_2)
	else:
		diff = len_team_2 - len_team_1
		if diff <= 2:
			print(len_team_2 - 1)
		else:
			print(len_team_1 + 1)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	A = tuple(map(int, input().split()))
	d = defaultdict(int)
	if n == 1:
		print(0)
		continue
	if n == len(set(A)):
		print(1)
		continue
	for a in A:
		d[a] += 1
	M = max(d.values())
	L = len(d.keys())
	if L > M:
		print(M)
	elif L == M:
		print(min(L, M - 1))
	else:
		print(L)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	d = {}
	dist = m = 0
	if n == 1:
		print(0)
		continue
	for v in a:
		if v in d:
			d[v] += 1
		else:
			dist += 1
			d[v] = 1
		if d[v] > m:
			m = d[v]
			most_freq = v
	dist -= 1
	if dist == m or dist + 1 == m:
		print(dist)
	elif dist < m:
		print(dist + 1)
	else:
		print(min(m, dist))


from collections import Counter

def ans(n, arr):
	hmap = Counter(arr)
	(key, freq) = hmap.most_common()[0]
	if len(arr) <= 2:
		return n // 2
	return max([min([len(hmap.keys()) - 1, freq]), min([len(hmap.keys()), freq - 1])])
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	ans_ = ans(n, arr)
	print(ans_)


from io import BytesIO
import os
from collections import Counter
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main():
	cases = int(input())
	while cases:
		cases -= 1
		students = int(input())
		skills = list(map(int, input().split()))
		counter = Counter(skills)
		most_freq = counter.most_common(1)[0][1]
		if most_freq > len(counter):
			print(min(students // 2, most_freq - 1, len(counter)))
		else:
			print(min(students // 2, most_freq, len(counter) - 1))
main()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	f = n // 2
	d = defaultdict(int)
	a = list(map(int, input().split()))
	b = len(set(a))
	for i in range(len(a)):
		d[a[i]] += 1
	c = max(list(d.values()))
	if b == c:
		print(min(b, c) - 1)
	else:
		print(min(b, c))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dic = {}
	for item in a:
		dic[item] = dic.get(item, 0) + 1
	mx = max(dic.values())
	print(max(min(len(dic), mx - 1), min(len(dic) - 1, mx)))


a = int(input())
for _ in range(a):
	vmax = 0
	v = 1
	z = int(input())
	ary = sorted(list(map(int, input().split())))
	l = z - 1
	for i in range(z - 1):
		if ary[i] == ary[i + 1]:
			l -= 1
			v += 1
		else:
			if vmax < v:
				vmax = v
			v = 1
	if vmax < v:
		vmax = v
	if vmax > l:
		l += 1
		vmax -= 1
	print(min(vmax, l))


def func(arr):
	if len(arr) == 2:
		return 1
	if len(arr) == 1:
		return 0
	dif = {}
	for i in arr:
		if i in dif:
			dif[i] += 1
		else:
			dif[i] = 1
	counter = -1
	for i in dif:
		if dif[i] > counter:
			counter = dif[i]
	result = []
	tmp = len(set(arr))
	if counter == tmp:
		counter -= 1
	return min(tmp, counter) if min(tmp, counter) > 0 else 1
n = int(input())
for m in range(n):
	k = int(input())
	array = list(map(int, input().split()))
	print(func(array))


t = int(input())
import math
delay = []
for _ in range(t):
	n = int(input())
	a = input().split()
	a = [int(i) for i in a]
	if n == 1:
		print('0')
		delay.append('0')
		continue
	dic = {}
	for val in a:
		if val not in dic:
			dic[val] = 1
		else:
			dic[val] += 1
	max_freq = 0
	for k in dic:
		if dic[k] > max_freq:
			max_freq = dic[k]
	num_diff = len(dic) - 1
	if max_freq == 0 and num_diff == 1:
		ans = 0
	elif max_freq == 1:
		ans = 1
	elif max_freq - 1 >= num_diff + 1:
		ans = num_diff + 1
	elif max_freq >= num_diff and max_freq - num_diff <= 1:
		ans = num_diff
	else:
		ans = max_freq
	print(ans)


gg = 'abcdefghijklmnopqrstuvwxyz'
t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	max_c = 0
	disti = 0
	if n <= 1:
		print(0)
	elif n == 2 or n == 3:
		print(1)
	else:
		a = [0 for i in range(0, n + 1)]
		for i in l:
			a[i] += 1
		for i in a:
			if i > 0:
				disti += 1
		max_c = max(a[:])
		if max_c >= disti:
			if max_c == disti:
				print(disti - 1)
			else:
				print(disti)
		elif max_c < disti:
			print(max_c)
		else:
			print(1)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
import bisect as bi

def I():
	return int(input())

def In():
	return map(int, input().split())

def find_gt(a, x):
	i = bi.bisect_right(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		for _ in range(I()):
			n = I()
			l1 = list(In())
			d = {}
			for x in l1:
				if d.get(x, 0) != 0:
					d[x] += 1
				else:
					d[x] = 1
			(m1, m2) = (len(d), max(d.values()))
			m1 -= 1
			if m1 > m2:
				print(m2)
			elif m2 > m1:
				if m2 >= m1 + 2:
					m1 += 1
				print(m1)
			else:
				print(m1)
	except:
		pass
M = 998244353
P = 1000000007
main()


import math
t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	ls = list(map(int, input().strip().split()))
	ls.sort()
	n_ls = []
	for v in ls:
		if len(n_ls) and n_ls[-1][1] == v:
			n_ls[-1][0] += 1
		else:
			n_ls.append([1, v])
	n_ls.sort(reverse=True)
	maz = 0
	for v in n_ls:
		val = max(min(len(n_ls), v[0] - 1), min(len(n_ls) - 1, v[0]))
		if val > maz:
			maz = val
	print(maz)


inp = lambda cast=int: [cast(x) for x in input().split()]
printf = lambda s='', *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs)
(t,) = inp()
for _ in range(t):
	(n,) = inp()
	A = inp()
	(M, maxi) = ({}, 0)
	for a in A:
		M[a] = 1 + (M[a] if a in M else 0)
		maxi = max(maxi, M[a])
	print(max(min(maxi, len(M) - 1), min(maxi - 1, len(M))))


t = int(input())
while t != 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	s = set()
	for i in a:
		s.add(i)
	a.sort()
	max1 = 1
	cnt = 1
	for i in range(n - 1):
		if a[i] == a[i + 1]:
			cnt = cnt + 1
		else:
			max1 = max(max1, cnt)
			cnt = 1
	max1 = max(max1, cnt)
	t = t - 1
	print(max(min(max1, len(s) - 1), min(max1 - 1, len(s))))


def single(u, k):
	if u == 0:
		return 1
	elif abs(k[0] - u) < 2:
		return min(u, k[0])
	elif u > k[0] + 1:
		return k[0]
	else:
		return u + 1
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	else:
		(u, v, k) = (0, 0, [])
		d = Counter(l)
		for x in d:
			if d[x] > 1:
				v += 1
				k.append(d[x])
			else:
				u += 1
		if v == 0:
			print(1)
		elif v == 1:
			print(single(u, k))
		else:
			m = max(k)
			print(single(u + v - 1, [m]))


import math
alph = 'abcdefghijklmnopqrstuvwxyz'
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	different = 0
	last = -1
	now = 0
	count = 0
	for j in range(n):
		count = max(count, now)
		if a[j] != last:
			different += 1
			now = 1
			last = a[j]
		else:
			now += 1
		count = max(count, now)
	if different == count:
		print(count - 1)
	elif different > count:
		print(count)
	else:
		print(different)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	skills = Counter(A)
	(prefer, times) = skills.most_common()[0]
	if times == len(skills):
		print(times - 1)
	elif times > len(skills):
		print(len(skills))
	else:
		print(times)


from math import ceil

def solve():
	t = int(input())
	for _ in range(t):
		n = int(input())
		unique = set()
		total = {}
		for i in map(int, input().split()):
			unique.add(i)
			total[i] = total.get(i, 0) + 1
		mxx = max(tuple(total.values()))
		maximum = max(mxx - (len(unique) <= mxx), 0)
		print(min(len(unique), maximum))
solve()


from math import inf, sqrt, floor, ceil
from collections import Counter, defaultdict, deque
from heapq import heappush as hpush, heappop as hpop, heapify as h
from operator import itemgetter
from itertools import product
from bisect import bisect_left, bisect_right
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = Counter(a)
	(s, d) = (0, 0)
	for v in c:
		d += 1
		c[v] -= 1
	s = c.most_common(1)[0][1]
	if d <= s or abs(d - s) == 1:
		print(min(s, d))
	else:
		print(min(s + 1, d - 1))


from math import factorial
from math import log, ceil
from collections import defaultdict
from math import *
from sys import stdin, stdout
import itertools
import os
import sys
import threading
from collections import deque, Counter, OrderedDict, defaultdict
from heapq import *
from fractions import Fraction
mod = 998244353

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d:
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d:
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = defaultdict(list)
	for i in range(n):
		(x, y) = mi()
		d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			s.add(i)
			n = n // i
	if n > 2:
		s.add(n)
	return s

def find_all(a_str, sub):
	start = 0
	while True:
		start = a_str.find(sub, start)
		if start == -1:
			return
		yield start
		start += len(sub)

def SieveOfEratosthenes(n, isPrime):
	isPrime[0] = isPrime[1] = False
	for i in range(2, n):
		isPrime[i] = True
	p = 2
	while p * p <= n:
		if isPrime[p] == True:
			i = p * p
			while i <= n:
				isPrime[i] = False
				i += p
		p += 1
	return isPrime

def dijkstra(edges, f, t):
	g = defaultdict(list)
	for (l, r, c) in edges:
		g[l].append((c, r))
	(q, seen, mins) = ([(0, f, ())], set(), {f: 0})
	while q:
		(cost, v1, path) = heappop(q)
		if v1 not in seen:
			seen.add(v1)
			path = (v1, path)
			if v1 == t:
				return (cost, path)
			for (c, v2) in g.get(v1, ()):
				if v2 in seen:
					continue
				prev = mins.get(v2, None)
				next = cost + c
				if prev is None or next < prev:
					mins[v2] = next
					heappush(q, (next, v2, path))
	return float('inf')

def binsearch(a, l, r, x):
	while l <= r:
		mid = l + (r - 1) // 2
		if a[mid]:
			return mid
		elif a[mid] > x:
			l = mid - 1
		else:
			r = mid + 1
	return -1

def find_all_paths(graph, start, end, path=[]):
	path = path + [start]
	if start == end:
		return path
	paths = []
	for node in graph[start]:
		if node not in path:
			newpaths = find_all_paths(graph, node, end, path)
			for newpath in newpaths:
				paths.append(newpath)
	return paths
for _ in range(ii()):
	n = ii()
	l = sorted(li())
	x = [0] * (max(l) + 1)
	for i in range(n):
		x[l[i]] += 1
	z = max(x)
	f = 0
	count = 0
	for i in x:
		if i == z and f == 0:
			f = 1
		elif i > 0:
			count += 1
	if count >= z:
		print(z)
	else:
		print(min(count + 1, (count + z) // 2))


for _ in range(int(input())):
	n = int(input())
	c = {}
	for x in map(int, input().split()):
		c[x] = c.get(x, 0) + 1
	a = 0
	for x in c:
		a = max(a, min(len(c) - 1, c[x]), min(len(c), c[x] - 1))
	print(a)


t = int(input())
for q in range(t):
	n = int(input())
	entrada = list(map(int, input().split(' ')))
	entrada.sort()
	tSet = len(set(entrada))
	tEnt = len(entrada)
	if tEnt == 1:
		print(0)
	elif tSet == tEnt:
		print(1)
	else:
		cont = 0
		maior = 0
		elemento = -1
		for k in range(n - 1):
			if entrada[k] == entrada[k + 1]:
				elemento = entrada[k]
				cont += 1
			else:
				if cont > maior:
					maior = cont
				cont = 0
		if cont > maior:
			maior = cont
		maior += 1
		if tSet > maior:
			print(maior)
		elif elemento in set(entrada) and tSet == maior:
			print(tSet - 1)
		else:
			print(tSet)


from math import *
from copy import *
from heapq import *
from string import *
from random import *
from bisect import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
from collections import defaultdict

def solve(l):
	n = len(l)
	if n == 1:
		return 0
	d = dict(Counter(l))
	maxx = 0
	d = sorted(d.items(), key=itemgetter(1), reverse=True)
	nn = len(d)
	for i in d:
		(key, val) = i
		maxx = max(maxx, min(nn - 1, val))
		if val - nn + 1 > 1:
			maxx = max(maxx, nn)
	return maxx
T = int(input())
for _ in range(T):
	N = int(stdin.readline())
	l = list(map(int, stdin.readline().rstrip().split()))
	print(solve(l))


for _ in range(int(input())):
	n = int(input())
	ll = list(map(int, input().split()))
	d = {}
	for i in ll:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	m = max(d.values())
	if m > len(d):
		print(len(d))
	elif m == len(d):
		print(len(d) - 1)
	else:
		print(m)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	d = defaultdict(int)
	for x in input().split():
		d[x] += 1
	v = d.values()
	m = max(v)
	l = len(v)
	ans = 0
	if m <= l - 1:
		ans = m
	elif m == l:
		ans = l - 1
	else:
		ans = l
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(1)
	else:
		x = Counter(l)
		p = len(x) - 1
		y = list(x.values())
		m = 0
		for i in y:
			if p >= i:
				m = max(m, i)
			elif i - p >= 2:
				m = max(m, p + 1)
			else:
				m = max(m, p)
		print(m)


t = int(input())
for q in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	number = 1
	for i in range(0, n - 1):
		if a[i] != a[i + 1]:
			number += 1
	mn = 0
	now = 1
	for i in range(0, n - 1):
		if a[i] == a[i + 1]:
			now += 1
		else:
			mn = max(mn, now)
			now = 1
	mn = max(mn, now)
	print(max(min(number - 1, mn), min(number, mn - 1)))


from statistics import mode
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = mode(a)
	maxsize = a.count(m)
	nodupes = len(list(set(a)))
	if nodupes == maxsize:
		print(maxsize - 1)
	else:
		print(min([nodupes, maxsize]))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = [0] * (n + 1)
	s = set()
	m = 0
	k = 0
	for i in a:
		l[i] += 1
		if l[i] >= m:
			m = l[i]
		if i not in s:
			s.add(i)
			k += 1
	if k > m:
		print(m)
	elif k == m:
		print(m - 1)
	else:
		print(k)


from math import *
from bisect import bisect_left, bisect_right
from queue import Queue
from sys import stdin, stdout
from collections import Counter
input = lambda : stdin.readline().strip()
fast_print = stdout.write
t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	a = len(set(ls)) - 1
	b = max(dict(Counter(ls)).values())
	if a >= b:
		fast_print(str(b) + '\n')
	elif a + 1 == b:
		fast_print(str(a) + '\n')
	else:
		fast_print(str(a + 1) + '\n')


t = int(input())
for x in range(t):
	n = int(input())
	ll = list(map(int, input().split(' ')))
	dic = {}
	for xx in range(n):
		if ll[xx] not in dic.keys():
			dic[ll[xx]] = 1
		else:
			dic[ll[xx]] = dic[ll[xx]] + 1
	xxs = len(dic)
	ans = []
	for hi in dic.values():
		ans.append(min(hi, xxs - 1))
		ans.append(min(hi - 1, xxs))
	print(max(ans))


queries = int(input())
for _ in range(queries):
	_ = input()
	array = [int(item) for item in input().split()]
	array.sort()
	occurences = 0
	max_occurences = 0
	distinct = 0
	for value in range(len(array)):
		if value == 0:
			occurences = 1
			distinct = 1
		elif array[value] != array[value - 1]:
			max_occurences = max(max_occurences, occurences)
			occurences = 1
			distinct += 1
		else:
			occurences += 1
	max_occurences = max(max_occurences, occurences)
	print(max(min(max_occurences - 1, distinct), min(max_occurences, distinct - 1)))


from collections import Counter
T = int(input())
out = []
for i in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	dict_1 = dict(Counter(l))
	max1 = max(dict_1.values())
	temp_count = len(dict_1)
	if max1 >= temp_count + 1:
		out.append(temp_count)
	elif max1 == temp_count:
		out.append(temp_count - 1)
	else:
		out.append(max1)
for i in range(T):
	print(out[i])


def li():
	return list(map(int, input().split()))
from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = li()
	unique = list(set(a))
	b = []
	l = len(unique)
	d = Counter(a)
	for x in unique:
		if d[x] > l:
			b.append(l)
		elif d[x] == l:
			b.append(l - 1)
		else:
			b.append(d[x])
	print(max(b))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = [0] * n
	for i in range(n):
		lis[arr[i] - 1] += 1
	a = 0
	b = max(lis)
	for i in range(n):
		if lis[i] > 0:
			a += 1
	c = min(a, b)
	if a == b:
		print(c - 1)
	else:
		print(c)


from collections import Counter
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	d = Counter(a)
	a = max(d.values())
	b = len(d)
	if a == b:
		print(a - 1)
	elif a > b:
		print(b)
	else:
		print(a)
	t -= 1