#include #include using namespace std; using namespace atcoder; using ll=long long; using ldub=long double; using lldub=__float128; using str=string; using mint=modint; template using tup2=tuple; template using tup3=tuple; template using tup4=tuple; template using tup5=tuple; template using tup6=tuple; template using tup7=tuple; template using vec=vector; template using vec2=vector>; template using vec3=vector>; template using vec4=vector>; template using vec5=vector>; template using vec6=vector>; template using que=queue; template using Pque=priority_queue; template using pque=priority_queue, greater>; struct Edge{ ll from,to,w=1,num=-1; }; using gvec=vector; using gvec2=vector; template bool chmax(T &a,T b){if(a bool chmin(T &a,T b){if(b,vec2> M; void solve(){ ll N; cin >> N; vec X(N),Y(N),Z(2*N-1); for(ll i=0;i> X[i]; for(ll j=0;j> Y[j]; for(ll t=0;t<2*N-1;t++) cin >> Z[t]; vec2 A(N,vec(N)); for(ll k=0;k<30;k++){ ll f=0,g=0; for(ll i=0;i>k)&1)==1) f=1; for(ll j=0;j>k)&1)==0) g=1; if(f&&g){ cout << -1 << endl; return; } vec2 F(N,vec(N,-1)); if(f){ for(ll i=0;i>k)&1)==1) F[i][j]=1; } vec C(N); for(ll i=0;i>k)&1)==1) C[i]=N; for(ll t=0;t<2*N-1;t++){ ll s=0; for(ll i=0;i<=t;i++){ ll j=t-i; if(i>=N||j>=N) continue; if(F[i][j]!=-1) s^=1; } for(ll i=0;i<=t;i++){ ll j=t-i; if(i>=N||j>=N) continue; if(F[i][j]!=-1) continue; if((((Z[t])>>k)&1)==s||C[i]==N-1) F[i][j]=0; else F[i][j]=1,s^=1,C[i]++; } } } if(g){ for(ll i=0;i>k)&1)==0) F[i][j]=0; } vec C(N); for(ll j=0;j>k)&1)==0) C[j]=N; for(ll t=0;t<2*N-1;t++){ ll s=0; for(ll j=0;j<=t;j++){ ll i=t-j; if(i>=N||j>=N) continue; if(F[i][j]==-1) s^=1; } for(ll j=0;j<=t;j++){ ll i=t-j; if(i>=N||j>=N) continue; if(F[i][j]!=-1) continue; if((((Z[t])>>k)&1)==s||C[j]==N-1) F[i][j]=1; else F[i][j]=0,s^=1,C[j]++; } } } if(!f&&!g){ if(N<=3){ vec E(2*N-1); for(ll t=0;t<2*N-1;t++) E[t]=(Z[t]>>k)&1; if(!M.count(E)){ cout << -1 << endl; return; } F=M[E]; }else{ for(ll i=0;i=N||j>=N) continue; if(F[i][j]!=-1) s^=1; } if(((Z[t]>>k)&1)==s) F[t-t/2][t/2]=0; else F[t-t/2][t/2]=1; } } } for(ll i=0;i>k)&1; ll f=1; for(ll i=0;i> T; while(T--) solve(); return 0; }