#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &x, T y) {
  x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
  x = x < y ? x : y;
}
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
const int N = 40;
int a[N], b[N];
long long f[N][2][2];
long long solve(int n, int m) {
  if (n < 0 || m < 0) return 0;
  int l1 = 0, l2 = 0;
  memset(a, 0, sizeof(a)), memset(b, 0, sizeof(b));
  while (n) a[++l1] = n & 1, n >>= 1;
  while (m) b[++l2] = m & 1, m >>= 1;
  memset(f, 0, sizeof(f));
  int tn = max(l1, l2);
  f[tn + 1][1][1] = 1;
  for (int i = tn + 1; i > 1; i--)
    for (int j : {0, 1})
      for (int k : {0, 1}) {
        if (!f[i][j][k]) continue;
        int tx = j ? a[i - 1] : 1, ty = k ? b[i - 1] : 1;
        for (int x = 0; x <= tx; x++)
          for (int y = 0; y <= ty; y++) {
            if (x && y) continue;
            f[i - 1][j && x == tx][k && y == ty] += f[i][j][k];
          }
      }
  return f[1][0][0] + f[1][0][1] + f[1][1][0] + f[1][1][1];
}
int main() {
  int T;
  read(T);
  while (T--) {
    int l, r;
    read(l), read(r);
    cout << solve(r, r) - solve(l - 1, r) - solve(r, l - 1) +
                solve(l - 1, l - 1)
         << '\n';
  }
  return 0;
}
