#include <bits/stdc++.h>
using namespace std;
long long dp[44][2][2], arrx[44], arry[44];
long long dfs(int pos, int limitx, int limity) {
  if (dp[pos][limitx][limity] != -1) return dp[pos][limitx][limity];
  if (pos == 0) return 1;
  int upx = 1, upy = 1;
  long long ret = 0;
  if (limitx) upx = arrx[pos];
  if (limity) upy = arry[pos];
  for (int i = 0; i <= upx; ++i) {
    for (int j = 0; j <= upy; ++j) {
      long long nxt_ret =
          dfs(pos - 1, limitx && (i == upx), limity && (j == upy));
      long long cur_ret = (i ^ j) == (i + j) ? 1 : 0;
      ret += nxt_ret * cur_ret;
    }
  }
  return dp[pos][limitx][limity] = ret;
}
long long solve(int x, int y) {
  if (x < 0) return 0;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= 40; ++i) arrx[i] = x % 2, x /= 2;
  for (int i = 1; i <= 40; ++i) arry[i] = y % 2, y /= 2;
  return dfs(40, true, true);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int x, y;
    cin >> x >> y;
    cout << solve(y, y) - 2 * solve(x - 1, y) + solve(x - 1, x - 1) << endl;
  }
  return 0;
}
