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.io.OutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintWriter;
  import java.util.List;
  import java.util.InputMismatchException;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.io.InputStream;
  
  /**
   * Built using CHelper plug-in
   * Actual solution is at the top
   */
  public class Main {
      public static void main(String[] args) {
          InputStream inputStream = System.in;
          OutputStream outputStream = System.out;
          InputReader in = new InputReader(inputStream);
          PrintWriter out = new PrintWriter(outputStream);
          CMikasa solver = new CMikasa();
          int testCount = Integer.parseInt(in.next());
          for (int i = 1; i <= testCount; i++)
              solver.solve(i, in, out);
          out.close();
      }
  
      static class CMikasa {
          final int N = 32;
  
          long doit(long[] a) {
              long num = 0;
              for (int i = 0; i < a.length; i++) {
                  num = num * 2 + a[i];
              }
              return num;
          }
  
          public void solve(int testNumber, InputReader in, PrintWriter out) {
              long a = in.nextInt();
              long b = in.nextInt();
              if (a > b) {
                  out.println(0);
                  return;
              }
              long[] aa = new long[N];
              String s = Long.toBinaryString(a);
              for (int i = s.length() - 1; i >= 0; i--) {
                  aa[N - 1 - (s.length() - 1 - i)] = s.charAt(i) - '0';
              }
  
              List<Integer> ps = new ArrayList<>();
              int ct = 0;
              for (int i = 0; i < N; i++) {
                  if (aa[i] == 0) {
                      ct++;
                      ps.add(i);
                  }
              }
  
              long k = doit(aa);
              assert k == a;
  
              long t1 = 0;
              long t2 = (long) (Math.pow(2, ct) + 0.1) - 1;
              while (t1 < t2) {
                  long m = (t1 + t2) / 2;
                  s = Long.toBinaryString(m);
                  long[] cand = new long[N];
                  for (int i = s.length() - 1; i >= 0; i--) {
                      cand[ps.get(ps.size() - 1 - (s.length() - 1 - i))] = s.charAt(i) - '0';
                  }
                  for (int i = 0; i < N; i++) {
                      cand[i] = cand[i] ^ aa[i];
                  }
                  long d = doit(cand);
                  if (d <= b) {
                      t1 = m + 1;
                  } else {
                      t2 = m;
                  }
              }
              s = Long.toBinaryString(t1);
              long[] cand = new long[N];
              for (int i = s.length() - 1; i >= 0; i--) {
                  cand[ps.get(ps.size() - 1 - (s.length() - 1 - i))] = s.charAt(i) - '0';
              }
              out.println(doit(cand));
          }
  
      }
  
      static class InputReader {
          private static final int BUFFER_LENGTH = 1 << 10;
          private InputStream stream;
          private byte[] buf = new byte[BUFFER_LENGTH];
          private int curChar;
          private int numChars;
  
          public InputReader(InputStream stream) {
              this.stream = stream;
          }
  
          private int nextC() {
              if (numChars == -1) {
                  throw new InputMismatchException();
              }
              if (curChar >= numChars) {
                  curChar = 0;
                  try {
                      numChars = stream.read(buf);
                  } catch (IOException e) {
                      throw new InputMismatchException();
                  }
                  if (numChars <= 0)
                      return -1;
              }
              return buf[curChar++];
          }
  
          public String next() {
              int c = nextC();
              while (isSpaceChar(c)) {
                  c = nextC();
              }
              StringBuilder res = new StringBuilder();
              do {
                  res.append((char) c);
                  c = nextC();
              } while (!isSpaceChar(c));
              return res.toString();
          }
  
          public int nextInt() {
              int c = skipWhileSpace();
              int sgn = 1;
              if (c == '-') {
                  sgn = -1;
                  c = nextC();
              }
              int res = 0;
              do {
                  if (c < '0' || c > '9') {
                      throw new InputMismatchException();
                  }
                  res *= 10;
                  res += c - '0';
                  c = nextC();
              } while (!isSpaceChar(c));
              return res * sgn;
          }
  
          public int skipWhileSpace() {
              int c = nextC();
              while (isSpaceChar(c)) {
                  c = nextC();
              }
              return c;
          }
  
          public boolean isSpaceChar(int c) {
              return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
          }
  
      }
  }
  
  