#include <bits/stdc++.h>
bool debug = false;
long long int dp[40][2][2];
int cnt(int n) {
  int ans = 0;
  while (n) {
    ans++;
    n >>= 1;
  }
  return ans;
}
long long int val(int a, int b, int pos, bool af, bool bf) {
  if (pos == 0)
    return 1;
  else {
    int tot = cnt(pos);
    if (dp[tot][af][bf] >= 0) return dp[tot][af][bf];
    long long int ans = 0;
    if (af) {
      if (a & pos) {
        if (bf) {
          if (b & pos)
            ans = val(a, b, pos >> 1, true, false) +
                  val(a, b, pos >> 1, false, false) +
                  val(a, b, pos >> 1, false, true);
          else
            ans = val(a, b, pos >> 1, true, true) +
                  val(a, b, pos >> 1, false, true);
        } else
          ans = val(a, b, pos >> 1, true, false) +
                (val(a, b, pos >> 1, false, false) << 1);
      } else {
        if (bf) {
          if (b & pos)
            ans = val(a, b, pos >> 1, true, true) +
                  val(a, b, pos >> 1, true, false);
          else
            ans = val(a, b, pos >> 1, true, true);
        } else
          ans = val(a, b, pos >> 1, true, false) << 1;
      }
    } else {
      if (bf) {
        if (b & pos)
          ans = val(a, b, pos >> 1, false, true) +
                (val(a, b, pos >> 1, false, false) << 1);
        else
          ans = val(a, b, pos >> 1, false, true) << 1;
      } else
        ans = val(a, b, pos >> 1, false, false) * 3;
    }
    return dp[tot][af][bf] = ans;
  }
}
void solve() {
  int l, r;
  long long int ans = 0;
  scanf("%d%d", &l, &r);
  for (int i = 0; i < 40; i++)
    dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = -1;
  if (l == 0) {
    printf("%lld\n", val(r, r, 1 << 30, true, true));
    return;
  } else
    l--;
  if (debug) printf("rr=%lld\n", val(r, r, 1 << 30, true, true));
  ans += val(r, r, 1 << 30, true, true);
  for (int i = 0; i < 40; i++)
    dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = -1;
  if (debug) printf("rl=%lld\n", val(r, l, 1 << 30, true, true));
  ans -= val(r, l, 1 << 30, true, true) << 1;
  for (int i = 0; i < 40; i++)
    dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = -1;
  if (debug) printf("ll=%lld\n", val(l, l, 1 << 30, true, true));
  ans += val(l, l, 1 << 30, true, true);
  printf("%lld\n", ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
