#include <bits/stdc++.h>
using namespace std;
char c[20];
int A[20], n;
long long f[20][2][2];
int X, Y, lim;
long long dfs(int x, int k1, int k2) {
  if (!x) return k2;
  if (f[x][k1][k2] != -1) return f[x][k1][k2];
  int l = k1 ? A[x] : 15, i;
  long long ans = 0;
  for (i = 0; i <= min(l, lim); i++) {
    if (x == X && !(i & (1 << Y))) continue;
    ans += dfs(x - 1, k1 && i == l, k2 || i == lim);
  }
  f[x][k1][k2] = ans;
  return ans;
}
long long G() {
  int i;
  long long ans = 0;
  for (i = 0; i <= 15; i++) {
    X = i / 4 + 1;
    Y = i % 4;
    lim = i;
    if (X > n) continue;
    memset(f, -1, sizeof(f));
    ans += dfs(n, 1, 0);
  }
  return ans;
}
int main() {
  int T, i, j, l;
  long long ans;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", c + 1);
    l = strlen(c + 1);
    n = 0;
    for (i = l; i >= 1; i--) {
      if (c[i] <= '9' && c[i] >= '0')
        A[++n] = c[i] - '0';
      else
        A[++n] = c[i] - 'a' + 10;
    }
    ans = 0;
    if (A[n]) {
      A[1]--;
      for (i = 1; i <= n; i++)
        if (A[i] < 0) A[i] += 16, A[i + 1]--;
      if (!A[n]) n--;
      if (n) ans = -G();
    }
    scanf("%s", c + 1);
    l = strlen(c + 1);
    n = 0;
    for (i = l; i >= 1; i--) {
      if (c[i] <= '9' && c[i] >= '0')
        A[++n] = c[i] - '0';
      else
        A[++n] = c[i] - 'a' + 10;
    }
    ans += G();
    printf("%I64d\n", ans);
  }
  return 0;
}
