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.
import java.util.*;
import java.io.*;
public class Sol{

/*   
->n=1 important test case
->Dont get stuck on one approach try all possibilities
->check int overflow,array bounds,etc
->Dont think just idea think of its (correct) implementation completely (then only code)
*/

public static void main(String []args){
//precomp();
int times=ni();while(times-->0){solve();}out.close();}

static void solve(){  
   int n=ni();int m=ni();
   int cn=n;
  
   int a[]=new int[31];int used[]=new int[31];
   for(int i=0;i<31;i++){a[i]=1-cn%2;cn/=2;}
   
   
   int ans=n;
   for(int i=30;i>=0;i--){if(a[i]==1){ans+=power(2,i);}}
   
   for(int i=30;i>=0;i--){if(a[i]==1){if(ans-power(2,i)>m)ans-=power(2,i);}}
   out.println(ans-n);
   return;
 }

// 1 0 1 0 0 1 = 41
// 0 1 0 1 1 0
//           0 = 0  m<=40
//         1 0 = 2  m<=42
//       1 0 0 = 4  m<=
//   1 0 0 0 0 = 16
//       1 1 0 = 6
//   1 0 1 0 0 = 20
//   1 0 1 1 0 = 22
//   1 0 0 1 0 = 18
 

//-----------------Utility--------------------------------------------

static int gcd(int a,int b){if(b==0)return a; return gcd(b,a%b);}
 
static int Max=Integer.MAX_VALUE; static long mod=1000000007;
 
static int v(char c){return (int)(c-'a')+1;}

public static long power(long x, long y )
    {
        //0^0 = 1
        long res = 1L;
        x = x%mod;
        while(y > 0)
        {
            if((y&1)==1)
                res = (res*x)%mod;
            y >>= 1;
            x = (x*x)%mod;
        }
        return res;
    }
 
static class Pair implements Comparable<Pair>{
        int id;int value;Pair next;
        public Pair(int id,int value) {
         
          this.id=id;this.value=value;next=null;
        }
        @Override
        public int compareTo(Pair p){return Long.compare(value,p.value);}
    }

//----------------------I/O---------------------------------------------
 
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static FastReader in=new FastReader(inputStream);
static PrintWriter out=new PrintWriter(outputStream);
 
static class FastReader 
{ 
  BufferedReader br; 
  StringTokenizer st; 
 
        FastReader(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }
 
  public String next() 
  { 
      while (st == null || !st.hasMoreElements()) 
      { 
          try
          { 
              st = new StringTokenizer(br.readLine()); 
          } 
          catch (IOException  e) 
          { 
              e.printStackTrace(); 
          } 
      } 
      return st.nextToken(); 
  } 
 
  public int nextInt() 
  { 
      return Integer.parseInt(next()); 
  } 
 
  public long nextLong() 
  { 
      return Long.parseLong(next()); 
  } 
 
 public  double nextDouble() 
  { 
      return Double.parseDouble(next()); 
  } 
 
 
  String nextLine() 
  { 
      String str = ""; 
      try
      { 
          str = br.readLine(); 
      } 
      catch (IOException e) 
      { 
          e.printStackTrace(); 
      } 
      return str; 
  } 
}
static int ni() {
        return in.nextInt();
    }
static long nl(){return in.nextLong();}
static String ns(){return in.nextLine();}
//---------------------I/O------------------------------------------------- 
}