Students were playing with their cubes instead of listening in class, so a professor decided that a program which solves the cube for them would make them lose interest. The professor never said what size the cube is, so this problem only deals with the 2×2×2 cube.
A 2×2×2 cube is made of eight corner pieces. Each face is split into 2×2 squares, and a solved cube shows a single colour on each of its six faces. The cube has no fixed centre pieces, so it does not matter which colour ends up on which face. The cube is solved as soon as every face shows a single colour.
One move turns one face of the cube 90 degrees, clockwise or counterclockwise. A 180 degree turn counts as two moves.
Given the state of a scrambled cube, find the smallest number of moves that solves it.
The first line contains the number of test cases T (1≤T≤100).
Each test case is six lines that draw the cube unfolded, and a blank line separates consecutive test cases. The layout is:
UU
UU
LL FF RR BB
LL FF RR BB
DD
DD
The first two lines are the up face. The middle two lines are the left, front, right and back faces, written in that order from the left and separated by single spaces. The last two lines are the down face.
Every face is written as you see it from outside the cube. The left, front, right and back faces go once around the sides of the cube, and the first line of all four touches the up face. The up face is unfolded above the front face, so its first line touches the back face and its second line touches the front face. The down face is unfolded below the front face, so its first line touches the front face and its second line touches the back face. The left column of both the up face and the down face touches the left face.
Colours are written with the six letters G, R, O, B, Y and W. Every cube in the input is a scramble of a solved cube with six different colours, so each colour appears exactly four times and the cube can always be solved with the moves described above.
For each test case, print the smallest number of moves that solves the cube, one per line.