#include <bits/stdc++.h>
using namespace std;
const long long N = 300;
const long long M = 15;
const long long mod = 1e9 + 7;
const long long MOD = 998244353;
const long long P = 1336;
const long double eps = 0.000000001;
const long long inf = 1e9 + 7;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
long long dp[35][3][3][3][3];
long long comp(long long a, long long b) {
  if (a < b)
    return 0;
  else if (a == b)
    return 1;
  else
    return 2;
}
long long solve(long long ch, long long ch1) {
  vector<long long> a, b;
  if (ch == 0) a.push_back(0);
  if (ch1 == 0) b.push_back(0);
  while (ch) {
    long long x = ch % 2;
    ch /= 2;
    a.push_back(x);
  }
  while (ch1) {
    long long x = ch1 % 2;
    ch1 /= 2;
    b.push_back(x);
  }
  while (a.size() < b.size()) a.push_back(0);
  while (b.size() < a.size()) b.push_back(0);
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  long long n = a.size();
  for (long long l = 0; l < n; l++) {
    for (long long f1 = 0; f1 < 3; f1++) {
      for (long long f2 = 0; f2 < 3; f2++) {
        for (long long f3 = 0; f3 < 3; f3++) {
          for (long long f4 = 0; f4 < 3; f4++) {
            dp[l][f1][f2][f3][f4] = 0;
          }
        }
      }
    }
  }
  for (long long c1 = 0; c1 < 2; c1++) {
    for (long long c2 = 0; c2 < 2; c2++) {
      if (c1 && c2) continue;
      long long fl1 = comp(c1, a[0]);
      long long fl2 = comp(c1, b[0]);
      long long fl3 = comp(c2, a[0]);
      long long fl4 = comp(c2, b[0]);
      dp[0][fl1][fl2][fl3][fl4]++;
    }
  }
  for (long long l = 0; l < n - 1; l++) {
    for (long long f1 = 0; f1 < 3; f1++) {
      for (long long f2 = 0; f2 < 3; f2++) {
        for (long long f3 = 0; f3 < 3; f3++) {
          for (long long f4 = 0; f4 < 3; f4++) {
            for (long long c1 = 0; c1 < 2; c1++) {
              for (long long c2 = 0; c2 < 2; c2++) {
                if (c1 && c2) continue;
                long long fl1 = comp(c1, a[l + 1]);
                long long fl2 = comp(c1, b[l + 1]);
                long long fl3 = comp(c2, a[l + 1]);
                long long fl4 = comp(c2, b[l + 1]);
                if (f1 != 1) fl1 = f1;
                if (f2 != 1) fl2 = f2;
                if (f3 != 1) fl3 = f3;
                if (f4 != 1) fl4 = f4;
                dp[l + 1][fl1][fl2][fl3][fl4] += dp[l][f1][f2][f3][f4];
              }
            }
          }
        }
      }
    }
  }
  long long ans = 0;
  for (long long f1 = 0; f1 < 3; f1++) {
    for (long long f2 = 0; f2 < 3; f2++) {
      for (long long f3 = 0; f3 < 3; f3++) {
        for (long long f4 = 0; f4 < 3; f4++) {
          if (f1 == 0 || f2 == 2) continue;
          if (f3 == 0 || f4 == 2) continue;
          ans += dp[n - 1][f1][f2][f3][f4];
        }
      }
    }
  }
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  long long T;
  cin >> T;
  while (T--) {
    long long l, r;
    cin >> l >> r;
    cout << solve(l, r) << '\n';
  }
}
