#include <bits/stdc++.h>
using namespace std;
bool getBit(long long val, int idx) { return val & (1LL << (31 - idx)); }
long long k(long long val, long long r) {
  if (r == 0) {
    return 1;
  }
  int res = 0;
  for (int i = 0; i < 33; i++) {
    if (i != 32 && getBit(r, i) != 1) continue;
    bool valid = 1;
    for (int j = 0; j < i; j++)
      if (getBit(r, j) == 1 && getBit(val, j) == 1) valid = 0;
    if (!valid) continue;
    int cnt = 0;
    for (int j = i + 1; j < 32; j++)
      if (getBit(val, j) == 0) cnt++;
    res += (1 << cnt);
  }
  return res;
}
long long g(long long val, long long l, long long r) {
  r--;
  if (l > r) return 0;
  return k(val, r) - k(val, l - 1);
}
long long first(long long l, long long r) {
  if (l == r) {
    return (l == 0);
  }
  if (l == 0) {
    if (r == 0)
      return 0;
    else if (r == 1)
      return 1;
    else
      return 2 * r - 1 + first(1, r);
  }
  if (l % 2 == 0 && r % 2 == 0) {
    return 3 * first(l / 2, r / 2);
  } else if (l % 2 == 0 && r % 2 == 1) {
    return first(l, r - 1) + 2 * g(r - 1, l, r);
  } else if (l % 2 == 1 && r % 2 == 0) {
    return first(l + 1, r) + 2 * g(l, l, r);
  } else {
    long long val = 0;
    if ((l & (r - 1)) == 0) val = 1;
    return first(l + 1, r - 1) + 2 * g(l, l, r) + 2 * g(r - 1, l, r) - 2 * val;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int l, r;
    cin >> l >> r;
    r++;
    cout << first(l, r) << "\n";
  }
}
