#include <bits/stdc++.h>
using namespace std;
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
long long inf = 1e18, MOD = 1e9 + 7;
long long compute(int l, int r, int rsb) {
  vector<vector<long long>> dp(rsb + 1, vector<long long>(4, 0));
  dp[rsb][0] = 1;
  for (int bit = rsb - 1; bit >= 0; bit--) {
    int val = (1 << bit);
    int a = 0, b = 0;
    if (l & val) a = 1;
    if (r & val) b = 1;
    dp[bit][3] = 3 * dp[bit + 1][3] + (b == 1 ? 2 * dp[bit + 1][2] : 0) +
                 (a == 0 ? dp[bit + 1][1] : 0) +
                 ((a == 0 && b == 1) ? dp[bit + 1][0] : 0);
    dp[bit][2] = (b == 1 ? dp[bit + 1][2] : 2 * dp[bit + 1][2]) +
                 ((a == 0 && b == 0) ? dp[bit + 1][0] : 0);
    dp[bit][1] = (a == 1 ? dp[bit + 1][1] : 2 * dp[bit + 1][1]) +
                 (b == 1 ? dp[bit + 1][0] : 0);
    dp[bit][0] = ((a == 1 && b == 1) ? 0 : dp[bit + 1][0]);
  }
  long long ret = 0;
  for (int i = 0; i < 4; i++) ret += dp[0][i];
  return ret;
}
void solve() {
  int l, r;
  cin >> l >> r;
  long long ans = 0;
  if (l == 0) ans = 2 * (r + 1) - 1, l++;
  if (r == 0) {
    cout << ans << endl;
    return;
  }
  int lmsb = log2(l), rmsb = log2(r);
  if (lmsb != rmsb) {
    ans += 2 * compute(l, r, rmsb);
    for (int bit = rmsb; bit >= lmsb + 2; bit--) {
      ans += 2 * compute(l, (1 << bit) - 1, bit - 1);
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int ti = 1;
  cin >> ti;
  for (int i = 1; i <= ti; i++) {
    solve();
  }
  return 0;
}
