#include <bits/stdc++.h>
using namespace std;
int l, r;
long long dp[30][2][2][2];
long long solve(int ind, bool isl, bool isr, bool is) {
  if (ind == -1) {
    return 1;
  }
  long long &p = dp[ind][isl][isr][is];
  if (p != -1) {
    return p;
  }
  p = 0;
  for (int ca = 0; ca < 2; ca++) {
    for (int cb = 0; cb < 2; cb++) {
      if (is && (ca > cb)) continue;
      if (ca && cb) continue;
      if (isl && (ca < ((l >> ind) & 1))) {
        continue;
      }
      if (isr && (cb > ((r >> ind) & 1))) {
        continue;
      }
      p += solve(ind - 1, isl && (ca == ((l >> ind) & 1)),
                 isr && (cb == ((r >> ind) & 1)), bool(is && (ca == cb)));
    }
  }
  return p;
}
void test_case() {
  memset(dp, -1, sizeof(dp));
  cin >> l >> r;
  cout << (2 * solve(29, 1, 1, 1)) - !l << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tcase;
  cin >> tcase;
  while (tcase--) {
    test_case();
  }
  return 0;
}
