CF2001A.Make All Equal

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

Make All Equal

You are given a cyclic array a1,a2,,ana_1, a_2, \ldots, a_n.

You can perform the following operation on aa at most n1n - 1 times:

  • Let mm be the current size of aa, you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, ama_m and a1a_1 are adjacent and ama_m is the previous one), and delete exactly one of them. In other words, choose an integer ii (1im1 \leq i \leq m) where aia(imodm)+1a_i \leq a_{(i \bmod m) + 1} holds, and delete exactly one of aia_i or a(imodm)+1a_{(i \bmod m) + 1} from aa.

Your goal is to find the minimum number of operations needed to make all elements in aa equal.

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 \le n \le 100) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \le a_i \le n) — the elements of array aa.

Output

For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in aa equal.

Note

In the first test case, there is only one element in aa, so we can't do any operation.

In the second test case, we can perform the following operations to make all elements in aa equal:

  • choose i=2i = 2, delete a3a_3, then aa would become [1,2][1, 2].
  • choose i=1i = 1, delete a1a_1, then aa would become [2][2].

It can be proven that we can't make all elements in aa equal using fewer than 22 operations, so the answer is 22.

Samples

7
1
1
3
1 2 3
3
1 2 2
5
5 4 3 2 1
6
1 1 2 2 3 3
8
8 7 6 3 8 7 6 3
6
1 1 4 5 1 4
0
2
1
4
4
6
3

在线编程 IDE

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