#include<bits/stdc++.h>
using namespace std;
int T,n,h[65][65];
int up;
int main(){
cin>>T;
while(T--){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>h[i][j];
if(!(i%2)){
if(j>=2&&h[i][j]<h[i][j-1]) up++;
if(j==n&&h[i][j]>h[i-1][j]) up++;
}
else{
if(j>=2&&h[i][j]>h[i][j-1]) up++;
if(j==1&&i!=1&&h[i][j]>h[i-1][j]) up++;
}
}
}
int ej=0;
if(n%2) ej=n;
else ej=1;
if(up<=(n*n-1)/2){
for(int i=1;i<=n;i++){
if(i%2){
for(int j=1;j<=n;j++){
cout<<h[i][j];
if(i!=n||j!=ej) cout<<" ";
}
}
else{
for(int j=n;j>=1;j--){
cout<<h[i][j];
if(i!=n||j!=ej) cout<<" ";
}
}
}
}
else{
for(int i=n;i>=1;i--){
if(i%2){
for(int j=n;j>=1;j--){
cout<<h[i][j];
if(i*j!=1) cout<<" ";
}
}
else{
for(int j=1;j<=n;j++){
cout<<h[i][j]<<" ";
}
}
}
}
cout<<endl;
}
return 0;
}