text: //xor basis
  //misaka and rin will carry me to cm
  #include <iostream>
  #include <cstdio>
  #include <cstring>
  #include <utility>
  #include <cassert>
  #include <algorithm>
  #include <vector>
  #include <array>
  #include <tuple>
  
  #define ll long long
  #define lb long double
  #define sz(vec) ((int)(vec.size()))
  #define all(x) x.begin(), x.end()
  
  const lb eps = 1e-9;
  const ll mod = 1e9 + 7, ll_max = 1e18;
  //const ll mod = (1 << (23)) * 119 +1;
  const int MX = 2e5 +10, int_max = 0x3f3f3f3f;
  
  using namespace std;
  
  //i will learn from moo.
  /* Input */
  template<class T> void read(T &x) { cin >> x; }
  template<class H, class T> void read(pair<H, T> &p) { cin >> p.first >> p.second; }
  template<class T, size_t S> void read(array<T, S> &a) { for (T &i : a) read(i); }
  template<class T> void read(vector<T> &v) { for (T &i : v) read(i); }
   
  template<class H, class... T> void read(H &h, T &...t) { read(h); read(t...); }
   
  /* Output */
  template<class H, class T> ostream &operator<<(ostream &o, pair<H, T> &p) { o << p.first << " " << p.second; return o; }
  template<class T, size_t S> ostream &operator<<(ostream &o, array<T, S> &a) { string s; for (T i : a) o << s << i, s = " "; return o; }
  template<class T> ostream &operator<<(ostream &o, vector<T> &v) { string s; for (T i : v) o << s << i, s = " "; return o; }
   
  template<class T> void write(T x) { cout << x; }
  template<class H, class... T> void write(const H &h, const T &...t) { write(h); write(t...); }
   
  void print() { write('\n'); }
  template<class H, class... T> void print(const H &h, const T &...t) { write(h); if (sizeof...(t)) write(' '); print(t...); }
   
  /* Misc */
  template <typename T> void ckmin(T &a, const T &b) { a = min(a, b); }
  template <typename T> void ckmax(T &a, const T &b) { a = max(a, b); }
  
  //i just want the insert function implemented
  
  inline int hsb(ll x){
  	assert(x > 0);
  	return 63-__builtin_clzll(x);
  }
  
  struct basis{
  	const int H = 63;
  	ll arr[70]; //64 actually
  	basis(){
  		memset(arr, 0, sizeof(arr));
  	}
  	ll B(int x){
  		return arr[x];
  	}	
  	ll norm(ll x){
  		for(ll i = H; ~i; i--){
  			if(B(i) && ((x^B(i)) < x)){
  				x ^= B(i);
  			}
  		}
  		return x;
  	}
  	
  	void insert(ll x){
  		if(x == 0) return ;
  		if(B(hsb(x)) == 0){
  			arr[hsb(x)] = x;
  			return ;
  		}
  		insert(norm(x));
  	}
  	void erase(int x){
  		arr[x] = 0;
  	}
  };
  
  ll tho[MX], dus[MX];
  int n, m;
  ll X;
  void solve(){
  	read(n, m);
  	memset(tho, 0, sizeof(tho));
  	memset(dus, 0, sizeof(dus));
  	X = 0;
  	for(int i = 0; i<n; i++){
  		ll a; read(a, tho[i]);
  		X ^= a;
  		tho[i] ^= a;
  	}
  	for(int i = 0; i<m; i++){
  		ll a; read(a, dus[i]);
  		X ^= a;
  		dus[i] ^= a;
  	}
  	basis T = basis();
  	basis D = basis();
  	sort(tho, tho + n, greater<ll>());
  	sort(dus, dus + m, greater<ll>());
  	for(int i = 0; i<n; i++){
  		T.insert(tho[i]);
  	}
  	for(int i = 0; i<m; i++){
  		D.insert(dus[i]);
  	}
  	for(ll i = 63; ~i; i--){
  		ll b = (T.B(i) > 0), a = (D.B(i) > 0), c = ((X&(1ll << i)) > 0);
  		int state = c*4 + b*2 + a; //hehe
  		if(state%4 == 0) continue; //x00 -> no one can flip the bit
  		//000 -> nothing
  		//001 -> dustin wont flip
  		//010 -> thomas will flip
  		//011 -> aa
  		//100 -> nothing
  		//101 -> dustin will flip
  		//110 -> thomas wont flip
  		//111 -> aaaa
  		if(state == 1 || state == 6) continue; //can flip, but wont flip
  		if(state == 2) X ^= T.B(i);
  		if(state == 5) X ^= D.B(i);
  		if(state == 7){
  			X ^= T.B(i);
  			T.insert(T.B(i)^D.B(i));
  			T.erase(i); //T.B(i) no longer matters, since dustin will always do D.B(i) after thomas
  		}
  		if(state == 3){
  			T.insert(T.B(i)^D.B(i));
  			T.erase(i);
  		}
  	}	
  	cout << X << "\n";
  }
  
  int main(){
    cin.tie(0) -> sync_with_stdio(0);
  	int T = 1;
  	cin >> T;
    while(T--){
  		solve();
  	}
  	return 0;
  }
  
  
  
  