#include <bits/stdc++.h>
using namespace std;
long long memo[2][2][2][35];
string a, b;
long long dp(bool st, bool men, bool may, int pos) {
  if (pos == 31) return 1;
  long long &ret = memo[st][men][may][pos];
  if (ret != -1) return ret;
  long long ans = 0;
  if ((men || b[pos] == '1') && (may || a[pos] == '0'))
    ans += dp(1, men, may, pos + 1);
  if (st) ans += dp(1, (men || b[pos] == '1'), (may || a[pos] == '0'), pos + 1);
  if (may || a[pos] == '0') ans += dp(st, (men || b[pos] == '1'), may, pos + 1);
  return ret = ans;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    memset(memo, -1, sizeof memo);
    int x, y;
    cin >> x >> y;
    a = bitset<31>(x).to_string();
    b = bitset<31>(y).to_string();
    cout << 2 * dp(0, 0, 0, 0) - (x == 0) << endl;
  }
  return 0;
}
