#include <bits/stdc++.h>
using namespace std;
long long f[16][16][1 << 17];
int b[16];
long long dp(int x, int Mx, int sum, bool flag) {
  if (x == 0) {
    return (sum >> Mx) & 1;
  }
  if (!flag && f[x][Mx][sum] != -1) return f[x][Mx][sum];
  int lm = flag ? b[x] : 15;
  long long ans = 0;
  for (int i = 0; i <= lm; i++) {
    int tmp = sum;
    if (x <= 4) tmp |= i << ((x - 1) * 4);
    ans += dp(x - 1, max(Mx, i), tmp, flag && i == lm);
  }
  return flag ? ans : f[x][Mx][sum] = ans;
}
long long solve(long long x) {
  int p = 0;
  memset(b, 0, sizeof(b));
  while (x) {
    b[++p] = x % 16;
    x /= 16;
  }
  return dp(p, 0, 0, 1);
}
int main() {
  int _;
  scanf("%d", &_);
  memset(f, -1, sizeof(f));
  while (_--) {
    long long l, r;
    scanf("%I64x%I64x", &l, &r);
    printf("%I64d\n", solve(r) - solve(l - 1));
  }
  return 0;
}
