#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 10;
ll f[40][2][2];
ll dp(int a, int b) {
  vector<int> na, nb;
  while (a) {
    na.push_back(a % 2);
    a /= 2;
  }
  while (b) {
    nb.push_back(b % 2);
    b /= 2;
  }
  int len = max(na.size(), nb.size());
  na.resize(len);
  nb.resize(len);
  memset(f, 0, sizeof(f));
  f[len][1][1] = 1;
  for (int i = len - 1; i >= 0; i--) {
    for (int a = 0; a <= 1; a++) {
      for (int b = 0; b <= 1; b++) {
        if (!a || na[i] == 1) f[i][a][b && nb[i] == 0] += f[i + 1][a][b];
        if (!b || nb[i] == 1) f[i][a && na[i] == 0][b] += f[i + 1][a][b];
        f[i][a && na[i] == 0][b && nb[i] == 0] += f[i + 1][a][b];
      }
    }
  }
  return f[0][0][0] + f[0][0][1] + f[0][1][0] + f[0][1][1];
}
int main() {
  ios::sync_with_stdio(false);
  int kase;
  cin >> kase;
  while (kase--) {
    int l, r;
    cin >> l >> r;
    cout << dp(r, r) + (l ? -2 * dp(l - 1, r) + dp(l - 1, l - 1) : 0) << endl;
  }
  return 0;
}
