#include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } void solve() { int n; cin >> n; vector r(n), c(n); rep(i, 0, n) cin >> r[i], r[i]--; rep(i, 0, n) cin >> c[i], c[i]--; auto reorder = [&](vector> ans) { vector> nans; { vector> nr(n); rep(i, 0, n) { vector cnt(n); rep(j, 0, n) cnt[ans[i][j]]++; int x = max_element(all(cnt)) - cnt.begin(); nr[x].push_back(i); } rep(i, 0, n) { nans.push_back(ans[nr[r[i]].back()]); nr[r[i]].pop_back(); } swap(ans, nans); } { vector> nc(n); rep(j, 0, n) { vector cnt(n); rep(i, 0, n) cnt[ans[i][j]]++; int x = max_element(all(cnt)) - cnt.begin(); nc[x].push_back(j); } rep(j, 0, n) { int nj = nc[c[j]].back(); nc[c[j]].pop_back(); rep(i, 0, n) nans[i][j] = ans[i][nj]; } } return nans; }; auto build_per = [&]() { vector> ans(n, vector(n, -1)); vector rr(n), rc(n); rep(i, 0, n) rr[r[i]] = i; rep(i, 0, n) rc[c[i]] = i; rep(i, 0, n) { ans[rr[i]][rc[i]] = i; ans[rr[(i + 1) % n]][rc[i]] = i; ans[rr[i]][rc[(i + 1) % n]] = i; if (n == 4) ans[rr[i]][rc[(i + 2) % n]] = i; } return ans; }; if (n == 1) { cout << "1\n"; return; } if (n == 2) { cout << "-1\n"; return; } if (n == 3) { if (c[0] == c[1] && c[0] == c[2]) { cout << "-1\n"; return; } if ((c[0] ^ c[1] ^ c[2]) == 3) { vector> ans = { {0, 0, 1}, {2, 1, 1}, {2, 0, 2}, }; ans = reorder(ans); rep(i, 0, n) { rep(j, 0, n) cout << ans[i][j] + 1 << ' '; cout << '\n'; } return; } vector> ans = { {0, 0, 1}, {0, 1, 1}, {2, 0, 2}, }; vector cc(n); rep(i, 0, n) cc[c[i]]++; vector to(n, -1); rep(i, 0, n) { if (cc[i] == 0) to[2] = i; if (cc[i] == 1) to[1] = i; if (cc[i] == 2) to[0] = i; } rep(i, 0, n) rep(j, 0, n) ans[i][j] = to[ans[i][j]]; ans = reorder(ans); rep(i, 0, n) { rep(j, 0, n) cout << ans[i][j] + 1 << ' '; cout << '\n'; } return; } if (n == 4) { int sz = (int)set(all(c)).size(); if (sz == n) { auto ans = build_per(); rep(i, 0, n) { rep(j, 0, n) cout << ans[i][j] + 1 << ' '; cout << '\n'; } return; } if (sz == 1) { cout << "-1\n"; return; } vector> ans; vector cc(n); rep(i, 0, n) cc[c[i]]++; vector> cnt(n); if (sz == 2) { if (ranges::max(cc) == 3) { ans = { {0, 0, 0, 0}, {1, 1, 0, 1}, {0, 2, 1, 2}, {3, 0, 3, 1}, }; cnt[0] = {2, 3}; cnt[1] = {1}; cnt[3] = {0}; } else { ans = { {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 2, 1, 2}, {3, 0, 3, 1}, }; cnt[0] = {2, 3}; cnt[2] = {0, 1}; } } else { ans = { {0, 0, 0, 0}, {1, 0, 1, 2}, {2, 2, 2, 2}, {0, 3, 1, 3}, }; cnt[0] = {3}; cnt[1] = {1, 2}; cnt[2] = {0}; } vector to(n, -1); rep(i, 0, n) { to[cnt[cc[i]].back()] = i; cnt[cc[i]].pop_back(); } rep(i, 0, n) rep(j, 0, n) ans[i][j] = to[ans[i][j]]; ans = reorder(ans); rep(i, 0, n) { rep(j, 0, n) cout << ans[i][j] + 1 << ' '; cout << '\n'; } return; } vector> ans(n, vector(n, -1)); rep(i, 0, n) { rep(j, 0, n - 2) { ans[i][(i + j) % n] = r[i]; } } rep(j, 0, n) { rep(i, 0, n) if (ans[i][j] == -1) ans[i][j] = c[j]; } rep(i, 0, n) { rep(j, 0, n) cout << ans[i][j] + 1 << ' '; cout << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int t = 1; cin >> t; while (t--) solve(); }