#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(long long a) {
  write(a);
  puts("");
}
inline void wri(long long a) {
  write(a);
  putchar(' ');
}
long long t, l, r;
long long dp[35][2][2];
long long calc(long long x, long long y) {
  if (x < 0 || y < 0) return 0;
  memset(dp, 0, sizeof(dp));
  dp[32][1][1] = 1;
  for (long long i = 31; i >= 0; i--) {
    for (int j = 0; j <= 1; j++) {
      for (int k = 0; k <= 1; k++) {
        long long nextj = (j && !((x >> i) & 1));
        long long nextk = (k && !((y >> i) & 1));
        dp[i][nextj][nextk] += dp[i + 1][j][k];
        if (!j || ((x >> i) & 1)) dp[i][j][nextk] += dp[i + 1][j][k];
        if (!k || ((y >> i) & 1)) dp[i][nextj][k] += dp[i + 1][j][k];
      }
    }
  }
  return dp[0][0][0] + dp[0][0][1] + dp[0][1][0] + dp[0][1][1];
}
int main() {
  t = read();
  while (t--) {
    l = read(), r = read();
    writeln(calc(r, r) - 2 * calc(l - 1, r) + calc(l - 1, l - 1));
  }
  return 0;
}
