#P1458C. Latin Square
Latin Square
No submission language available for this problem.
Description
You are given a square matrix of size . Every row and every column of this matrix is a permutation of , , , . Let be the element at the intersection of -th row and -th column for every . Rows are numbered top to bottom, and columns are numbered left to right.
There are six types of operations:
- R: cyclically shift all columns to the right, formally, set the value of each to ;
- L: cyclically shift all columns to the left, formally, set the value of each to ;
- D: cyclically shift all rows down, formally, set the value of each to ;
- U: cyclically shift all rows up, formally, set the value of each to ;
- I: replace the permutation read left to right in each row with its inverse.
- C: replace the permutation read top to bottom in each column with its inverse.
One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of .
Given the initial matrix description, you should process operations and output the final matrix.
The first line contains a single integer () — number of test cases. test case descriptions follow.
The first line of each test case description contains two integers and () — size of the matrix and number of operations.
Each of the next lines contains integers separated by single spaces — description of the matrix ().
The last line of the description contains a string of characters describing the operations in order, according to the format above.
The sum of does not exceed , and the sum of does not exceed .
For each test case, print lines with integers each — the final matrix after operations.
Input
The first line contains a single integer () — number of test cases. test case descriptions follow.
The first line of each test case description contains two integers and () — size of the matrix and number of operations.
Each of the next lines contains integers separated by single spaces — description of the matrix ().
The last line of the description contains a string of characters describing the operations in order, according to the format above.
The sum of does not exceed , and the sum of does not exceed .
Output
For each test case, print lines with integers each — the final matrix after operations.
Samples
Note
Line breaks between sample test case answers are only for clarity, and don't have to be printed.