#include <bits/stdc++.h>
using namespace std;
int n, m;
void solve() {
  scanf("%d%d", &n, &m);
  if (n > m) {
    printf("0\n");
    return;
  }
  int ans = 0;
  m++;
  for (int k = 31; k >= 0 && n < m; k--) {
    if ((n >> k) & 1 && (m >> k) & 1) continue;
    if (m >> k & 1) {
      ans |= 1 << k;
      n |= 1 << k;
    }
  }
  printf("%d\n", ans);
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
