#include <bits/stdc++.h>
using namespace std;
const int N = 31;
long long dp[N][2][2][2][2][2];
int a[N], b[N];
long long calc(int pos, int rem, bool ok1, bool ok2, bool ok3, bool ok4) {
  if (pos > 30) {
    return (!ok1) && (!ok2) && (!ok3) && (!ok4) && (!rem);
  }
  long long &res = dp[pos][rem][ok1][ok2][ok3][ok4];
  if (res != -1) return res;
  res = 0;
  for (int i = 0; i <= 1; ++i) {
    for (int j = 0; j <= 1; ++j) {
      int k = i + j + rem;
      if ((i ^ j) != (k >= 2 ? k - 2 : k)) continue;
      bool newOk1 = ((ok1 && i == a[pos]) || i > a[pos]);
      bool newOk2 = ((ok2 && i == b[pos]) || i < b[pos]);
      bool newOk3 = ((ok3 && j == a[pos]) || j > a[pos]);
      bool newOk4 = ((ok4 && j == b[pos]) || j < b[pos]);
      res += calc(pos + 1, (k >= 2), newOk1, newOk2, newOk3, newOk4);
    }
  }
  return res;
}
void solve() {
  int l, r;
  cin >> l >> r;
  for (int i = 0; i <= 30; ++i) {
    a[i] = (1 & ((r) >> (i)));
    b[i] = (1 & ((l) >> (i)));
  }
  memset(dp, -1, sizeof(dp));
  cout << calc(0, 0, 0, 0, 0, 0) << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int numTest;
  cin >> numTest;
  while (numTest--) solve();
  return 0;
}
