t=int(input())
s=0
while t>0:
t1=input().split()
n=int(t1[0])
m=int(t1[1])
k=int(t1[2])
a=[[0]*m]*n
for i in range(n):
a[i]=[int(j) for j in input().split()]
if s>0:print()
s=s+1
k=k%4
if k==1:
for j in range(m):
for i in range(n-1,-1,-1):
if i!=0:print(a[i][j],end=' ')
else:print(a[i][j])
elif k==2:
for i in range(n-1,-1,-1):
for j in range(m-1,-1,-1):
if j!=0:print(a[i][j],end=' ')
else:print(a[i][j])
elif k==3:
for j in range(m-1,-1,-1):
for i in range(n):
if i!=n-1:print(a[i][j],end=' ')
else:print(a[i][j])
else:
for i in range(n):
for j in range(m):
if j!=m-1:print(a[i][j],end=' ')
else:print(a[i][j])
t=t-1