#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &a) {
  T w = 1;
  a = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') w = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) a = (a * 10) + (ch - '0');
  a *= w;
}
template <typename T>
inline void ckmax(T &a, T b) {
  a = a > b ? a : b;
}
template <typename T>
inline void ckmin(T &a, T b) {
  a = a < b ? a : b;
}
const long long MAX = 1e5 + 10;
const long long N = 35;
long long dp[MAX][2][2];
long long sol(long long x, long long y) {
  if (x < 0 || y < 0) return 0;
  memset(dp, 0, sizeof(dp));
  dp[N][1][1] = 1;
  for (long long i = N - 1; ~i; --i) {
    for (long long j = 0; j < 2; ++j) {
      for (long long k = 0; k < 2; ++k) {
        long long nj = (j && !((x >> i) & 1ll)), nk = (k && !((y >> i) & 1ll));
        dp[i][nj][nk] += dp[i + 1][j][k];
        if (!j || ((x >> i) & 1ll)) dp[i][j][nk] += dp[i + 1][j][k];
        if (!k || ((y >> i) & 1ll)) dp[i][nj][k] += dp[i + 1][j][k];
      }
    }
  }
  return dp[0][0][0] + dp[0][1][0] + dp[0][0][1] + dp[0][1][1];
}
signed main() {
  long long cases;
  read(cases);
  while (cases--) {
    long long l, r;
    read(l);
    read(r);
    printf("%lld\n", sol(r, r) - 2ll * sol(l - 1, r) + sol(l - 1, l - 1));
  }
  return 0;
}
