#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    long long int n;
    long long int m;
    cin >> n >> m;
    long long int nBits[32];
    for (int i = 31; i >= 0; i--) {
      nBits[i] = ((n & (1 << i)) != 0);
    }
    long long int curr = 0;
    long long int res = 0;
    for (int i = 31; i >= 0; i--) {
      long long int bit = (curr ^ (nBits[i] << i));
      long long int val = (1LL << i);
      if ((bit + val) > (m + 1)) {
        curr = bit;
      } else {
        curr = (curr ^ ((1 - nBits[i]) << i));
        res = res ^ val;
      }
    }
    cout << res << endl;
  }
  return 0;
}
