#include using namespace std; #define all(v) (v).begin(),(v).end() #define pb emplace_back #define rep(i, n) for(int i=0;i<(n);i++) #define foa(e, v) for(auto& e : v) #define dout(a) cout< using pqr = priority_queue, greater>; template inline bool chmax(T1 &a, T2 b) { bool compare = a < b; if(compare) a = b; return compare; } template inline bool chmin(T1 &a, T2 b) { bool compare = a > b; if(compare) a = b; return compare; } template inline T back(std::set &s) { return *s.rbegin(); } template inline T back(std::multiset &s) { return *s.rbegin(); } template inline T pop_back(std::set &s) { auto it = prev(s.end()); T val = *it; s.erase(it); return val; } template inline T pop_back(std::multiset &s) { auto it = prev(s.end()); T val = *it; s.erase(it); return val; } const int dy[8] = {-1, 0, 0, 1, 1, -1, 1, -1}; const int dx[8] = {0, -1, 1, 0, -1, -1, 1, 1}; const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (3LL << 59); const int inf = 1 << 30; const char br = '\n'; // depends on dijkstra vector USHI(ll n, vector> edges, ll s) { // v : x_i - x_j <= M vector>> G(n); for(auto [i, j, M] : edges) { G[j].push_back({M, i}); } vector dist(n, INF); dist[s] = 0; bool update = 1; bool ng = 0; rep(_, n) { update = 0; rep(i, n) { for(auto [d, node]: G[i]) { if(dist[i] + d < dist[node]) { dist[node] = dist[i] + d; update = 1; } } } if(!update) break; else if(_ == n - 1) ng = 1; } if(ng) { dist[0] = -1; } return dist; } vector> calc(vector> &s) { int n = s.size() + 1; int m = n; vector ans(n, vector(m, 0LL)); for(int i = 1; i < n; i ++) { for(int j = 1; j < m; j ++) { ans[i][j] = s[i - 1][j - 1] - ans[i - 1][j] - ans[i][j - 1] - ans[i - 1][j - 1]; } } vector> edges; rep(i, n) { rep(j, m) { bool flag = (i + j) & 1; if(!flag) { edges.pb(i, j + n, ans[i][j]); } else { edges.pb(j + n, i, ans[i][j]); } } } auto dis = USHI(n + m, edges, 0); if(dis[0] == -1) { ans[0][0] = -1; return ans; } else { rep(i, n) rep(j, m) { bool flag = (i + j) & 1; if(!flag) { ans[i][j] += dis[j + n] - dis[i]; } else { ans[i][j] += dis[i] - dis[j + n]; } } } return ans; } void solve() { ll n; cin >> n; vector a((n - 1) * (n - 1)); for(ll &e : a) cin >> e; sort(all(a)); { vector s(n - 1, vector(n - 1, 0LL)); rep(i, n - 1) rep(j, n - 1) s[i][j] = a[i * (n - 1) + j]; auto ret = calc(s); if(ret[0][0] != -1) { rep(i, n) { rep(j, n) { cout << ret[i][j] << " "; } cout << endl; } return; } } cout << -1 << endl; return; } int main() { cin.tie(0); ios::sync_with_stdio(false); int testcase = 1; cin >> testcase; while(testcase --) solve(); return 0; }