#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 2e5 + 5;
const long long mod = 1e9 + 7;
int T, l, r, a[40], b[40];
long long dp[40][2][2];
long long dfs(int pos, bool limit1, bool limit2) {
  if (pos == -1) return 1ll;
  if (~dp[pos][limit1][limit2]) return dp[pos][limit1][limit2];
  int up1 = limit1 ? a[pos] : 1;
  int up2 = limit2 ? b[pos] : 1;
  long long temp = 0;
  for (int i = 0; i <= up1; i++) {
    for (int j = 0; j <= up2; j++) {
      if ((i & j) == 0)
        temp += dfs(pos - 1, limit1 && a[pos] == i, limit2 && b[pos] == j);
    }
  }
  return dp[pos][limit1][limit2] = temp;
}
long long solve(int l, int r) {
  if (l < 0 || r < 0) return 0;
  memset(dp, -1, sizeof(dp));
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  int pos1 = 0, pos2 = 0;
  while (l) a[pos1++] = l & 1, l >>= 1;
  while (r) b[pos2++] = r & 1, r >>= 1;
  return dfs((pos1 - 1) > (pos2 - 1) ? (pos1 - 1) : (pos2 - 1), 1, 1);
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &l, &r);
    printf("%lld\n", solve(r, r) + solve(l - 1, l - 1) - 2 * solve(l - 1, r));
  }
  return 0;
}
