#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1.0);
long long l, r;
long long dp[35][2][2][2][2];
long long go(long long idx, long long sma, long long smb, long long bga,
             long long bgb) {
  if (idx == -1) {
    return 1;
  }
  if (dp[idx][sma][smb][bga][bgb] != -1) {
    return dp[idx][sma][smb][bga][bgb];
  }
  long long mina = (bga == 0 ? ((l >> idx) & 1) : 0);
  long long maxa = (sma == 0 ? ((r >> idx) & 1) : 1);
  long long minb = (bgb == 0 ? ((l >> idx) & 1) : 0);
  long long maxb = (smb == 0 ? ((r >> idx) & 1) : 1);
  long long ans = 0;
  for (long long j = mina; j < maxa + 1; ++j) {
    for (long long k = minb; k < maxb + 1; ++k) {
      if (j == k and j == 1)
        ;
      else {
        ans += go(idx - 1, sma | (j < maxa), smb | (k < maxb), bga | (j > mina),
                  bgb | (k > minb));
      }
    }
  }
  return dp[idx][sma][smb][bga][bgb] = ans;
}
void solve() {
  cin >> l >> r;
  memset(dp, -1, sizeof dp);
  cout << go(31, 0, 0, 0, 0) << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
