#include <bits/stdc++.h>
using namespace std;
bool getBit(int u, int b) { return u & (1 << b); }
long long DP[30][2][2];
long long dp(int u, bool isARestricted, bool isBRestricted, int x, int y) {
  if (u == -1) {
    return 1;
  }
  if (DP[u][isARestricted][isBRestricted] != -1) {
    return DP[u][isARestricted][isBRestricted];
  }
  int limitA = isARestricted ? getBit(x, u) : 1;
  int limitB = isBRestricted ? getBit(y, u) : 1;
  long long ans = 0;
  for (int choiceA = 0; choiceA <= limitA; ++choiceA) {
    for (int choiceB = 0; choiceB <= limitB; ++choiceB) {
      if (choiceA == 1 and choiceB == 1) {
        continue;
      }
      ans += dp(u - 1, isARestricted and choiceA == limitA,
                isBRestricted and choiceB == limitB, x, y);
    }
  }
  return DP[u][isARestricted][isBRestricted] = ans;
}
long long f(int x, int y) {
  if (x < 0 or y < 0) {
    return 0;
  }
  memset(DP, -1, sizeof DP);
  return dp(29, true, true, x, y);
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int l, r;
    cin >> l >> r;
    cout << f(r, r) - 2 * f(r, l - 1) + f(l - 1, l - 1) << endl;
  }
}
