#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void read(long long &x) {
  long long v = 0ll, f = 1ll;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void readc(char &x) {
  char c;
  while (((c = getchar()) == ' ') || c == '\n')
    ;
  x = c;
}
inline void writes(string s) { puts(s.c_str()); }
inline void writeln() { writes(""); }
inline void writei(int x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
inline void writell(long long x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
long long n, m, i, j, t, dp[35][2][2];
vector<long long> dig1, dig2;
long long dfs(long long x, bool l1, bool l2) {
  if (x == -1) return 1;
  if (dp[x][l1][l2] != -1) return dp[x][l1][l2];
  dp[x][l1][l2] = 0;
  int i, j, ed1 = (l1 ? dig1[x] : 1), ed2 = (l2 ? dig2[x] : 1);
  for ((i) = (0); (i) <= (ed1); (i)++)
    for ((j) = (0); (j) <= (ed2); (j)++)
      if (!(i & j)) {
        dp[x][l1][l2] += dfs(x - 1, l1 && (i == ed1), l2 && (j == ed2));
      }
  return dp[x][l1][l2];
}
long long solve(long long x, long long y) {
  if (x < 0 || y < 0) return 0;
  dig1.clear();
  dig2.clear();
  memset(dp, -1, sizeof(dp));
  while (x || y) {
    dig1.push_back(x % 2);
    dig2.push_back(y % 2);
    x /= 2;
    y /= 2;
  }
  long long ans = dfs(((long long)dig1.size()) - 1, 1, 1);
  return ans;
}
int main() {
  read(t);
  while (t--) {
    read(n);
    read(m);
    cout << solve(m, m) - solve(n - 1, m) - solve(m, n - 1) +
                solve(n - 1, n - 1)
         << endl;
  }
  return 0;
}
