Did the grandfather gather it or not?
The well-known toy, the "Rubik's Cube," is made up of smaller cubes, as depicted in Figure 1a, where letters represent colors (e.g., B for blue, R for red, etc.). The objective of the game is to rotate the cube's faces until each face is a single, uniform color, as shown in the figure. Keep in mind that rotating one face alters the color arrangement of all adjacent faces. Figure 2 demonstrates the rotation of one of the faces.
Your grandfather, with years of experience, claims he can find a sequence of rotations to solve any Rubik's Cube configuration. To display all the cube's faces, we will represent it as shown in Figure 3a. The six colors are always yellow (Y), red (R), blue (B), green (G), white (W), and purple (M), denoted by their first letters in English.
You are provided with the initial configuration of the cube and a sequence of rotations. Each rotation is represented by an integer indicating the face to be rotated and the direction of rotation (a positive number indicates a clockwise rotation, while a negative number indicates a counterclockwise rotation). The cube's faces are numbered as shown in Figure 3b. Your task is to write a program that determines if the given sequence of rotations results in the cube being solved.
Input
The input consists of several test cases. The first line contains an integer indicating the number of tests. Each test includes ten lines. The first nine lines of each test describe the initial configuration, formatted as shown in illustration 3a. The tenth line contains a sequence of rotations, ending with the number 0.
Output
Your program should output a single line of 0s and 1s without spaces. Output 1 if the cube is solved; otherwise, output 0.