#include <bits/stdc++.h>
using namespace std;
long long dp[35][2][2];
long long dfs(int l, int r, int x, int a, int b) {
  if (x == -1) return 1;
  if (dp[x][a][b] != -1) return dp[x][a][b];
  int na = 1, nb = 1;
  if (a) na = (l >> x) & 1;
  if (b) nb = (r >> x) & 1;
  dp[x][a][b] = 0;
  for (int i = 0; i <= na; i++) {
    for (int j = 0; j <= nb; j++) {
      if ((i & j) != 1)
        dp[x][a][b] += dfs(l, r, x - 1, a & (na == i), b & (nb == j));
    }
  }
  return dp[x][a][b];
}
long long solve(int l, int r) {
  if (l < 0 || r < 0) return 0;
  memset(dp, -1, sizeof(dp));
  return dfs(l, r, 30, 1, 1);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int l, r;
    cin >> l >> r;
    cout << solve(r, r) - 2 * solve(l - 1, r) + solve(l - 1, l - 1) << '\n';
  }
}
