#include<stdio.h>
int jz[100][100]={0};
int jz2[100][100]={0};
int main (void){
int n,m,k,t,i,j,jh,i2,j2;
jh=0;
scanf("%d",&t);
while (t--){
scanf("%d %d %d",&n,&m,&k);
n=n-1;
m=m-1;
for (i=0;i<=n;i++){
for (j=0;j<=m;j++){
scanf("%d",&jz[i][j]);
}
}
k=k%4;
if (k==1){
for (i2=0;i2<=m;i2++){
for (j2=0;j2<=n;j2++){
jz2[i2][j2]=jz[n-j2][i2];
}
}
for (i=0;i<=m;i++){
for (j=0;j<=n;j++){
printf("%d ",jz2[i][j]);
}
printf("\n");
}
printf("\n");
}
else if (k==2){
for (i2=0;i2<=n;i2++){
for (j2=0;j2<=m;j2++){
jz2[i2][j2]=jz[n-i2][m-j2];
}
}
for (i=0;i<=n;i++){
for (j=0;j<=m;j++){
printf("%d ",jz2[i][j]);
}
printf("\n");
}
printf("\n");
}
else if (k==3){
for (i2=0;i2<=m;i2++){
for (j2=0;j2<=n;j2++){
jz2[i2][j2]=jz[j2][m-i2];
}
}
for (i=0;i<=m;i++){
for (j=0;j<=n;j++){
printf("%d ",jz2[i][j]);
}
printf("\n");
}
printf("\n");
}
else {
for (i2=0;i2<=n;i2++){
for (j2=0;j2<=m;j2++){
jz2[i2][j2]=jz[i2][j2];
}
}
for (i=0;i<=n;i++){
for (j=0;j<=m;j++){
printf("%d ",jz2[i][j]);
}
printf("\n");
}
printf("\n");
}
}
}