CF2146A.Equal Occurrences

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Equal Occurrences

We call an array balanced if and only if the numbers of occurrences of any of its elements are the same. For example, [1,1,3,3,6,6][1,1,3,3,6,6] and [2,2,2,2][2,2,2,2] are balanced, but [1,2,3,3][1,2,3,3] is not balanced (the numbers of occurrences of elements 11 and 33 are different). Note that an empty array is always balanced.

You are given a non-decreasing array aa consisting of nn integers. Find the length of its longest balanced subsequence^{\text{∗}}.

^{\text{∗}}A sequence bb is a subsequence of a sequence aa if bb can be obtained from aa by the deletion of several (possibly, zero or all) element from arbitrary positions.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n1001 \leq n \leq 100) — the length of aa.

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1a1a2ann1\le a_1\le a_2\le \cdots \le a_n\le n) — the elements of aa.

Output

For each test case, output a single integer — the length of the longest balanced subsequence of aa.

Note

In the first test case, the whole array a=[1,1,4,4,4]a = [1, 1, 4, 4, 4] is not balanced because the number of occurrences of element 11 is 22, while the number of occurrences of element 44 is 33, which are not equal. The subsequence [1,1,4,4][1, 1, 4, 4] is balanced because the numbers of occurrences of elements 11 and 44 are both 22. Thus, the length of the longest balanced subsequence of aa is 44.

In the second test case, the whole array a=[1,2]a = [1, 2] is already balanced, so the length of the longest balanced subsequence of aa is 22.

In the third test case, the longest balanced subsequence of aa is [1,1,1,2,2,2,3,3,3][1,1,1,2,2,2,3,3,3].

In the fourth test case, the whole array a=[3,3,3,3,3]a = [3, 3, 3, 3, 3] is already balanced, so the length of the longest balanced subsequence of aa is 55.

Samples

4
5
1 1 4 4 4
2
1 2
15
1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
5
3 3 3 3 3
4
2
9
5

在线编程 IDE

建议全屏模式获得最佳体验