#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool f = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = 1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return f ? -x : x;
}
void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(long long x) {
  write(x);
  puts("");
}
long long f[40][2][2];
long long work(long long a, long long b) {
  if (a < 0 || b < 0) return 0;
  memset(f, 0, sizeof(f));
  f[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++) {
        bool ntj = (j && !((a >> i) & 1)), ntk = (k && !((b >> i) & 1));
        f[i][ntj][ntk] += f[i + 1][j][k];
        if (((a >> i) & 1) || !j) f[i][j][ntk] += f[i + 1][j][k];
        if (((b >> i) & 1) || !k) f[i][ntj][k] += f[i + 1][j][k];
      }
  }
  return f[0][0][0] + f[0][1][0] + f[0][0][1] + f[0][1][1];
}
signed main() {
  long long t = read();
  while (t--) {
    long long l = read(), r = read();
    writeln(work(r, r) - work(l - 1, r) * 2 + work(l - 1, l - 1));
  }
  return 0;
}
