#include <bits/stdc++.h>
using namespace std;
long long dp[50][2][2], a[50], b[50];
inline long long dfs(long long pos, long long l1, long long l2) {
  if (pos == -1) return 1;
  if (dp[pos][l1][l2] != -1) return dp[pos][l1][l2];
  long long up1 = l1 ? a[pos] : 1;
  long long up2 = l2 ? b[pos] : 1;
  long long res = 0;
  for (long long i = 0; i <= up1; i++)
    for (long long j = 0; j <= up2; j++)
      if (!(i & j)) res += dfs(pos - 1, l1 && i == up1, l2 && j == up2);
  dp[pos][l1][l2] = res;
  return res;
}
inline long long solve(long long l, long long r) {
  if (l == -1 || r == -1) return 0;
  long long pos1 = 0, pos2 = 0;
  memset(dp, -1, sizeof(dp));
  while (l || r) {
    a[pos1++] = (l & 1), b[pos2++] = (r & 1);
    l >>= 1, r >>= 1;
  }
  return dfs(max(pos1, pos2) - 1, 1, 1);
}
signed main() {
  long long T;
  cin >> T;
  while (T--) {
    long long l, r;
    cin >> l >> r;
    cout << solve(r, r) - solve(l - 1, r) - solve(r, l - 1) +
                solve(l - 1, l - 1)
         << endl;
  }
}
