#include <bits/stdc++.h>
using namespace std;
long long mx(long long x) {
  long long res = 0;
  while (x) {
    x >>= 1;
    res++;
  }
  return res - 1;
}
int main() {
  int n;
  long long l, r;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> l >> r;
    long long res = 0;
    while (l != 0 || r != 0) {
      long long f = mx(l), s = mx(r);
      if (s > f) {
        if (r == (1ll << (s + 1)) - 1)
          res ^= r;
        else
          res ^= ((1ll << s) - 1);
        break;
      } else {
        res ^= (1ll << f);
        l ^= (1ll << f);
        r ^= (1ll << s);
      }
    }
    cout << res << endl;
  }
  return 0;
}
