#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
void __Check(bool condition, const char* expression, int line) {
  if (!condition) {
    fprintf(stderr, "Check failed at line %d: %s\n", line, expression);
    exit(-1);
  }
}
template <class Collection, class Key>
bool ContainsKey(const Collection& collection, const Key& key) {
  return collection.find(key) != collection.end();
}
const int INF = 0x3F3F3F3F;
const long long INF64 = 0x3F3F3F3F3F3F3F3F;
const int INIT = -1;
int h(int x, int n) {
  int zeroes = 0;
  for (int i = 1; i < (n & (-n)); i <<= 1) {
    if (!(x & i)) {
      zeroes++;
    }
  }
  n -= (n & (-n));
  if (!(x & n)) {
    return (1 << zeroes);
  }
  return 0;
}
long long g(int x, int n) {
  if (n == 0) {
    return 0;
  }
  long long ret = h(x, n) + g(x, n - (n & (-n)));
  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 & 1) {
    return f(l + 1, r) + 2 * (g(l, r) - g(l, l));
  } else if (r & 1) {
    return f(l, r - 1) + 2 * (g(r - 1, r) - g(r - 1, l));
  } else {
    return 3 * f(l >> 1, r >> 1);
  }
}
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    int l, r;
    cin >> l >> r;
    cout << f(l, r + 1) << endl;
  }
}
