#include <bits/stdc++.h>
using namespace std;
const long long N = 32;
long long dp[32][2][2][2][2];
long long a1[N];
long long a2[N];
long long get(bool f1, bool f2, bool g1, bool g2, long long id) {
  if (id >= N) return 1;
  long long ce = dp[id][f1][f2][g1][g2];
  if (ce != -1) return ce;
  ce = 0;
  long long left1 = (f1 ? 0 : a1[id]), left2 = (f2 ? 1 : a2[id]),
            right1 = (g1 ? 0 : a1[id]), right2 = (g2 ? 1 : a2[id]);
  for (long long i = left1; i <= left2; i++) {
    for (long long j = right1; j <= right2; j++) {
      if (j == 1 && i == 1) continue;
      bool l1 = (f1 || (i > a1[id]));
      bool l2 = (f2 || (i < a2[id]));
      bool r1 = (g1 || (j > a1[id]));
      bool r2 = (g2 || (j < a2[id]));
      ce += get(l1, l2, r1, r2, id + 1);
    }
  }
  dp[id][f1][f2][g1][g2] = ce;
  return ce;
}
void solve() {
  long long l, r;
  cin >> l >> r;
  memset(dp, -1, sizeof(dp));
  for (long long j = N - 1; j >= 0; j--) {
    a1[j] = l % 2;
    l /= 2;
  }
  for (long long j = N - 1; j >= 0; j--) {
    a2[j] = r % 2;
    r /= 2;
  }
  long long ans = get(0, 0, 0, 0, 0);
  cout << ans << endl;
}
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
