#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int inf = INT_MAX;
const long long inff = 1e18;
const long long mod = 1e9 + 7;
long long t;
long long l, r;
vector<pair<long long, long long> > v;
long long p2[50], p3[50];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  p2[0] = 1, p3[0] = 1;
  for (long long i = 1; i <= 35; i++)
    p2[i] = p2[i - 1] * 2, p3[i] = p3[i - 1] * 3;
  cin >> t;
  while (t--) {
    v.clear();
    cin >> l >> r;
    long long ok = 0;
    if (l == 0 && r == 0) {
      cout << 1 << '\n';
      continue;
    }
    if (l == 0) l++, ok = 1;
    long long high1 = 0, high2 = 0;
    for (long long i = 35; i >= 0; i--) {
      if ((r & (1ll << i))) {
        high1 = max(high1, i);
      }
      if ((l & (1ll << i))) {
        high2 = max(high2, i);
      }
    }
    if (high1 == high2) {
      if (ok)
        cout << 2 * r + 1 << '\n';
      else
        cout << 0 << '\n';
    } else {
      long long now = 0;
      for (long long i = high2; i >= 0; i--) {
        if (i == high2)
          ;
        else if (l & (1ll << i))
          ;
        else
          v.push_back(make_pair((now & l) ^ (1ll << i), i));
        now |= (1ll << i);
      }
      v.push_back(make_pair(l, 0));
      now = 0;
      for (long long i = high1; i >= 0; i--) {
        if (i == high1)
          ;
        else if (r & (1ll << i))
          v.push_back(make_pair((now & r), i));
        else
          ;
        now |= (1ll << i);
      }
      v.push_back(make_pair(r, 0));
      for (long long i = high2 + 1; i <= high1 - 1; i++) {
        v.push_back(make_pair(1ll << i, i));
      }
      long long ans = 0, N = ((long long)(v).size());
      for (long long i = 0; i < N; i++) {
        for (long long j = 0; j < i; j++) {
          now = 0;
          if ((v[i].first & v[j].first) != 0) continue;
          if (v[i].second < v[j].second) {
            for (long long k = v[i].second; k < v[j].second; k++)
              if ((((1ll << k) & v[i].first)) == 0) now++;
            ans += (p2[now] * p3[v[i].second]);
          } else {
            for (long long k = v[j].second; k < v[i].second; k++)
              if ((((1ll << k) & v[j].first)) == 0) now++;
            ans += (p2[now] * p3[v[j].second]);
          }
        }
      }
      ans *= 2;
      if (ok == 1) ans += r * 2 + 1;
      cout << ans << '\n';
    }
  }
}
