#include <bits/stdc++.h>
using namespace std;
long long dp[30][2][2];
int getMaxBit(int bit, int num, int ind) {
  if (bit) {
    return 1;
  }
  if (num & (1 << ind)) {
    return 1;
  } else {
    return 0;
  }
}
long long solveDp(int ind, int bit1, int bit2, int x, int y) {
  if (ind < 0) {
    return 1;
  }
  long long &ret = dp[ind][bit1][bit2];
  if (ret != -1) {
    return ret;
  }
  ret = 0;
  int st1 = getMaxBit(bit1, x, ind), st2 = getMaxBit(bit2, y, ind);
  for (int b1 = 0; b1 <= st1; ++b1) {
    for (int b2 = 0; b2 <= st2; ++b2) {
      if (b1 == 1 && b2 == 1) {
        continue;
      }
      int nbit1 = bit1, nbit2 = bit2;
      if (b1 < st1) {
        nbit1 |= 1;
      }
      if (b2 < st2) {
        nbit2 |= 1;
      }
      ret += solveDp(ind - 1, nbit1, nbit2, x, y);
    }
  }
  return ret;
}
long long solve3(int x, int y) {
  if (x < 0 || y < 0) {
    return 0;
  }
  memset(dp, -1, sizeof(dp));
  return solveDp(30 - 1, 0, 0, x, y);
}
void solve2() {
  int l, r;
  scanf("%d %d ", &l, &r);
  printf("%lld\n", solve3(r, r) - solve3(l - 1, r) * 2 + solve3(l - 1, l - 1));
}
void solve() {
  int t;
  scanf("%d ", &t);
  while (t--) {
    solve2();
  }
}
int main() {
  solve();
  return 0;
}
