#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, n, m, i, z, x, j, sum;
  cin >> t;
  for (long long int q = 0; q < t; q++) {
    cin >> n >> m;
    vector<long long int> v;
    sum = 0;
    for (i = 31; i >= 0; i--) {
      z = (n >> i) & 1;
      x = ((m + 1) >> i) & 1;
      if (z == 1 && x == 1) {
        v.push_back(0);
      } else if (z == 0 && x == 0) {
        v.push_back(0);
      } else if (z == 0 && x == 1) {
        v.push_back(1);
        sum += pow(2, i);
      } else {
        v.push_back(0);
        j = i;
        break;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
