#include <bits/stdc++.h>
using namespace std;
long long n, f[16][16][1 << 16], a[21];
long long dfs(long long x, long long mx, long long now, long long op) {
  if (!x) return now >> mx & 1;
  if (!op && f[x][mx][now] != -1) return f[x][mx][now];
  long long i, ans = 0, hh = (op ? a[x] : 15);
  for (i = 0; i <= hh; i++)
    ans += dfs(x - 1, max(mx, i), (x <= 4) ? (now | (i << ((x - 1) * 4))) : now,
               op && (i == hh));
  if (!op) f[x][mx][now] = ans;
  return ans;
}
long long solve(long long x) {
  if (x <= 0) return 0;
  n = 0;
  while (x) {
    a[++n] = x % 16;
    x /= 16;
  }
  return dfs(n, 0, 0, 1);
}
int main() {
  memset(f, -1, sizeof(f));
  long long t1, l, r;
  scanf("%lld", &t1);
  while (t1--) {
    cin >> hex >> l >> hex >> r;
    printf("%lld\n", solve(r) - solve(l - 1));
  }
}
