#include <bits/stdc++.h>
using namespace std;
int t, l, r, sz;
string sl, sr;
vector<string> gr, le;
void build_less(int num) {
  le.clear();
  sr = "";
  while (num) {
    sr += (char)(num % 2 + '0');
    num /= 2;
  }
  if (!r) sr = "0";
  reverse(sr.begin(), sr.end());
  sz = sr.length();
  le.resize(__builtin_popcount(r) + 1);
  for (int i = 0; i < le.size(); i++) le[i] = "";
  le[0] = sr;
  int turn = 1;
  for (int i = 0; i < sz; i++) {
    if (sr[i] == '1') {
      le[turn] += '0';
      ++turn;
    }
    for (int j = turn; j < le.size(); j++) le[j] += sr[i];
  }
}
void build_greater(int num) {
  gr.clear();
  sl = "";
  int szl = 0;
  while (num) {
    sl += (char)(num % 2 + '0');
    num /= 2;
  }
  if (!l) sl = "0";
  reverse(sl.begin(), sl.end());
  szl = sl.length();
  string zeros(sz - szl, '0');
  gr.resize(sz - __builtin_popcount(l) + 1);
  sl = zeros + sl;
  gr[0] = sl;
  for (int i = 1; i < gr.size(); i++) gr[i] = "";
  int turn = 1;
  for (int i = 0; i < sz; i++) {
    if (sl[i] == '0') {
      gr[turn] += '1';
      ++turn;
    }
    for (int j = turn; j < gr.size(); j++) gr[j] += sl[i];
  }
}
vector<string> valid;
long long p[34];
void combine_l_r() {
  set<string> val;
  valid.clear();
  map<string, bool> vl, vr;
  for (int i = 0; i < le.size(); i++) vr[le[i]] = 1;
  for (int i = 0; i < gr.size(); i++) vl[gr[i]] = 1;
  string tmp = "";
  for (int i = 0; i < le.size(); i++) {
    tmp = "";
    for (int j = 0; j < le[i].length(); j++) {
      tmp += le[i][j];
      if (vl[tmp]) {
        val.insert(le[i]);
        break;
      }
    }
  }
  for (int i = 0; i < gr.size(); i++) {
    tmp = "";
    for (int j = 0; j < gr[i].length(); j++) {
      tmp += gr[i][j];
      if (vr[tmp]) {
        val.insert(gr[i]);
        break;
      }
    }
  }
  for (auto x : val) valid.push_back(x);
}
bool can(string x, string y) {
  int mn = min(x.length(), y.length());
  for (int i = 0; i < mn; i++) {
    if (x[i] == '1' && y[i] == '1') return 0;
  }
  return 1;
}
long long comb(string x, string y) {
  long long mul = 1;
  if (x.length() < y.length()) swap(x, y);
  int st = y.length(), ed = x.length();
  for (int i = st; i < ed; i++) {
    if (x[i] == '0') mul *= 2;
  }
  return mul * (p[sz - x.length()]);
}
long long solve() {
  long long ret = 0;
  for (int i = 0; i < valid.size(); i++) {
    for (int j = 0; j < valid.size(); j++) {
      if (can(valid[i], valid[j])) {
        ret += comb(valid[i], valid[j]);
      }
    }
  }
  return ret;
}
int main() {
  long long mul = 1;
  for (int i = 0; i < 33; i++) {
    p[i] = mul;
    mul *= 3;
  }
  cin >> t;
  while (t--) {
    cin >> l >> r;
    build_less(r);
    build_greater(l);
    combine_l_r();
    printf("%lld\n", solve());
  }
}
