tcase = input()
for case in range(tcase):
n,m,k=map(int,raw_input().strip().split())
myList = [[0] * m] * n
for i in range(n):
myList[i]=map(int,raw_input().strip().split())
k=k%4
s=''
if k==0:
for i in range(n):
for j in range(m-1):
s=s+str(myList[i][j])+' '
s=s+str(myList[i][m-1])
print s
s=''
if k==1:
for i in range(m):
for j in range(n-1,0,-1):
s=s+str(myList[j][i])+' '
s=s+str(myList[0][i])
print s
s=''
if k==2:
for i in range(n-1,-1,-1):
for j in range(m-1,0,-1):
s=s+str(myList[i][j])+' '
s=s+str(myList[i][0])
print s
s=''
if k==3:
for i in range(m-1,-1,-1):
for j in range(n-1):
s=s+str(myList[j][i])+' '
s=s+str(myList[n-1][i])
print s
s=''
print