description: You are given two integers n and m. Find the \operatorname{MEX} of the sequence n ⊕ 0, n ⊕ 1, …, n ⊕ m. Here, ⊕ is the [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
  
  \operatorname{MEX} of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, \operatorname{MEX}(0, 1, 2, 4) = 3, and \operatorname{MEX}(1, 2021) = 0. 
  
  Input
  
  The first line contains a single integer t (1 ≤ t ≤ 30 000) — the number of test cases.
  
  The first and only line of each test case contains two integers n and m (0 ≤ n, m ≤ 10^9).
  
  Output
  
  For each test case, print a single integer — the answer to the problem.
  
  Example
  
  Input
  
  
  5
  3 5
  4 6
  3 2
  69 696
  123456 654321
  
  
  Output
  
  
  4
  3
  0
  640
  530866
  
  Note
  
  In the first test case, the sequence is 3 ⊕ 0, 3 ⊕ 1, 3 ⊕ 2, 3 ⊕ 3, 3 ⊕ 4, 3 ⊕ 5, or 3, 2, 1, 0, 7, 6. The smallest non-negative integer which isn't present in the sequence i. e. the \operatorname{MEX} of the sequence is 4.
  
  In the second test case, the sequence is 4 ⊕ 0, 4 ⊕ 1, 4 ⊕ 2, 4 ⊕ 3, 4 ⊕ 4, 4 ⊕ 5, 4 ⊕ 6, or 4, 5, 6, 7, 0, 1, 2. The smallest non-negative integer which isn't present in the sequence i. e. the \operatorname{MEX} of the sequence is 3.
  
  In the third test case, the sequence is 3 ⊕ 0, 3 ⊕ 1, 3 ⊕ 2, or 3, 2, 1. The smallest non-negative integer which isn't present in the sequence i. e. the \operatorname{MEX} of the sequence is 0.
solution: import java.util.*;
  import java.io.*;
  
  public class c {
  
  	public static void main(String[] args) throws Exception {
  	
  		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  		StringTokenizer tok = new StringTokenizer(stdin.readLine());
  		int nC = Integer.parseInt(tok.nextToken());
  		
  		StringBuffer sb = new StringBuffer();
  		for (int loop=0; loop<nC; loop++) {
  			tok = new StringTokenizer(stdin.readLine());
  			int n = Integer.parseInt(tok.nextToken());
  			int m = Integer.parseInt(tok.nextToken());
  			sb.append(solve(n,m)+"\n");
  			//System.out.println(solve(n,m)+" "+altSolve(n,m));
  		}
  		System.out.print(sb);
  	}
  	
  	public static int altSolve(int n, int m) {
  		
  		int next = numBits(Math.max(n,m))+1;
  		boolean[] used = new boolean[1<<next];
  		
  		for (int i=0; i<=m; i++)
  			used[n^i] = true;
  		for (int i=0; i<used.length; i++)
  			if (!used[i])
  				return i;
  		return -1;
  		
  	}
  	
  	public static int solve(int n, int m) {
  		
  		if (m < n) return 0;
  		
  		// all ones case.
  		if (perfect2(m)) return m+1;
  		
  		// cases same bits
  		// m has more bits than n.
  		int mBits = numBits(m);
  		int nBits = numBits(n);
  		
  		if (mBits > nBits) {
  			int restM = (m >> nBits) << nBits;
  			int add = solve(n, m & ((1<<nBits)-1));
  			return restM+add;
  		}
  		
  		//System.out.println(Integer.toBinaryString(n));
  		//System.out.println(Integer.toBinaryString(m));
  
  		int lastZeroBit = 1;
  		while ((m & lastZeroBit) != 0) lastZeroBit <<= 1;
  		
  		int start = Integer.highestOneBit(n);
  		boolean hasOneZero = false;
  		int res = 0;
  		
  		for (int i=start; i>=1; i>>=1) {
  			int mChoice = m & i;
  			int nChoice = n & i;
  			
  			if (nChoice == 0 && mChoice > 0)
  				res += i;
  			else if (nChoice > 0 && mChoice > 0 && i < lastZeroBit)
  				res += i;
  			else if (nChoice > 0 && mChoice == 0) {
  				hasOneZero = true;
  				break;
  			}
  		}
  		
  
  		
  		if (hasOneZero) return res;
  		else {
  			
  			return res + 1;
  		}
  	}
  	
  	public static boolean perfect2(int m) {
  		return Integer.bitCount(m+1) == 1;
  	}
  	
  	public static int numBits(int n) {
  		int res = 0;
  		while (n > 0) {
  			res++;
  			n /= 2;
  		}
  		return res;
  	}
  }