Rubik’s cube

Time Limit: 2000 ms Memory Limit: 65536 KiB

Problem Description

    Flabby is addicted to Rubik’s cube. He has many kinds of Rubik’s cube and plays them well. He can reorder a Rubik’s cube in a few seconds. He is famous for playing Rubik’s cube and attracts many young girls. One of his friends, Ant, is jealous of him and decides to let him be embarrassed in public. Ant makes a small Rubik’s cube by himself. This small magic is 2*2*2 and is drawn in blue and red, as shown in Figure 1. 
                                                             
    This Rubik’s cube can be rotated as well as other kinds of magic cube. Each of six faces can rotated clockwise and counterclockwise. Each 90 degree rotation on any face is counted as one step. The Rubik’s cube which has been reordered has only one color on each face.
     Ant is a clever boy. Sometimes, he can make a Rubik’s cube which can be reordered in a few steps. He can also make a Rubik’s cube which can’t be reordered in any way.
                                                            
                                                                 
Flabby knows what Ant thinks in his mind. He knows that you are a good programmer and asks you for help. Tell him whether this special Rubik’s cube can be reordered in a few steps.

Input

    In the input file, the first line is an integer T which is the number of test case. For each test case, there is 6 lines of integers describe the Rubik’s cube. For each line, there are four integers. Pij which is shown in Figure 3 corresponds to the jth integer of the ith line. If Pij is 0, it means the corresponding place is red. If Pij is 1, it means the corresponding place is blue. 

Output

    If the magic cube can be reordered in n steps, output the minimum n in a line. If the magic cube cannot be reordered, output “IMPOSSIBLE!” in a line. 

Sample Input

3
0 0 0 0
0 1 0 1
1 1 0 0
1 0 1 0
1 1 0 0
1 1 1 1
0 0 1 1
1 1 0 0
0 1 0 1
1 0 0 0
1 1 1 1
0 1 0 0
1 0 1 1
0 1 0 0
0 0 0 0
1 1 1 1
1 0 0 0
0 1 1 1

Sample Output

1
IMPOSSIBLE!
8

Hint

 

Source

2013年山东省第四届ACM大学生程序设计竞赛