#include <bits/stdc++.h>
using namespace std;
long long a[100], b[100], lena, lenb;
long long dp[100][2][2];
long long dfs(long long pos, bool limita, bool limitb) {
  if (pos == 0) return 1;
  long long &ret = dp[pos][limita][limitb];
  if (~ret)
    return ret;
  else
    ret = 0;
  long long bdl = limita ? a[pos] : 1, bdr = limitb ? b[pos] : 1;
  for (long long i = 0; i <= bdl; ++i) {
    for (long long j = 0; j <= bdr; ++j) {
      if (!(i & j)) {
        ret += dfs(pos - 1, limita && (i == bdl), limitb && (j == bdr));
      }
    }
  }
  return ret;
}
long long calc(long long numa, long long numb) {
  if (numa < 0) return 0;
  memset(dp, -1, sizeof(dp));
  lena = lenb = 0;
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  while (numa) {
    a[++lena] = numa & 1;
    numa >>= 1;
  }
  while (numb) {
    b[++lenb] = numb & 1;
    numb >>= 1;
  }
  return dfs(lenb, 1, 1);
}
long long solve(long long x, long long y) {
  return calc(y, y) - 2 * (calc(x - 1, y)) + calc(x - 1, x - 1);
}
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long x, y;
    scanf("%lld%lld", &x, &y);
    cout << solve(x, y) << "\n";
  }
  return 0;
}
