#include <bits/stdc++.h>
using namespace std;
long long const N = 35;
long long dp[N][2][2];
long long get(long long l, long long r) {
  if (l < 0 || r < 0) return 0;
  memset(dp, 0, sizeof dp);
  dp[35][1][1] = 1;
  for (long long i = 34; i >= 0; i--) {
    for (long long j = 0; j <= 1; j++) {
      for (long long k = 0; k <= 1; k++) {
        long long x = (j && !((l >> i) & 1ll)), y = (k && !((r >> i) & 1ll));
        dp[i][x][y] += dp[i + 1][j][k];
        if (!j || ((l >> i) & 1ll)) dp[i][j][y] += dp[i + 1][j][k];
        if (!k || ((r >> i) & 1ll)) dp[i][x][k] += dp[i + 1][j][k];
      }
    }
  }
  return dp[0][0][0] + dp[0][1][0] + dp[0][1][1] + dp[0][0][1];
}
signed main() {
  long long T, l, r;
  scanf("%lld", &T);
  while (T--) {
    scanf("%lld%lld", &l, &r);
    long long ans = get(r, r) - get(l - 1, r) * 2 + get(l - 1, l - 1);
    printf("%lld\n", ans);
  }
  return 0;
}
