#include <bits/stdc++.h>
using namespace std;
string s, p;
long long l, r, dp[32][3][3][3][3];
string ts(long long x) {
  string ans;
  while (x) {
    ans.push_back('0' + x % 2);
    x /= 2;
  }
  while (ans.size() != 31) ans.push_back('0');
  reverse(ans.begin(), ans.end());
  return ans;
}
long long f(int x, int a, int b, int c, int d) {
  if (x == s.size()) return 1;
  if (dp[x][a][b][c][d] != -1) return dp[x][a][b][c][d];
  long long ans = 0;
  for (int i = 0; i <= 1; i++) {
    for (int j = 0; j <= 1; j++) {
      if (i == j && i) continue;
      char aa = i + '0', bb = j + '0';
      if (a && s[x] > aa) continue;
      if (b && p[x] < aa) continue;
      if (c && s[x] > bb) continue;
      if (d && p[x] < bb) continue;
      int na = (a & (s[x] == aa));
      int nb = (b & (p[x] == aa));
      int nc = (c & (s[x] == bb));
      int nd = (d & (p[x] == bb));
      ans += f(x + 1, na, nb, nc, nd);
    }
  }
  return dp[x][a][b][c][d] = ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    memset(dp, -1, sizeof dp);
    cin >> l >> r;
    s = ts(l), p = ts(r);
    cout << f(0, 1, 1, 1, 1) << endl;
  }
}
