#include <bits/stdc++.h>
using namespace std;
inline int trans(int mask_pre, int chs, int l, int r) {
  int up = mask_pre >> 1, low = mask_pre & 1;
  if (up == 0 && low == 0) {
    return mask_pre;
  }
  if (up == 0 && low == 1) {
    if (chs < l)
      return -1;
    else if (chs == l)
      return mask_pre;
    else
      return 0;
  }
  if (up == 1 && low == 0) {
    if (chs > r)
      return -1;
    else if (chs == r)
      return mask_pre;
    else
      return 0;
  }
  if (up == 1 && low == 1) {
    if (chs < l || chs > r) return -1;
    if (chs == l && l == r) return mask_pre;
    if (chs == l) return 1;
    if (chs == r) return 2;
    return 0;
  }
  return -1;
}
int binl[50];
int binr[50];
int le_max;
long long dp[50][4][4];
inline void upload(int x, int* bin) {
  int len = 0;
  while (x) {
    bin[len++] = x & 1;
    x >>= 1;
  }
  le_max = max(le_max, len);
}
int T, l, r;
int dx[3] = {0, 1, 0};
int dy[3] = {0, 0, 1};
int main() {
  cin >> T;
  while (T--) {
    cin >> l >> r;
    le_max = 0;
    memset(binl, 0, sizeof(binl));
    memset(binr, 0, sizeof(binr));
    memset(dp, 0, sizeof(dp));
    upload(l, binl), upload(r, binr);
    dp[le_max][3][3] = 1;
    for (int i = le_max; i > 0; --i) {
      for (int maska = 0; maska < 4; ++maska) {
        for (int maskb = 0; maskb < 4; ++maskb) {
          for (int k = 0; k < 3; ++k) {
            int t1 = trans(maska, dx[k], binl[i - 1], binr[i - 1]);
            int t2 = trans(maskb, dy[k], binl[i - 1], binr[i - 1]);
            if (t1 == -1 || t2 == -1) continue;
            dp[i - 1][t1][t2] += dp[i][maska][maskb];
          }
        }
      }
    }
    long long ans = 0;
    for (int maska = 0; maska < 4; ++maska) {
      for (int maskb = 0; maskb < 4; ++maskb) {
        ans += dp[0][maska][maskb];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
