Our beloved Robi Kaka has invented a cube. You have to solve only one side of this cube – a square of cells. Every cell has colors – Red, Blue, Green, Yellow stripes drawn in clockwise direction one after another. There is also a white rotating button in the middle of each cell. Tapping it once rotates the cell in clockwise direction.
We define the current state of a cell with its top color. Here are the four possible states of a cell, named after their top color
You are given a jumbled square. Robi kaka calls it solved only if every adjacent cell of it shares same color.
Being the most talented bhatija, Robi kaka will ask you queries. Every time Robi kaka will select cell and press the white button exactly once. Then Robi kaka asks you to find the minimum number of rotations (using the white button) needed to solve the square. Every rotation made by Robi kaka takes place.
Note that, to answer each query, you just take a look at the cube and answer the minimum number of rotation required. But you do not perform any operation on the cube.
The first line contains an integer , number of test cases.
For each test case the first line contains an integer , length of the side of the square.
The following lines each contain a string of characters, denoting the initial square. The character in the -th row and -th column is one of '', '', '', '' denoting the top color of the cell in the -th row from top and -th column from left of the square.
The next line will contain an integer , the number of queries Robi Kaka will ask you.
The following lines each contain numbers, and , meaning that Robi Kaka rotates the cell in -th row from top and -th column from left using the button in it.
For each query, print the minimum number of rotations needed to solve the square.
1 3 BGR GGG RGR 2 3 1 2 2
Login to submit
There are only 4 possible Solved structure/formation. Now, let's calculate and store the number of ...