#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long f = 1, ans = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return f * ans;
}
const long long MAXN = 30;
long long T, a, b, A[MAXN], B[MAXN], len, f[MAXN][2][2];
long long dfs(long long ps, long long lim1, long long lim2) {
  if (ps == -1) return 1;
  if (f[ps][lim1][lim2] != -1) return f[ps][lim1][lim2];
  long long e1 = (lim1 == 1) ? A[ps] : 1, e2 = (lim2 == 1) ? B[ps] : 1, res = 0;
  for (long long i = 0; i <= e1; i++)
    for (long long j = 0; j <= e2; j++) {
      if (i == 1 && j == 1) continue;
      res += dfs(ps - 1, lim1 && (i == A[ps]), lim2 && (j == B[ps]));
    }
  return f[ps][lim1][lim2] = res;
}
long long calc(long long x, long long y) {
  if (x < 0 || y < 0) return 0;
  memset(f, -1, sizeof(f));
  len = 29;
  for (long long i = 29; i >= 0; i--) A[i] = (x & (1 << i)) ? 1 : 0;
  for (long long i = 29; i >= 0; i--) B[i] = (y & (1 << i)) ? 1 : 0;
  return dfs(29, 1, 1);
}
signed main() {
  T = read();
  while (T--) {
    long long l = read(), r = read();
    printf("%lld\n", calc(r, r) + calc(l - 1, l - 1) - 2 * calc(l - 1, r));
  }
  return 0;
}
