#include <bits/stdc++.h>
using namespace std;
long long T, l, r;
long long N = 31;
long long dp[35][2][2];
long long dfs(long long pos, long long la, long long lb, long long x,
              long long y) {
  if (pos == -1) return 1;
  if (dp[pos][la][lb] != -1) return dp[pos][la][lb];
  long long ret =
      dfs(pos - 1, la && (!((x >> pos) & 1)), lb && (!((y >> pos) & 1)), x, y);
  if (((x >> pos) & 1) || (!la))
    ret += dfs(pos - 1, la, lb && (!((y >> pos) & 1)), x, y);
  if (((y >> pos) & 1) || (!lb))
    ret += dfs(pos - 1, la && (!((x >> pos) & 1)), lb, x, y);
  dp[pos][la][lb] = ret;
  return ret;
}
long long solve(long long x, long long y) {
  if (x < 0 || y < 0) return 0;
  memset(dp, -1, sizeof(dp));
  return dfs(N, 1, 1, x, y);
}
int main() {
  cin >> T;
  while (T--) {
    cin >> l >> r;
    cout << solve(r, r) - solve(l - 1, r) - solve(r, l - 1) +
                solve(l - 1, l - 1)
         << endl;
  }
  return 0;
}
