#include <bits/stdc++.h>
using namespace std;
long long l, r;
int t, len, l1, l2, a[50], b[50];
long long dp[50][2][2];
inline long long dfs(int pos, int lim1, int lim2) {
  if (pos == len + 1) return 1;
  if (dp[pos][lim1][lim2] != -1) return dp[pos][lim1][lim2];
  int rex = lim1 ? a[len - pos + 1] : 1;
  int rey = lim2 ? b[len - pos + 1] : 1;
  long long ret = 0;
  for (int i = 0; i <= rex; i++) {
    for (int j = 0; j <= rey; j++) {
      if ((i & j) != 0) continue;
      ret += dfs(pos + 1, lim1 && i == rex, lim2 && j == rey);
    }
  }
  return dp[pos][lim1][lim2] = ret;
}
inline long long calc(int x, int y) {
  if (x < 0) return 0;
  memset(dp, -1, sizeof(dp));
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  l1 = 0;
  l2 = 0;
  len = 0;
  while (x) {
    a[++l1] = x % 2;
    x >>= 1;
  }
  while (y) {
    b[++l2] = y % 2;
    y >>= 1;
  }
  len = max(l1, l2);
  return dfs(1, 1, 1);
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld", &l, &r);
    printf("%lld\n", calc(r, r) - 2 * calc(l - 1, r) + calc(l - 1, l - 1));
  }
  return 0;
}
