#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
const int OO = 1e9 + 10;
const int MOD = 1e9 + 7;
string s;
vector<int> v[2];
long long dp[40][3][3][3][3];
long long solve(int i, int smaller1, int bigger1, int smaller2, int bigger2) {
  if (i == v[0].size()) return 1;
  long long &ret = dp[i][smaller1][bigger1][smaller2][bigger2];
  if (ret != -1) return ret;
  ret = 0;
  int from1, to1, from2, to2;
  if (smaller1) {
    from1 = 0;
  } else {
    from1 = v[0][i];
  }
  if (smaller2) {
    from2 = 0;
  } else {
    from2 = v[0][i];
  }
  if (bigger1) {
    to1 = 1;
  } else {
    to1 = v[1][i];
  }
  if (bigger2) {
    to2 = 1;
  } else {
    to2 = v[1][i];
  }
  for (int k1 = from1; k1 <= to1; ++k1) {
    for (int k2 = from2; k2 <= to2; ++k2) {
      if (k1 + k2 == 2) continue;
      int torefsmaller1 = smaller1 | (k1 > v[0][i]);
      int torefsmaller2 = smaller2 | (k2 > v[0][i]);
      int torefbigger1 = bigger1 | (k1 < v[1][i]);
      int torefbigger2 = bigger2 | (k2 < v[1][i]);
      ret += solve(i + 1, torefsmaller1, torefbigger1, torefsmaller2,
                   torefbigger2);
    }
  }
  return ret;
}
void convert(int i, int number) {
  while (number) {
    v[i].push_back(number % 2);
    number /= 2;
  }
  while (v[i].size() <= 30) v[i].push_back(0);
  reverse(v[i].begin(), v[i].end());
}
int main() {
  int tc;
  cin >> tc;
  while (tc-- > 0) {
    v[0].clear();
    v[1].clear();
    int l, r;
    cin >> l >> r;
    convert(0, l);
    convert(1, r);
    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0, 0, 0, 0) << endl;
  }
}
