#include #include #include using namespace std; void solve() { int n; cin >> n; vector a(n), b(n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; vector> ans(n, vector (n)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) ans[i][j] = a[i]; if (n > 4) { for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { int x = (i + j) % n; ans[x][i] = b[i]; } } } else if (n == 2) { cout << "-1\n"; return; } else if (n == 1) { cout << 1 << endl; return; } else { vector p(n); for (int i = 0; i < n; ++i) p[i] = i; int af = 0; do { int flag = 0; for (int i = 0; i < n; ++i) { if (ans[p[i]][i] == b[i]) { flag = 1; break; } } if (flag) continue; for (int i = 0; i < n; ++i) ans[p[i]][i] = b[i]; af = 1; break; } while (next_permutation(p.begin(), p.end())); if (af == 0) { cout << -1 << endl; return; } } for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cout << ans[i][j] << " \n"[j == n - 1]; } int main() { int t; cin >> t; while (t--) solve(); }