t=int(input())
a=[[[0 for i in range(100)]] for i in range(100)]
for tt in range(t):
n,m,k=list(map(int,input().split()))
for i in range(0,n):
a[i]=list(map(int,input().split()))
if k%4==1:
for j in range(0,m):
for i in range(n-1,-1,-1):
print(a[i][j],end=" ")
print("")
elif k%4==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],end=" ")
print("")
elif k%4==3:
for i in range(m-1,-1,-1):
for j in range(0,n):
if j==n-1:
print(a[j][i],end="")
else:
print(a[j][i],end=" ")
print("")
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],end=" ")
print("")
print("")