t = eval(input())
a = [[0 for x in range(100)] for y in range(100)]
b = [[0 for x in range(100)] for y in range(100)]
for _t in range(t):
n, m, k = map(int, input().split())
k = k % 4
for i in range(n):
x = list(map(int,input().split()))
for j in range(m):
a[i][j] = x[j]
for _k in range(k):
for i in range(n):
for j in range(m):
b[j][n-i-1] = a[i][j]
for i in range(m):
for j in range(n):
a[i][j] = b[i][j]
tmp = m
m = n
n = tmp
for i in range(n):
for j in range(m):
print(a[i][j], end = " ")
print()
print()