#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int pwr(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}
const int B = 35;
long long dp[B + 1][2][2];
string bin(long long r) {
  string ans = "";
  for (int i = 0; i < B; i++) {
    ans += '0' + ((r >> i) & 1);
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
long long go(long long r1, long long r2) {
  string s1 = bin(r1);
  string s2 = bin(r2);
  memset(dp, 0, sizeof dp);
  dp[0][1][1] = 1;
  for (int i = 0; i < B; i++) {
    for (int k = 0; k < 2; k++) {
      for (int l = 0; l < 2; l++) {
        if (!dp[i][k][l]) continue;
        for (int b1 = 0; b1 < 2; b1++) {
          for (int b2 = 0; b2 < 2; b2++) {
            if (b1 && b2) continue;
            if (b1 && s1[i] == '0' && k) continue;
            if (b2 && s2[i] == '0' && l) continue;
            int ii = i + 1;
            int kk = k & (b1 == (s1[i] - '0'));
            int lk = l & (b2 == (s2[i] - '0'));
            dp[ii][kk][lk] += dp[i][k][l];
          }
        }
      }
    }
  }
  return dp[B][0][0];
}
long long brute(int l, int r) {
  int ans = 0;
  for (int i = 0; i < l; i++) {
    for (int j = 0; j < r; j++) {
      if (!(i & j)) ans++;
    }
  }
  return ans;
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long l, r;
    cin >> l >> r;
    long long ans = go(r + 1, r + 1) - 2 * go(l, r + 1) + go(l, l);
    cout << ans << '\n';
  }
  return 0;
}
