#include <bits/stdc++.h>
using namespace std;
const int debug_ = 0;
const int maxn = 200000;
const long long inf = 998244353;
int unum[33], dnum[33];
long long dp[33][2][2][2];
long long dfs(int pos, int u, int e, int d) {
  if (pos == -1) {
    return 2 - e;
  }
  if (dp[pos][u][e][d] != -1) {
    return dp[pos][u][e][d];
  }
  long long& ret = dp[pos][u][e][d];
  ret = 0;
  for (int i = 0; i < 3; ++i) {
    int x = i / 2, y = i % 2;
    if (x == 1 && u && unum[pos] == 0) {
      continue;
    }
    if (y == 1 && e) {
      continue;
    }
    if (y == 0 && d && dnum[pos] == 1) {
      continue;
    }
    int nu = (u && x == unum[pos]);
    int ne = (e && y == x);
    int nd = (d && y == dnum[pos]);
    ret += dfs(pos - 1, nu, ne, nd);
  }
  return ret;
}
int main() {
  int tes, u, d;
  cin >> tes;
  while (tes--) {
    cin >> d >> u;
    for (int i = 0; i < 31; ++i) {
      dnum[i] = (d >> i) & 1;
      unum[i] = (u >> i) & 1;
    }
    memset(dp, -1, sizeof(dp));
    cout << dfs(32, 1, 1, 1) << endl;
  }
  return 0;
}
