def rotate(a):
row,col = len(a),len(a[0])
c = [[0 for j in range(row)] for i in range(col)]
for i in range(col):
for j in range(row):
c[i][j] = a[row-j-1][i]
return c;
for tcase in range(int(input())):
n,m,k = map(int,input().split())
a,k = [],k%4
for i in range(n):
a.append(list(map(int,input().split())))
for i in range(k):
a = rotate(a)
for i in range(len(a)):
for j in range(len(a[0])-1):
print(a[i][j],end = " ")
print(a[i][len(a[0])-1])
print()