//#pragma GCC target("avx2") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") #include using namespace std; using ll = long long; using pii = pair; using pll = pair; using pli = pair; #define MOD 998244353 //#define MOD 1000000007 #define el '\n' #define El '\n' #define YESNO(x) ((x) ? "Yes" : "No") #define YES YESNO(true) #define NO YESNO(false) #define EXIT_ANS(x) {cout << (x) << '\n'; return;} template void inline SORT(vector &v){sort(v.begin(),v.end()); return;} template void inline REV(vector &v){reverse(v.begin(),v.end()); return;} template void inline VEC_UNIQ(vector &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;} template T inline MAX(vector &v){return *max_element(v.begin(),v.end());} template T inline MIN(vector &v){return *min_element(v.begin(),v.end());} template T inline SUM(vector &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;} template void inline DEC(vector &v){for(int i = 0; i < (int)v.size(); i++)v[i]--; return;} template void inline INC(vector &v){for(int i = 0; i < (int)v.size(); i++)v[i]++; return;} void inline TEST(void){cerr << "TEST" << endl; return;} template bool inline chmin(T &x,T y){ if(x > y){ x = y; return true; } return false; } template bool inline chmax(T &x,T y){ if(x < y){ x = y; return true; } return false; } template vector inline get_vec(int n){ vector ans(n); for(int i = 0; i < n; i++)cin >> ans[i]; return ans; } template void inline print_vec(vector &vec,bool kaigyou = false){ int n = (int)vec.size(); for(int i = 0; i < n; i++){ cout << vec[i]; if(kaigyou || i == n - 1)cout << '\n'; else cout << ' '; } if(!n)cout << '\n'; return; } template void inline debug_vec(vector &vec,bool kaigyou = false){ int n = (int)vec.size(); for(int i = 0; i < n; i++){ cerr << vec[i]; if(kaigyou || i == n - 1)cerr << '\n'; else cerr << ' '; } if(!n)cerr << '\n'; return; } vector> inline get_graph(int n,int m = -1,bool direct = false){ if(m == -1)m = n - 1; vector> g(n); while(m--){ int u,v; cin >> u >> v; u--; v--; g[u].push_back(v); if(!direct)g[v].push_back(u); } return g; } template vector>> inline get_weighted_graph(int n,int m = -1,bool direct = false){ if(m == -1)m = n - 1; vector>> g(n); while(m--){ int u,v; cin >> u >> v; u--; v--; ll w; cin >> w; g[u].push_back(pair(w,v)); if(!direct)g[v].push_back(pair(w,u)); } return g; } // 疑似乱数(XorShift) unsigned long xor128(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } #define MULTI_TEST_CASE true void solve(void){ //問題を見たらまず「この問題設定から言えること」をいっぱい言う //よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く //複数の解法のアイデアを思いついた時は全部メモしておく //g++ -D_GLIBCXX_DEBUG -Wall -O2 a.cpp -o o int n; cin >> n; vector b = get_vec((n - 1) * (n - 1)); vector> ans(n,vector(n)); while(1){ vector idx((n - 1) * (n-1)); for(int i = 0; i < (int)idx.size(); i++)idx[i] = pair(xor128(),i); SORT(idx); vector> c(n - 1,vector(n - 1)); int cnt = 0; for(int i = 0; i < (int)idx.size(); i++)c[idx[i].second / (n - 1)][idx[i].second % (n - 1)] = b[i]; //for(int i = 0; i < n - 1; i++)debug_vec(c[i]); ll mn = 0; for(int i = 1; i < n; i++){ for(int j = 1; j < n; j++){ //b[i-1][j-1] = a[i-1][j-1] + a[i][j-1] + a[i -1][j] + a[i][j] ans[i][j] = c[i - 1][j - 1] - (ans[i - 1][j - 1] + ans[i - 1][j] + ans[i][j - 1]); chmin(mn,ans[i][j]); } } if(mn == 0)break; } for(int i = 0; i < n; i++)print_vec(ans[i]); return; } void calc(void){ return; } signed main(void){ cin.tie(nullptr); ios::sync_with_stdio(false); calc(); int t = 1; if(MULTI_TEST_CASE)cin >> t; while(t--){ solve(); } return 0; }