def xz(ls,n,m):
ls1 = []
a,b=n-1,0
for i in range(m):
ls1.append([])
for j in range(n):
ls1[i].append(ls[a][b])
a = a - 1
b = b + 1
a = n - 1
return ls1
for t in range(int(input())):
n,m,k = map(int,input().split())
ls = []
for i in range(n):
ls.append(list(map(int,input().split())))
for i in range(k%4):
ls = xz(ls,n,m)
n,m = m,n
n,m = m,n
for i in range(m):
for j in range(n):
print(ls[i][j],end = ('' if j == n else ' '))
print()
print()