#include<iostream>
#include<string>
#include<vector>
using namespace std;
int n,m,k,t;
int a[105][105];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m>>k;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
if(k%4==1)
{
for(int j=0;j<m;j++)
{
for(int i=n-1;i>=0;i--)
{
cout<<a[i][j];
if(i!=0)
cout<<" ";
else
cout<<endl;
}
}
}
else if(k%4==2)
{
for(int i=n-1;i>=0;i--)
{
for(int j=m-1;j>=0;j--)
{
cout<<a[i][j];
if(j!=0)
cout<<" ";
else
cout<<endl;
}
}
}
else if(k%4==3)
{
for(int j=m-1;j>=0;j--)
{
for(int i=0;i<n;i++)
{
cout<<a[i][j];
if(i!=n-1)
cout<<" ";
else
cout<<endl;
}
}
}
else
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout<<a[i][j];
if(j!=m-1)
cout<<" ";
else
cout<<endl;
}
}
}
cout<<endl;
}
}