#include <bits/stdc++.h>
using namespace std;
string aa;
vector<long long> VEC, VECC;
long long PRECOMP[203][203], THR = 200;
long long solve(long long number, long long range) {
  int siz, i, tot;
  long long ans, val;
  VEC.clear();
  VECC.clear();
  siz = 34;
  tot = 0;
  for (i = 0; i < siz; i++) {
    VECC.push_back((number & 1));
    VEC.push_back((range & 1));
    if (!(number & 1)) tot++;
    number >>= 1;
    range >>= 1;
  }
  reverse(VEC.begin(), VEC.end());
  ;
  reverse(VECC.begin(), VECC.end());
  ;
  ans = 0;
  for (i = 0; i < siz; i++) {
    if (VECC[i] == 0) tot--;
    if (VEC[i]) {
      val = (1LL << tot);
      ans += val;
    }
    if (VEC[i] && VECC[i]) break;
    if (i == siz - 1) ans++;
  }
  return ans;
}
long long check(long long l, long long r) {
  if (l <= THR && r <= THR) return PRECOMP[l][r];
  if (l == r) return 0;
  if (l == 0) return (2 * r + 1 + check(1, r));
  if (l % 2 == 0 && r % 2 == 0)
    return (3 * check(l / 2, r / 2) -
            2 * (solve(r + 1, r) - solve(r + 1, l - 1)));
  if (l % 2) return (check(l + 1, r) + 2 * (solve(l, r) - solve(l, l - 1)));
  if (r % 2) return (check(l, r - 1) + 2 * (solve(r, r) - solve(r, l - 1)));
}
int main() {
  long long i, j, k, l, n, m, tot, temp, curr, val, prev, a, b, c, t, ans, flag,
      x, y, siz, ii, jj, xx, yy, root, ind, len, q, xxx, yyy, d, ll, rr, r;
  for (i = 0; i <= THR; i++) {
    for (j = i; j <= THR; j++) {
      for (k = i; k <= j; k++) {
        for (l = i; l <= j; l++) {
          if ((k ^ l) == (k + l)) PRECOMP[i][j]++;
        }
      }
    }
  }
  cin >> q;
  while (q--) {
    cin >> l >> r;
    cout << check(l, r) << endl;
  }
}
