for s in range(int(input())):
row,col,k=map(int,input().split())
a=[[0 for j in range(col)]for i in range(row) ]
for i in range(row):
s=list(map(int,input().split()))
for j in range(col):
a[i][j]=s[j]
b=[[0 for j in range(row)]for i in range(col) ]
if k%4==1:
b=[[a[row-j-1][i] for j in range(row)]for i in range(col) ]
elif k%4==2:
b=[[a[row-i-1][col-j-1] for j in range(col)]for i in range(row) ]
elif k%4==3:
b=[[a[j][col-i-1] for j in range(row)]for i in range(col) ]
elif k%4==0:
b=a
for n in b:
for m in n[:-1]:
print(m,end=" ")
print(n[-1])
print()