In a deep forest, a war is going to start. Like other animals, the jaguars are also preparing for this ultimate battle. They are mighty strong and lightening fast, they even have an extra advantage over other animals. It's their wise and brave king - 'The Jaguar King'.
The king knows that only speed and strength is not enough for winning the war. They have to make a perfect formation. The king has set up a nice plan and placed all the jaguars according to that formation. There are N jaguars (including the king). The king is marked by 1 and the other jaguars are marked by a number from 2 to N. There are N positions numbered from 1 to N in the formation, and initially the jaguars are positioned by their number i.e. the ith jaguar is in ith position.
But the king realizes that to make the formation perfect and effective, some positions should deploy stronger jaguars and some should deploy faster ones. Since the strength and speed of all the jaguars are not same, the king decided to change the positions of some jaguars. The wise king knows the ability of each and every jaguar, so his decision is perfect but the problem is how to change the position of the jaguars.
One of the wise jaguars has given an idea. The idea is simple. All the jaguars will wait for the king's signal, all eyes will be set on the king. Suppose the king is in the ith position. The king jumps to jth position and when the jaguar at jth position sees the king coming, he immediately jumps to ith position. The king repeats this procedure until they are formatted like the new formation. No collision will occur in the jumping process as all of them are well trained for this.
If the king is in the ith position:
- The king always jumps to a valid position i.e. position between 1 and N
- if i modulo 4 is 1, the king can jump to position (i+1), (i+3), (i+4), (i-4).
- if i modulo 4 is 2, the king can jump to position (i+1), (i-1), (i+4), (i-4).
- if i modulo 4 is 3, the king can jump to position (i+1), (i-1), (i+4), (i-4).
- if i modulo 4 is 0, the king can jump to position (i-3), (i-1), (i+4), (i-4).
Now you are given the final formation of the jaguars, your target is to find the minimum number of times the king had to jump to gain the new formation.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case starts with a line containing an integer N (4 ≤ N ≤ 20, N is a multiple of 4). The next line contains N integers, which is a permutation of 1 to N, denoting the final formation of the jaguars. The ith integer denotes the jaguar which should be placed to ith position.
Output
For each case, print the case number first. Then if it's impossible to do so, print impossible
. If it's possible to do so, but not within 25 jumps, print impossible in 25 jumps
. Otherwise, print the minimum number of times the king has to jump to gain the new formation.
Sample
Sample Input | Sample Output |
---|---|
8 4 1 2 3 4 4 4 2 3 1 8 5 2 3 4 8 6 7 1 8 5 2 8 3 6 7 1 4 4 4 1 3 2 8 8 1 4 3 6 7 5 2 12 12 2 4 1 10 9 7 8 5 6 3 11 12 1 3 12 10 4 6 7 8 5 9 2 11 | Case 1: 0 Case 2: 1 Case 3: 2 Case 4: 7 Case 5: impossible Case 6: 15 Case 7: 25 Case 8: impossible in 25 jumps |