#include <bits/stdc++.h>
using namespace std;
int a[45], b[45];
long long dp[45][2][2];
long long dfs(int pos, int p1, int p2) {
  if (pos == -1) return 1;
  if (dp[pos][p1][p2] != -1) return dp[pos][p1][p2];
  long long res = 0, up1 = p1 ? 1 : a[pos], up2 = p2 ? 1 : b[pos];
  for (int i = 0; i <= up1; i++)
    for (int j = 0; j <= up2; j++)
      if (i + j == 2)
        continue;
      else
        res += dfs(pos - 1, p1 | (i < a[pos]), p2 | (j < b[pos]));
  dp[pos][p1][p2] = res;
  return res;
}
long long calc(int m, int n) {
  for (int i = 30; i >= 0; i--) a[i] = (m >> i) & 1, b[i] = (n >> i) & 1;
  memset(dp, -1, sizeof(dp));
  return dfs(30, 0, 0);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int t, l, r;
  cin >> t;
  while (t--) {
    cin >> l >> r;
    long long ans = calc(r, r);
    if (l) ans += -2 * calc(r, l - 1) + calc(l - 1, l - 1);
    cout << ans << endl;
  }
  return 0;
}
