def xuanzhuan(a):
n,m=len(a),len(a[0])
b=[[0 for j in range(n)]for i in range(m)]
for i in range(len(b)):
for j in range(len(b[0])):
b[i][j]=a[n-1-j][i]
return b
for case in range(int(input())):
n,m,k=list(map(int,input().split()))
a=[]
for i in range(n):
a.append(list(map(int,input().split())))
i=k%4
while i>0:
a=xuanzhuan(a)
i-=1
for i in range(len(a)):
for j in range(len(a[0])-1):
print(a[i][j],end=" ")
print(a[i][-1])
print()