#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
using namespace std;
const long long OO = 4e18;
const long double pi = 3.1415926535897932384626433832;
const int md = 1e9 + 7, oo = 2e9;
const int N = +100500;
int l[31], r[31], L, R;
void solve() {
  cin >> L >> R;
  int pL = L;
  if (!L && !R) {
    cout << 1 << endl;
    return;
  }
  long long f = 0;
  if (!L) {
    f = R + R + 1;
    ++L;
  }
  for (int i = 0; i < 31; ++i) l[30 - i] = 1 & (L >> i);
  for (int i = 0; i < 31; ++i) r[30 - i] = 1 & (R >> i);
  long long dp[32][2][2][2], ans = 0;
  memset(dp, 0, sizeof dp);
  dp[0][0][0][0] = 1;
  for (int i = 0; i < 32; ++i)
    for (int t = 0; t < 2; ++t)
      for (int d0 = 0; d0 < 2; ++d0)
        for (int d1 = 0; d1 < 2; ++d1) {
          if (i < 31)
            for (int p0 = 0; p0 < 2; ++p0)
              for (int p1 = 0; p1 < 2; ++p1) {
                if (p0 && p1) continue;
                if (!t && p0 > p1) continue;
                if (!d0 && p0 < l[i]) continue;
                if (!d1 && p1 > r[i]) continue;
                int nt = p0 < p1 ? 1 : t;
                int nd0 = p0 > l[i] ? 1 : d0;
                int nd1 = p1 < r[i] ? 1 : d1;
                dp[i + 1][nt][nd0][nd1] += dp[i][t][d0][d1];
              }
          if (i == 31) ans += dp[i][t][d0][d1];
        }
  ans *= 2;
  ans += f;
  cout << ans << endl;
}
int main() {
  ios_base ::sync_with_stdio(0);
  int tt;
  cin >> tt;
  while (tt--) solve();
}
