#include using namespace std; #define vi vector #define vii vector> #define ii pair using ll = long long int; #define pb push_back #define ss second #define ff first #define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++) #define nl "\n" #define br cout << "\n"; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); // #define mod 419 // #define mod 1000000007 // #define mod 10000000002065383 const int N = 2e5 + 100; // const int M = 1000000000; #ifndef ONLINE_JUDGE #include "debug.h" #else #define debug(...) 42 #endif template istream& operator>>(istream& in, vector& v) { for (auto& elem : v) { in >> elem; } return in; } void print(vi &v) { for (auto x : v) cout << x << " "; cout << endl; } /* if even >= 2 print even[0] even[1] for: odds>1: */ void solve() { ll n,cur=0; cin >> n; vi v((n-1)*(n-1)); cin >> v; sort(all(v)); vector> b(n-1, vector(n-1)); rep(i,0,n-1) rep(j,0,n-1) b[i][j] = v[cur++]; rep(i,0,n-1){ if(i&1) sort(rall(b[i])); else sort(all(b[i])); } debug(b); vector> a(n, vector(n)); rep(i,1,n){ if(i&1){ rep(j,1,n){ a[i][j] = b[i-1][j-1] - a[i-1][j-1] - a[i-1][j] - a[i][j-1]; } }else{ for(ll j = n-2; j>=0 ;j--){ a[i][j] = b[i-1][j] - a[i-1][j+1] - a[i-1][j] - a[i][j+1]; } } } rep(i,0,n){ rep(j,0,n){ cout << a[i][j] << " "; } cout << nl; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int Test = 1; cin >> Test; while (Test--) { solve(); } return 0; }