#include <bits/stdc++.h>
using namespace std;
long long l, r;
long long f[31][2][2];
vector<long long> a, b;
long long cnt_bit(long long x) {
  for (long long i = 31 + 1; i >= 0; --i)
    if (((x >> i) & 1) == 1) return i;
  return -1;
}
void reset() {
  for (long long i = 0; i < 31; ++i)
    for (long long j = 0; j <= 1; ++j)
      for (long long z = 0; z <= 1; ++z) f[i][j][z] = -1;
}
long long dpdigit(long long n, bool islessa, bool islessb, long long x,
                  long long y) {
  if (n < 0) return 1;
  long long &res = f[n][islessa][islessb];
  if (res != -1) return res;
  res = 0;
  long long ta = ((x >> n) & 1);
  long long tb = ((y >> n) & 1);
  a.clear();
  b.clear();
  if (islessa) {
    a.push_back(0);
    a.push_back(1);
  } else
    for (long long j = 0; j <= ta; ++j) a.push_back(j);
  if (islessb) {
    b.push_back(0);
    b.push_back(1);
  } else
    for (long long j = 0; j <= tb; ++j) b.push_back(j);
  for (long long u : a)
    for (long long v : b) {
      if (u != 1 || v != 1) {
        bool tama = islessa;
        bool tamb = islessb;
        if (u < ta) tama = 1;
        if (v < tb) tamb = 1;
        res += dpdigit(n - 1, tama, tamb, x, y);
      }
    }
  return res;
}
void queuery() {
  scanf("%lld%lld", &l, &r);
  reset();
  long long n;
  n = max(cnt_bit(r), cnt_bit(r));
  long long ans = dpdigit(n, 0, 0, r, r);
  if (l == 0) {
    printf("%lld\n", ans);
    return;
  }
  reset();
  n = max(cnt_bit(l - 1), cnt_bit(r));
  long long ta = dpdigit(n, 0, 0, l - 1, r);
  reset();
  long long tb = dpdigit(n, 0, 0, r, l - 1);
  reset();
  n = max(cnt_bit(l - 1), cnt_bit(l - 1));
  ans -= ta + tb;
  ans += dpdigit(n, 0, 0, l - 1, l - 1);
  printf("%lld\n", ans);
}
signed main() {
  long long t;
  scanf("%lld", &t);
  while (t--) queuery();
  return 0;
}
