eolymp
bolt
Try our new interface for solving problems
Problems

Amazing Trick

Amazing Trick

Alice is a magician and she creates a new trick. She has $n$ cards with different numbers from $1$ to $n$ written on them. First, she asks an audience member to shuffle the deck and put cards in a row. Let’s say the $i$-th card from the left has the number $a_i$ on it. Then Alice picks two permutations $p$ and $q$. There is a restriction on $p$ and $q$ --- permutations can’t have fixed points. Which means $∀ i: p_i \ne i$ and $q_i \ne i$. After permutations are chosen, Alice shuffles the cards according to them. Now the $i$-th card from the left is the card $a[p[q[i]]$. The trick is considered successful if $i$-th card from the left has the number $i$ on it after the shuffles. Help Alice pick the permutations $p$ and $q$ or say it is not possible for the specific starting permutation $a$. \InputFile The first line contains the number of tests $t~(1 \le t \le 10^5)$. Each test is described in two lines. The first line contains one integer $n~(1 \le n \le 10^5)$ --- the number of cards. The second line contains $n$ integers $a_i~(1 \le a_i \le n, ∀ i \ne j: a_i \ne a_j)$ --- the initial permutation of the cards. It is guaranteed that the sum of $n$ over all tests does not exceed $10^5$. \OutputFile Print the answer for each test case in the same order the cases appear in the input. For each test case, print “\textbf{Impossible}” in a single line, if no solution exists. Otherwise, print "\textbf{Possible}" in the first line, and in the following two lines print permutations $p$ and $q$.
Time limit 2 seconds
Memory limit 128 MiB
Input example #1
4
2
2 1
3
1 2 3
4
2 1 4 3
5
5 1 4 2 3
Output example #1
Impossible
Possible
3 1 2
2 3 1
Possible
3 4 2 1
3 4 2 1
Possible
4 1 2 5 3
3 1 4 5 2
Source 2022 ICPC, NEERC, Semifinals, December 7, Problem A