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.*;
  public class Mikasa{
      static class InputReader {
          private static final int DEFAULT_BUFFER_SIZE = 1 << 16; // Change this to increase your input size //
          private static final InputStream DEFAULT_STREAM = System.in;private static final int MAX_DECIMAL_PRECISION = 21;private int c;private byte[] buf;
          private int bufferSize, bufIndex, numBytesRead;private InputStream stream;private static final byte EOF = -1;private static final byte NEW_LINE = 10;
          private static final byte CARRIAGE_RETURN = 13;private static final byte SPACE = 32;private static final byte DASH = 45;private static final byte DOT = 46;
          private char[] charBuffer;private static byte[] bytes = new byte[58];private static int[] ints = new int[58];private static char[] chars = new char[128];
          static { char ch = ' ';int value = 0;byte _byte = 0;for (int i = 48; i < 58; i++) bytes[i] = _byte++;for (int i = 48; i < 58; i++) ints[i] = value++;for (int i = 32; i < 128; i++) chars[i] = ch++; }
          public InputReader() { this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE); }public InputReader(int bufferSize) { this(DEFAULT_STREAM, bufferSize); }public InputReader(InputStream stream) { this(stream, DEFAULT_BUFFER_SIZE); }
          public InputReader(InputStream stream, int bufferSize) { if (stream == null || bufferSize <= 0) throw new IllegalArgumentException();buf = new byte[bufferSize];charBuffer = new char[128];this.bufferSize = bufferSize;this.stream = stream; }
          private byte read() throws IOException { if (numBytesRead == EOF) throw new IOException();if (bufIndex >= numBytesRead) { bufIndex = 0;numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF; }return buf[bufIndex++]; }
          private int readJunk(int token) throws IOException { if (numBytesRead == EOF) return EOF;do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > token) return 0;bufIndex++; }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return EOF;bufIndex = 0; } while (true); }
          public byte nextByte() throws IOException {return (byte) ni(); }
          public int ni() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1, res = 0;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead)
          { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); }
          public long nl() throws IOException { if (readJunk(DASH - 1) == EOF) throw new IOException();int sgn = 1;long res = 0L;c = buf[bufIndex];if (c == DASH) { sgn = -1;bufIndex++; } do { while (bufIndex < numBytesRead)
          { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1);res += ints[buf[bufIndex++]]; } else { bufIndex++;return res * sgn; } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return res * sgn;bufIndex = 0; } while (true); }
          private void doubleCharBufferSize() { char[] newBuffer = new char[charBuffer.length << 1];for (int i = 0; i < charBuffer.length; i++) newBuffer[i] = charBuffer[i];charBuffer = newBuffer; }
          public String nli() throws IOException { try { c = read(); } catch (IOException e) { return null; }if (c == NEW_LINE) return "";
              if (c == EOF) return null;int i = 0;charBuffer[i++] = (char) c;do { while (bufIndex < numBytesRead) { if (buf[bufIndex] != NEW_LINE && buf[bufIndex] != CARRIAGE_RETURN)
              { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; } else { if(buf[bufIndex] == CARRIAGE_RETURN) bufIndex++;bufIndex++;
                  return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } while (true); }
          public String ns() throws IOException { if (numBytesRead == EOF) return null;if (readJunk(SPACE) == EOF) return null;for (int i = 0; ; )
          { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { if (i == charBuffer.length) doubleCharBufferSize();charBuffer[i++] = (char) buf[bufIndex++]; }
          else {bufIndex++;return new String(charBuffer, 0, i); } }numBytesRead = stream.read(buf);if (numBytesRead == EOF) return new String(charBuffer, 0, i);bufIndex = 0; } }
          public double nd() throws IOException { String doubleVal = ns();if (doubleVal == null) throw new IOException();return Double.valueOf(doubleVal); }
          public void close() throws IOException {stream.close(); }
      }
  
      // region variables
      static InputReader sc = new InputReader();
      static OutputStream outputStream = System.out;
      static PrintWriter w = new PrintWriter(outputStream);
      // endregion
  
      static boolean flag = false;
  
      private static void initiateIO()
              throws IOException {if (System.getProperty("ONLINE_JUDGE") == null) {
          try { w = new PrintWriter("output.txt");
              sc = new InputReader(new FileInputStream("input.txt"));
          } catch (Exception e) { throw new IOException(); }} }
  
      public static void main(String[] args)
              throws IOException {
  
          initiateIO();
  
          int t = sc.ni();
          for(int i = 0; i < t; i++) {
  //            if(i == 136) flag = true;
              solve();
          }
  
          w.close();
  
      }
  
      static void solve() throws IOException {
          long n = sc.nl();
          long m = sc.nl();
          long M = m;
  
          if(flag) {
              w.println(n+""+m);
          }
  
          String nn = Integer.toBinaryString((int)n);
          StringBuilder sb = new StringBuilder(nn);
          nn = sb.reverse().toString();
          String mm = Integer.toBinaryString((int)m);
          sb = new StringBuilder(mm);
          mm = sb.reverse().toString();
  
  
          int[] nnn = new int[30];
          int[] mmm = new int[30];
  
          int idxn = 0;
          int idxm = 0;
          for(int i = 29; i >= 0; i--) {
              if(idxn < nn.length()) nnn[i] = Integer.parseInt(nn.charAt(idxn++)+"");
              if(idxm < mm.length()) mmm[i] = Integer.parseInt(mm.charAt(idxm++)+"");
          }
  
          boolean f = false;
          int idx = 29;
  
          for(int i = 0; i < 30; i++, idx--) {
              int currn = nnn[i];
              int currm = mmm[i];
              if(currn == currm && currm == 0) continue;
              if(currn == 1 && currm == 0) {
                  f = true;
                  m += Math.pow(2, idx);
                  continue;
              }
  
              if(currn == 0 && currm == 1 && f) {
                  m -= Math.pow(2, idx);
              }
          }
  
          if(m == M) {
              idx = 0;
              f = true;
              for(int i = 29; i >= 0; i--, idx++) {
                  int currm = mmm[i];
                  int currn = nnn[i];
                  if(currm == 0) {
                      m += Math.pow(2, idx);
                      break;
                  } else if(currn == 0){
                      m -= Math.pow(2, idx);
                  }
              }
          }
  //        w.println(m);
          w.println(n^m);
      }
  }