#include <bits/stdc++.h>
using namespace std;
int n, m;
bool mark[40][2][2][2][2][2];
long long memo[40][2][2][2][2][2];
long long dp(int pos, int a1, int b1, int a2, int b2, int eq) {
  if (pos == -1) return 1;
  long long& ans = memo[pos][a1][b1][a2][b2][eq];
  if (mark[pos][a1][b1][a2][b2][eq]) return ans;
  mark[pos][a1][b1][a2][b2][eq] = true;
  ans = 0;
  int p1 = (n & (1 << pos)) >> pos;
  int p2 = (m & (1 << pos)) >> pos;
  for (int i = 0; i <= 1; i++) {
    for (int j = 0; j <= 1; j++) {
      if (i + j > 1) continue;
      if (eq && i < j) continue;
      if (a1 && i < p1) continue;
      if (b1 && i > p2) continue;
      if (a2 && j < p1) continue;
      if (b2 && j > p2) continue;
      ans += dp(pos - 1, a1 && i == p1, b1 && i == p2, a2 && j == p1,
                b2 && j == p2, eq && i == j);
    }
  }
  return ans;
}
void init() { memset(mark, false, sizeof mark); }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &n, &m);
    init();
    printf("%lld\n", 2 * dp(30, 1, 1, 1, 1, 1) - (n == 0));
  }
  return 0;
}
