#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 2e3 + 5;
const int mod = 1e9 + 7;
int lb[60], rb[60], tp = 0;
long long dp[60][2][2][2][2];
long long dps(int pos, int a1, int b1, int a2, int b2) {
  if (pos == -1)
    return 1;
  else if (dp[pos][a1][b1][a2][b2] != -1)
    return dp[pos][a1][b1][a2][b2];
  int begina = (a1 ? lb[pos] : 0), enda = (a2 ? rb[pos] : 1);
  int beginb = (b1 ? lb[pos] : 0), endb = (b2 ? rb[pos] : 1);
  long long &res = dp[pos][a1][b1][a2][b2];
  res = 0;
  for (int i = begina; i <= enda; ++i)
    for (int j = beginb; j <= endb; ++j) {
      if (i + j > 1) continue;
      res += dps(pos - 1, a1 && (i == lb[pos]), b1 && (j == lb[pos]),
                 a2 && (i == rb[pos]), b2 && (j == rb[pos]));
    }
  return res;
}
long long solve(int l, int r) {
  memset(dp, -1, sizeof(dp));
  memset(lb, 0, sizeof(lb));
  memset(lb, 0, sizeof(rb));
  tp = 0;
  while (l) {
    lb[tp++] = l & 1;
    l >>= 1;
  }
  tp = 0;
  while (r) {
    rb[tp++] = r & 1;
    r >>= 1;
  }
  return dps(tp - 1, 1, 1, 1, 1);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int l, r;
    cin >> l >> r;
    cout << solve(l, r) << "\n";
  }
  return 0;
}
