#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  long long* arr = new long long[3 * 100001];
  arr[0] = 0;
  for (int i = 1; i < 3 * 100001; i++) {
    arr[i] = arr[i - 1] ^ i;
  }
  while (t--) {
    long long minimumNonNegativeInteger, xorValue;
    cin >> minimumNonNegativeInteger >> xorValue;
    if (xorValue == arr[minimumNonNegativeInteger - 1]) {
      cout << minimumNonNegativeInteger << endl;
      continue;
    }
    long long requiredXor = xorValue ^ (arr[minimumNonNegativeInteger - 1]);
    if (requiredXor == minimumNonNegativeInteger) {
      cout << (minimumNonNegativeInteger + 2) << endl;
    } else {
      cout << (minimumNonNegativeInteger + 1) << endl;
    }
  }
}
