#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-') ch = getchar();
  if (ch == '-') f = -1, ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return x * f;
}
void write(long long x) {
  if (x == 0) {
    putchar('0'), putchar('\n');
    return;
  }
  int f = 0;
  char ch[20];
  if (x < 0) putchar('-'), x = -x;
  while (x) ch[++f] = x % 10 + '0', x /= 10;
  while (f) putchar(ch[f--]);
  putchar('\n');
  return;
}
int t, l, r, a[37], b[37], len;
long long f[37];
long long getf(int i, int yes1, int yes2) {
  if (i == -1) {
    return 1;
  }
  if (f[i] != -1 && yes1 && yes2) return f[i];
  int li1 = yes1 ? 1 : a[i], li2 = yes2 ? 1 : b[i];
  long long res = 0;
  res = getf(i - 1, yes1 | a[i], yes2 | b[i]);
  if (li1 == 1) res += getf(i - 1, yes1, yes2 | b[i]);
  if (li2 == 1) res += getf(i - 1, yes1 | a[i], yes2);
  if (yes1 && yes2) f[i] = res;
  return res;
}
long long work(int x, int y) {
  if (x < 0 || y < 0) return 0;
  memset(f, -1, sizeof(f));
  len = 0;
  while (((1ll << (len + 1)) - 1ll) < (long long)x) len++;
  while (((1ll << (len + 1)) - 1ll) < (long long)y) len++;
  for (register int i = (0); i <= (len); ++i) a[i] = (x & (1 << i)) ? 1 : 0;
  for (register int i = (0); i <= (len); ++i) b[i] = (y & (1 << i)) ? 1 : 0;
  return getf(len, 0, 0);
}
int main() {
  t = read();
  while (t--) {
    l = read(), r = read();
    write(work(r, r) - 2ll * work(r, l - 1) + work(l - 1, l - 1));
  }
  return 0;
}
