The well-known toy, the "Rubik's Cube," consists of smaller cubes, as shown in Figure , where letters indicate colors (for example, B for blue, R for red, and so on). The goal of the game is to rotate the faces so that, in the end, each face has the same, but its own, unique color, as illustrated. Note that when a face is rotated, the color configuration changes on all neighboring faces. Figure demonstrates the rotation of one of the faces.

Your grandfather, who has many years of experience, claims that for any Rubik's Cube configuration, he can devise a sequence of rotations to bring it to the solved state. In order to show all faces of the cube, we will display it as in Figure . 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 given the initial configuration of the cube and a sequence of rotations. Each rotation is given by an integer indicating the face that is rotated and the direction of rotation (a positive number means rotation clockwise, a negative number means rotation counterclockwise). The faces of the cube are numbered as in Figure . Write a program that checks whether the given sequence of rotations leads the cube to the solved state.
The input contains multiple test cases. The first line is an integer indicating the number of tests. Each test consists of ten lines. The first nine lines of each test describe the initial configuration in the format shown in Figure . The next line contains a sequence of rotations ending with the number 0.
Your program should output a single line consisting of a sequence of 0s and 1s without spaces. Output 1 if the cube is solved; otherwise, output 0.
Example 1