#include<stdio.h>
using namespace std;
const int maxn = 110;
int a[maxn][maxn] = { 0 };
int b[maxn][maxn] = { 0 };
int main(){
int T;
scanf("%d", &T);
while (T--){
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
k %= 4;
int t = 0;
int flag = k % 2;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
while (k--){
t++;
if (t % 2 != 0){
int x = 0;
int y = 0;
for (int i = n - 1; i >= 0; i--){
for (int j = 0; j < m; j++){
b[j][i] = a[x][y++];
}
x++;
y = 0;
}
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++)
a[i][j] = b[i][j];
}
}
else{
int x = 0;
int y = 0;
for (int i = m - 1; i >= 0; i--){
for (int j = 0; j < n; j++){
b[j][i] = a[x][y++];
}
x++;
y = 0;
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
a[i][j] = b[i][j];
}
}
}
if (flag== 0){
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (j == 0)
printf("%d", a[i][j]);
else
printf(" %d", a[i][j]);
}
printf("\n");
}
}
else{
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
if (j == 0)
printf("%d", a[i][j]);
else
printf(" %d", a[i][j]);
}
printf("\n");
}
}
printf("\n");
}
return 0;
}