#include <bits/stdc++.h>
using namespace std;
vector<int> L, R;
long long dp[32][2][2][2][2];
long long call(int pos, int bigA, int smallA, int bigB, int smallB) {
  if (pos == (int)R.size()) return 1;
  long long &res = dp[pos][bigA][smallA][bigB][smallB];
  if (~res) return res;
  res = 0;
  int startA = bigA ? 0 : L[pos];
  int stopA = smallA ? 1 : R[pos];
  int startB = bigB ? 0 : L[pos];
  int stopB = smallB ? 1 : R[pos];
  for (int i = startA; i <= stopA; i++) {
    for (int j = startB; j <= stopB; j++) {
      if (i & j) continue;
      res += call(pos + 1, bigA | i > L[pos],
                  smallA | i<R[pos], bigB | j> L[pos], smallB | j < R[pos]);
    }
  }
  return res;
}
long long solve(long long a, long long b) {
  L.clear();
  R.clear();
  while (b > 0) {
    R.push_back(b % 2);
    b /= 2;
  }
  while (a > 0) {
    L.push_back(a % 2);
    a /= 2;
  }
  while (L.size() < R.size()) L.push_back(0);
  reverse(L.begin(), L.end());
  reverse(R.begin(), R.end());
  memset(dp, -1, sizeof(dp));
  return call(0, 0, 0, 0, 0);
}
void syntex_error() {
  long long l, r;
  cin >> l >> r;
  cout << solve(l, r) << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long TC = 1LL, cs = 1LL;
  cin >> TC;
  while (TC--) {
    syntex_error();
  }
  return 0;
}
