#include #include #include #include #include using namespace std; long long readInt(long long l,long long r,char endd){ long long x=0; int cnt=0; int fi=-1; bool is_neg=false; while(true){ char g=getchar(); if(g=='-'){ assert(fi==-1); is_neg=true; continue; } if('0'<=g && g<='9'){ x*=10; x+=g-'0'; if(cnt==0){ fi=g-'0'; } cnt++; assert(fi!=0 || cnt==1); assert(fi!=0 || is_neg==false); assert(!(cnt>19 || ( cnt==19 && fi>1) )); } else if(g==endd){ assert(cnt>0); if(is_neg){ x= -x; } if(!(l<=x && x<=r)){ cerr< > comp; void dfs(int ind,int ty,int xr){ if(!ok)return; comp.push_back(make_pair(ind,ty)); if(ty == 1){ val_a[ind] = xr; for(int j=0;j> arr){ if(lvl == -1)return xr; int k=arr.size(); bool is_zero=true,is_one=true; vector > tm; for(int i=0;i> lvl) << lvl ) + (1<> lvl) << lvl ); if( mn_possible > R[arr[i].first]){ is_zero=false; } if(mx_possible < L[arr[i].first]){ is_zero=false; } if(! (L[arr[i].first]<= mn_possible && mx_possible <=R[arr[i].first])){ tm.push_back(arr[i]); } } else { int mx_possible=(((val_b[arr[i].first] ^ xr) >> lvl) << lvl ) + (1<> lvl) << lvl ); if( mn_possible > Q[arr[i].first]){ is_zero=false; } if(mx_possible < P[arr[i].first]){ is_zero=false; } if(! (P[arr[i].first]<= mn_possible && mx_possible <=Q[arr[i].first])){ tm.push_back(arr[i]); } } } if(is_zero){ int h= solve(lvl-1,xr,tm); if(h != -1)return h; } xr |= 1<> lvl) << lvl ) + (1<> lvl) << lvl ); if( mn_possible > R[arr[i].first]){ is_one=false; } if(mx_possible < L[arr[i].first]){ is_one=false; } if(! (L[arr[i].first]<= mn_possible && mx_possible <=R[arr[i].first])){ tm.push_back(arr[i]); } } else { int mx_possible=(((val_b[arr[i].first] ^ xr) >> lvl) << lvl ) + (1<> lvl) << lvl ); if( mn_possible > Q[arr[i].first]){ is_one=false; } if(mx_possible < P[arr[i].first]){ is_one=false; } if(! (P[arr[i].first]<= mn_possible && mx_possible <=Q[arr[i].first])){ tm.push_back(arr[i]); } } } if(is_one){ int h= solve(lvl-1,xr,tm); if(h != -1)return h; } return -1; } int main(){ //freopen("06.txt","r",stdin); //freopen("06o.txt","w",stdout); T=readIntLn(1,100); while(T--){ n=readIntSp(1,1000); m=readIntLn(1,1000); sm_nm+=n*m; ok=true; for(int i=0;i