#Lutece3308. Exchange Gifts
Exchange Gifts
Description
Christmas is coming. Now kids in the Xifeng Huhu Welfare Home are going to exchange gifts.
Before that, every kid has prepared a gift and so there are gifts in total. For convenience, we number the kids from to , and the gift prepared by the -th kid is also numbered ().
Initially, every kid will have a gift randomly selected from all of the gifts, and then they sit in a circle around the Christmas tree in the order of number to clockwise. (It means, kids with number and are adjacent.) Then they will start to exchange gifts. Each time for exchange, every kid will give the gift in his hands to the kid sitting on his left and take the gift given by the kid sitting on his right. To be specific, assuming that after times of exchange, the number of the gift in the -th kid's hands is , then after the -th exchange, . Specially, .
For example, if there are three kids, and the number of the gifts in their hands initially is respectively, then after the first time of exchange, the number of the gifts in their hands will become respectively.
However, kids don't want to get gifts prepared by themselves, so they wonder how many times they should exchange their gifts at least (possibly ), so that every kid will have a gift not prepared by himself. That is, for every from to , holds. Here, represents the number of the gift in the -th kid's hands.
Input
The input consists of multiple test cases. The first line contains a single integer , indicating the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer , indicating the number of the kids.
The second line of each test case contains integers , indicating the number of the gift in the -th kid's hands initially. It's guaranteed that every number from to appears exactly once.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print a line containing a single integer , denoting that at least times of exchange should be performed in order to achieve the requirement.
If such doesn't exist, print .
Samples
2
3
2 1 3
5
1 5 4 2 3
-1
2
3
5
2 3 5 1 4
7
4 2 6 3 1 5 7
10
7 3 1 6 2 4 10 8 9 5
0
1
4
Resources
电子科技大学第十三届 ACM 趣味程序设计竞赛