#include <bits/stdc++.h>
using namespace std;
int g(int x, int n) {
  int ret = 0;
  int zeroes = 0;
  for (int i = 1; i <= n; i <<= 1) {
    if (n & i) {
      n ^= i;
      if (!(x & n)) ret += 1 << zeroes;
    }
    if (!(x & i)) zeroes++;
  }
  return ret;
}
long long f(int l, int r) {
  if (l == r) return 0;
  if (l == 0) return 2 * r - 1 + f(1, r);
  if (l % 2) return f(l + 1, r) + 2ll * (g(l, r) - g(l, l));
  if (r % 2) return f(l, r - 1) + 2ll * (g(r - 1, r) - g(r - 1, l));
  return 3ll * f(l / 2, r / 2);
}
int main() {
  int t, l, r;
  cin >> t;
  while (t--) {
    cin >> l >> r;
    cout << f(l, r + 1) << endl;
  }
  return 0;
}
