#include <bits/stdc++.h>
long long play(long long L, int d, int g, bool gvs) {
  if (d < 0) return 0;
  long long res = 0;
  long long tot = 1, notg = 1;
  for (int i = 0; i < d; i++) {
    int tv = 0, gv = 0;
    if (g >= 4 * i && g < 4 * i + 4) {
      for (int j = 0; j <= g; j++) {
        if ((j & (1 << (g - i * 4))) != 0) {
          tv++;
          if (j != g) gv++;
        }
      }
    } else {
      tv = g + 1;
      gv = g;
    }
    tot *= tv;
    notg *= gv;
  }
  for (long long i = 0; i <= g; i++) {
    if (g >= 4 * d && g < 4 * d + 4) {
      if ((i & (1 << (g - d * 4))) == 0) continue;
    }
    if ((i << (4 * d)) > L) {
      continue;
    }
    if (i == (L >> (4 * d))) {
      res += play(L ^ (i << (4 * d)), d - 1, g, gvs || (i == g));
      continue;
    }
    if (gvs || i == g) {
      res += tot;
    } else {
      res += tot - notg;
    }
  }
  return res;
}
int main() {
  int T;
  scanf("%d", &T);
  for (; T--;) {
    long long L, R;
    scanf("%llx%llx", &L, &R);
    if (L == 0) L = 1;
    if (R == 0) R = 1;
    long long LS = 0, RS = 0;
    if (R == (1LL << 60) - 1) {
      RS = 1;
      R--;
    }
    for (int i = 1; i <= 15; i++) {
      LS += play(L, 14, i, false);
      RS += play(R + 1, 14, i, false);
    }
    printf("%lld\n", RS - LS);
  }
  return 0;
}
