#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
long long dp[33][2][2][2][2];
long long l, r;
vector<long long> numl, numr;
long long calc(long long pos, long long f1, long long f2, long long f3,
               long long f4) {
  if (pos == numr.size()) {
    return 1;
  }
  if (dp[pos][f1][f2][f3][f4] != -1) return dp[pos][f1][f2][f3][f4];
  dp[pos][f1][f2][f3][f4] = 0;
  long long l1 = (f1 ? 0 : numl[pos]);
  long long r1 = (f2 ? 1 : numr[pos]);
  long long l2 = (f3 ? 0 : numl[pos]);
  long long r2 = (f4 ? 1 : numr[pos]);
  for (long long x = l1; x <= r1; x++) {
    for (long long y = l2; y <= r2; y++) {
      if (x & y) continue;
      long long _f1 = (f1 | x > numl[pos]);
      long long _f2 = (f2 | x < numr[pos]);
      long long _f3 = (f3 | y > numl[pos]);
      long long _f4 = (f4 | y < numr[pos]);
      dp[pos][f1][f2][f3][f4] += calc(pos + 1, _f1, _f2, _f3, _f4);
    }
  }
  return dp[pos][f1][f2][f3][f4];
}
void solve() {
  cin >> l >> r;
  memset(dp, -1, sizeof(dp));
  numr.clear();
  numl.clear();
  for (long long i = 32; i >= 0; i--) {
    if ((1LL << i) & r)
      numr.push_back(1);
    else
      numr.push_back(0);
    if ((1LL << i) & l)
      numl.push_back(1);
    else
      numl.push_back(0);
  }
  cout << calc(0, 0, 0, 0, 0) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  for (long long i = 0; i < t; i++) {
    solve();
  }
}
