CF1969A.Two Friends

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

Two Friends

Monocarp wants to throw a party. He has nn friends, and he wants to have at least 22 of them at his party.

The ii-th friend's best friend is pip_i. All pip_i are distinct, and for every i[1,n]i \in [1, n], piip_i \ne i.

Monocarp can send invitations to friends. The ii-th friend comes to the party if both the ii-th friend and the pip_i-th friend receive an invitation (note that the pip_i-th friend doesn't have to actually come to the party). Each invitation is sent to exactly one of the friends.

For example, if p=[3,1,2,5,4]p = [3, 1, 2, 5, 4], and Monocarp sends invitations to the friends [1,2,4,5][1, 2, 4, 5], then the friends [2,4,5][2, 4, 5] will come to the party. The friend 11 won't come since his best friend didn't receive an invitation; the friend 33 won't come since he didn't receive an invitation.

Calculate the minimum number of invitations Monocarp has to send so that at least 22 friends come to the party.

Input

The first line contains one integer tt (1t50001 \le t \le 5000) — the number of test cases.

Each test case consists of two lines:

  • the first line contains one integer nn (2n502 \le n \le 50) — the number of friends;
  • the second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n (1pin1 \le p_i \le n; piip_i \ne i; all pip_i are distinct).

Output

Print one integer — the minimum number of invitations Monocarp has to send.

Note

In the first testcase, Monocarp can send invitations to friends 44 and 55. Both of them will come to the party since they are each other's best friends, and both of them have invitations.

In the second testcase, Monocarp can send invitations to friends 1,21, 2 and 33, for example. Then friends 11 and 22 will attend: friend 11 and his best friend 22 have invitations, friend 22 and his best friend 33 have invitations. Friend 33 won't attend since his friend 44 doesn't have an invitation. It's impossible to send invitations to fewer than 33 friends in such a way that at least 22 come.

In the third testcase, Monocarp can send invitations to both friends 11 and 22, and both of them will attend.

Samples

3
5
3 1 2 5 4
4
2 3 4 1
2
2 1
2
3
2

在线编程 IDE

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