#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 3 * 100000 + 1;
  vector<int> v(n);
  v[0] = 0;
  for (int i = 1; i < n; i++) {
    v[i] = i ^ v[i - 1];
  }
  int a, b, t, ans;
  cin >> t;
  while (t--) {
    cin >> a >> b;
    if (v[a - 1] == b) {
      ans = a;
    } else if ((v[a - 1] ^ b) == a)
      ans = a + 2;
    else
      ans = a + 1;
    cout << ans << endl;
  }
  return 0;
}
