#include <bits/stdc++.h>
long long dp[35][2][2][2];
int T, l, r, lb[35], ub[35], digl, digr;
long long dfs(int d, bool flagl, bool flagr, bool eq) {
  if (!d) return 1;
  long long &res = dp[d][flagl][flagr][eq];
  if (~res) return res;
  res = 0;
  int lbound = flagl ? lb[d] : 0, rbound = flagr ? ub[d] : 1;
  for (int y = 0; y <= rbound; ++y)
    for (int x = lbound; x <= (eq ? y : 1); ++x)
      if (!(x & y)) {
        res += dfs(d - 1, flagl & (x == lbound), flagr & (y == rbound),
                   eq & (x == y));
      }
  return res;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    memset(dp, -1, sizeof(dp)), memset(lb, 0, sizeof(lb)),
        memset(ub, 0, sizeof(ub));
    scanf("%d %d", &l, &r);
    int flag = !l;
    digl = digr = 0;
    while (l) lb[++digl] = l & 1, l >>= 1;
    while (r) ub[++digr] = r & 1, r >>= 1;
    printf("%lld\n", dfs(digr, 1, 1, 1) * 2 - flag);
  }
  return 0;
}
