import java.io.*;
import java.util.*;
int T = nextInt();
for(int t = 0; t < T; ++t) {
int N = nextInt();
int[][] matrix = new int[N][N];
for(int i = 0; i < N; ++i) {
line = reader.readLine();
for(int j = 0; j < line.length(); ++j) {
matrix[i][j] = line.charAt(j)-'0';
}
}
int M = nextInt();
for(int m = 0; m < M; ++m) {
action = reader.readLine();
if(action.contains("row")) {
int rowA
= new Integer(action.
split(" ")[1]).
intValue(); int rowB
= new Integer(action.
split(" ")[2]).
intValue();
for(int col = 0; col < matrix.length; ++col) {
int temp = matrix[rowA][col];
matrix[rowA][col] = matrix[rowB][col];
matrix[rowB][col] = temp;
}
} else if(action.contains("col")) {
int rowA
= new Integer(action.
split(" ")[1]).
intValue(); int rowB
= new Integer(action.
split(" ")[2]).
intValue();
for(int row = 0; row < matrix.length; ++row) {
int temp = matrix[row][rowA];
matrix[rowA][row] = matrix[row][rowB];
matrix[row][rowB] = temp;
}
} else if(action.equals("inc")) {
for(int r = 0; r < matrix.length; ++r) {
for(int c = 0; c < matrix.length; ++c) {
matrix[r][c] = (matrix[r][c] + 1)%10;
}
}
} else if(action.equals("dec")) {
for(int r = 0; r < matrix.length; ++r) {
for(int c = 0; c < matrix.length; ++c) {
matrix
[r
][c
] = (Math.
abs(matrix
[r
][c
] - 1))%10
; }
}
} else {
int[][] transpose = new int[N][N];
for(int r = 0; r < matrix.length; ++r) {
for(int c = 0; c < matrix.length; ++c) {
transpose[r][c] = matrix[c][r];
}
}
matrix = transpose.clone();
}
}
writer.println("Case #"+(t+1));
for(int r = 0; r < matrix.length; ++r) {
for(int c = 0; c < matrix.length; ++c) {
writer.print(matrix[r][c]);
}
writer.println();
}
writer.println();
}
}
public static void main
(String[] args
) { new Main().run();
}
public void run() {
try {
tokenizer = null;
solve();
reader.close();
writer.close();
e.printStackTrace();
}
}
return Integer.
parseInt(nextToken
()); }
return Long.
parseLong(nextToken
()); }
return Double.
parseDouble(nextToken
()); }
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
}
return tokenizer.nextToken();
}
}