tcase=int(raw_input())
for case in range(tcase):
data = []
a,b,c=map(int,raw_input().strip().split())
for i in range(a):
data.append(map(int,raw_input().strip().split()))
line=len(data)
row=len(data[0])
if c % 4==0:
for i in range(len(data)):
for j in range(len(data[0])):
print data[i][j],
print
elif c % 4==1:
L=[]
R=[]
for i in range(row):
for j in range(line):
L.append(data[line-j-1][i])
R.append(L)
L=[]
for i in range(len(R)):
for j in range(len(R[0])):
print R[i][j],
print
elif c % 4==2:
L=[]
R=[]
for i in range(line):
for j in range(row):
L.append(data[line-i-1][row-j-1])
R.append(L)
L=[]
for i in range(len(R)):
for j in range(len(R[0])):
print R[i][j],
print
elif c % 4==3:
L=[]
R=[]
for i in range(row):
for j in range(line):
L.append(data[j][row-i-1])
R.append(L)
L=[]
for i in range(len(R)):
for j in range(len(R[0])):
print R[i][j],
print
print