#include <bits/stdc++.h>
using namespace std;
const long long ff = 1e6 + 15;
const long long M = 1e9 + 7;
const long long inf = 1e18 + 1;
long long a[34], b[34], tp, tp1, tp2, ans;
long long f[33][2][2];
long long dfs(long long now, long long top1, long long top2) {
  if (now > tp) return 1;
  if (f[now][top1][top2]) return f[now][top1][top2];
  long long res = 0;
  long long lima = a[now], limb = b[now];
  res += dfs(now + 1, top1 & (lima == 0), top2 & (limb == 0));
  if (lima == 1 || !top1)
    res += dfs(now + 1, top1 & (lima == 1), top2 & (limb == 0));
  if (limb == 1 || !top2)
    res += dfs(now + 1, top1 & (lima == 0), top2 & (limb == 1));
  f[now][top1][top2] = res;
  return res;
}
long long calc(long long mx1, long long mx2) {
  if (mx1 < 0 || mx2 < 0) return 0;
  memset(f, 0, sizeof(f));
  tp1 = tp2 = 0;
  long long now = mx1;
  while (now > 0) a[++tp1] = now % 2, now >>= 1;
  now = mx2;
  while (now > 0) b[++tp2] = now % 2, now >>= 1;
  tp = max(tp1, tp2);
  while (tp1 < tp) a[++tp1] = 0;
  while (tp2 < tp) b[++tp2] = 0;
  reverse(a + 1, a + 1 + tp1);
  reverse(b + 1, b + 1 + tp2);
  long long ress = dfs(1, 1, 1);
  return ress;
}
void init() {
  memset(f, 0, sizeof(f));
  long long l, r;
  scanf("%lld %lld", &l, &r);
  cout << calc(r, r) - 2 * calc(l - 1, r) + calc(l - 1, l - 1) << "\n";
}
signed main() {
  long long Ts = 1;
  cin >> Ts;
  while (Ts--) init();
}
