def tran(li):
n = len(li);
m = len(li[0]);
t =[[0 for col in range(n)] for row in range(m)]
for i in range(0,m):
for j in range(0,n):
t[i][j] = li[n-1-j][i]
#print i,j,t,t[j][m-1-j],li[i][j]
return t
tcase = int(raw_input().strip())
for case in range(tcase):
n,m,k = map(int, raw_input().strip().split())
li = []
for i in range(n):
li.append(map(int, raw_input().strip().split()))
for j in range(k):
li = tran(li)
for i in range(len(li)):
for j in range(len(li[0])):
print li[i][j],
print
print