Samsung Electronics has a mascot named Holly.
One day, she decided to create a block game by herself to ease boredom.
The game is about removing blocks on a screen in a size of N*M (N for height and M for width).
The rules are following.
The N*M screen is piled up with 1*1 blocks. (The number of blocks on the screen could be 0.)
The blocks are in five different colors of red, yellow, green, blue and purple.
They are always piled up in the same column from bottom to top without empty space.
In other words, picture 1 and 2 are possible while picture 3 is not. (Blocks in the same column should not have any empty space.)
The game has steps to push one of the U, L, R and D buttons.
- A block array of one row will come up when pressing the U button.
The existing blocks will go up by one line.
The new block array in the row may have empty space(s).
If so, a block will fall down to fill the space for the condition of “The blocks are always piled up in the same column from bottom to top without any space.”
Picture 6 will ultimately become picture 8 when a new row comes up and a block falls down to fill a space after pressing the U button.
A new block array will not come up if there is at least one block left in all rows, and the game will proceed.
Let’s look at the below example.
Picture 9 has more than one block in every row, thus there will be no new block array even though the U button is pushed.
- The L button will move all blocks to the left as much as possible from their rows.
- The R button will move all blocks to the right as much as possible from their rows.
- The D button will remove a set of the biggest blocks.
A block set is made of adjacent blocks of the same color.
Blocks are called adjacent when they share a side.
Size of the block set is equal to the number of included blocks.
If there are multiple biggest block sets, they will be removed at the same time.
Let’s look at picture 15 and 16.
There is a set of the biggest blocks in size 8.
Also, there are 3 block sets that are in size 8.
If the D button is pressed in picture 15, block sets with slashes in picture 16 will be deleted simultaneously.
After this, blocks that have spaces below will fall down to meet the condition above.
Let’s look at the example.
The D button in picture 17 will delete three block sets in size 8.
Following this, blocks will fall down to fill empty spaces.
Simply put, picture 19 shows a final state when pressing the D button in picture 17.
Pressing L, R, D buttons will be skipped if there is no block on the N*M screen and the game will move onto the next step.
Write a program that prints a final screen when the initial N*M screen and the number of steps that Holly follows are given.
[Input]
The first line contains the number of test cases known as T(1≤T≤30).
The first line of each test has integers of N,M(1≤N30,1≤M30) and the number of steps Q(1≤Q≤30) separated by a space.
N lines have the initial N*M screen that is piled up by the random number of blocks.
A cell without a block is represented as ‘#’.
Cells piled up by red, yellow, green, blue and purple blocks are expressed as ‘R’, ‘Y’, ‘G’, ‘B’ and ‘P’.
Q lines have Q steps in order to push one of the U, L, R and D buttons.
A new block row separated by a space will be given when the U button is pushed.
The row has M cells as well as blocks and empty cells (‘#’).
[Output]
For each test, print a final screen of N*M after the Q step throughout N lines.
The first case proceeds as follows.
The second case proceeds as follows.