#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
using namespace std;
int T, l, r;
long long dp[32][2][2][2];
int L[32], R[32];
inline long long dfs(int p, bool l1, bool l2, bool ok) {
  if (p == -1) return ok;
  if (~dp[p][l1][l2][ok]) return dp[p][l1][l2][ok];
  int up1 = 1, up2 = 1;
  if (l1) up1 = L[p];
  if (l2) up2 = R[p];
  long long ans = 0;
  for (int i = 0; i <= up1; ++i) {
    for (int j = 0; j <= up2; ++j) {
      ans += dfs(p - 1, l1 && (i == up1), l2 && (j == up2), ok && (!(i & j)));
    }
  }
  return dp[p][l1][l2][ok] = ans;
}
inline long long solve(int l, int r) {
  if (l < 0 || r < 0) return 0;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= 30; ++i) {
    L[i] = l % 2;
    l >>= 1;
    R[i] = r % 2;
    r >>= 1;
  }
  return dfs(30, 1, 1, 1);
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &l, &r);
    printf("%lld\n", solve(r, r) - 2 * solve(l - 1, r) + solve(l - 1, l - 1));
  }
  return 0;
}
