#include #include using namespace std; using namespace atcoder; using ll = long long; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) using mint = modint998244353; #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() template bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} template bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} const ll inf=1000000000000000000; const ll mod=998244353; const double pi=acos(-1); ll root(ll x){ ll k=(ll)sqrt(x); while(k*k>x) k--; while((k+1)*(k+1)<=x) k++; return k; } //logN void press(vector &a){ map ord; for(auto x:a) ord[x]=1; ll j=0; for(auto &p:ord){ p.second=j; j++; } for(auto &x:a) x=ord[x]; return; } mint kaijou(ll n){ mint a=1; rep(i,n){ a*=(i+1); } return a; } vector> matmul(vector> &a,vector> &b){ vector> ret(a.size(),vector(b[0].size())); rep(i,a.size()) rep(j,b[0].size()) rep(k,b.size()) ret[i][j]+=a[i][k]*b[k][j]; return ret; } vector> matpow(vector> a,ll n){ ll siz=a.size(); vector> ret(siz,vector(siz)); while(n>0){ if(n&1) ret=matmul(a,ret); rep(i,siz) ret[i][i]=1; a=matmul(a,a); n=n/2; } return ret; } int main () { ll t; cin>>t; rep(ti,t){ ll n; cin>>n; vector> r(n),c(n); rep(i,n) cin>>r[i].first; rep(i,n) cin>>c[i].first; if(n==2){ cout<<-1< a(n); rep(i,n) a[i]=c[i].first; vector> ans(n,vector(n,-1)); rep(i,n) rep(j,n) if(r[i].first==c[j].first) ans[i][j]=r[i].first; queue togo,amari; rep(i, n) { auto it = lower_bound(a.begin(), a.end(), i + 1); if (it == a.end() || *it != i + 1) amari.push(i + 1); else togo.push(i + 1); } rep(i,n){ if(!togo.empty()){ if(togo.front()!=c[i].first){ ans[togo.front()-1][i]=c[i].first; togo.pop(); } else{ togo.push(togo.front()); togo.pop(); if(togo.front()!=c[i].first){ ans[togo.front()-1][i]=c[i].first; togo.pop(); } else{ ans[amari.front()-1][i]=c[i].first; amari.pop(); } } } else{ ans[amari.front()-1][i]=c[i].first; amari.pop(); } } rep(i,n) rep(j,n) if(ans[i][j]==-1) ans[i][j]=i+1; vector> p(n,vector(n)); rep(i,n) rep(j,n) p[r[i].second][c[j].second]=ans[i][j]; rep(i,n) { rep(j,n) cout<