#!/usr/bin/python
# -*- coding: UTF-8 -*-
tcase=int(raw_input().strip())
for i in range(tcase):
n,m,k=map(int,raw_input().strip().split())
Matrix=[]
for j in range(n):
Matrix.append(map(int,raw_input().strip().split()))
s=""
for num in range(1,k+1):
if num % 2==1:
a=m
b=n
else:
a=n
b=m
Matrix1=[[0 for i in range(b)] for j in range(a)]
for i in range(a):
for j in range(b):
Matrix1[i][j]=Matrix[b-j-1][i]
Matrix=Matrix1
s=""
for row in range(len(Matrix)):
for col in range(len(Matrix[0])):
print str(Matrix[row][col]),
print
print