#include <bits/stdc++.h>
using namespace std;
long long T, l, r, f[35][2][2][2][2];
long long dfs(long long pos, bool f1, bool f2, bool f3, bool f4) {
  if (pos == -1) return 1;
  if (f[pos][f1][f2][f3][f4] != -1) return f[pos][f1][f2][f3][f4];
  long long &t = f[pos][f1][f2][f3][f4];
  t = 0;
  long long d1 = f1 ? l >> pos & 1 : 0, d2 = f2 ? r >> pos & 1 : 1,
            d3 = f3 ? l >> pos & 1 : 0, d4 = f4 ? r >> pos & 1 : 1;
  for (long long x = d1; x <= d2; x++)
    for (long long y = d3; y <= d4; y++)
      if (!x || !y)
        t += dfs(pos - 1, f1 & (x == d1), f2 & (x == d2), f3 & (y == d3),
                 f4 & (y == d4));
  return t;
}
long long cal(long long l, long long r) {
  long long pos = 0;
  while (r - (r & (-r))) r -= (r & (-r));
  return dfs(log2(r + 1) + 1, 1, 1, 1, 1);
}
signed main() {
  scanf("%lld", &T);
  for (; T--;) {
    memset(f, -1, sizeof(f));
    scanf("%lld%lld", &l, &r);
    printf("%lld\n", cal(l, r));
  }
}
