#include <bits/stdc++.h>
using namespace std;
const int maxn = 35;
int T, l, r;
int a[maxn], b[maxn];
long long dp[35][2][2];
long long dfs(int wz, int lim1, int lim2) {
  if (wz == -1) return 1;
  long long &ret = dp[wz][lim1][lim2];
  if (ret != -1) return ret;
  ret = 0;
  int mx1 = lim1 ? a[wz] : 1;
  int mx2 = lim2 ? b[wz] : 1;
  for (int i = 0; i <= mx1; i++)
    for (int j = 0; j <= mx2; j++) {
      if (i & j) continue;
      ret += dfs(wz - 1, lim1 && (i == mx1), lim2 && (j == mx2));
    }
  return ret;
}
long long get(int l, int r) {
  if (l == -1 || r == -1) return 0;
  memset(dp, -1, sizeof(dp));
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  int p1 = 0, p2 = 0;
  while (l) {
    a[p1++] = l & 1;
    l /= 2;
  }
  while (r) {
    b[p2++] = r & 1;
    r /= 2;
  }
  return dfs(31, 1, 1);
}
signed main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &l, &r);
    long long ans = get(r, r) - get(l - 1, r) * 2 + get(l - 1, l - 1);
    printf("%lld\n", ans);
  }
}
