#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int inf = 1e9 + 7;
inline void read(int &x) {
  x = 0;
  char ch = getchar();
  while (ch < '0') ch = getchar();
  while (ch >= '0') {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
}
int pre[N][20];
char s[20];
inline void Init() {
  for (int j = (0); j <= (15); j++) {
    for (int i1 = (0); i1 <= (15); i1++)
      for (int i2 = (0); i2 <= (15); i2++)
        for (int i3 = (0); i3 <= (15); i3++)
          for (int i4 = (0); i4 <= (15); i4++) {
            int xx = i1 * 4096 + i2 * 256 + i3 * 16 + i4;
            int mp = max(i1, max(i2, max(i3, max(i4, j))));
            if (xx)
              pre[xx][j] = pre[xx - 1][j];
            else
              pre[xx][j] = 0;
            if ((1 << mp) & xx) pre[xx][j]++;
          }
  }
}
int pos[20];
inline int Get(char x) {
  if (x >= '0' && x <= '9')
    return x - '0';
  else
    return x - 'a' + 10;
}
long long f[15][20][2];
inline long long Solve() {
  memset(f, 0, sizeof(f));
  f[0][0][1] = 1;
  for (int i = (0); i <= (11); i++)
    for (int j = (0); j <= (15); j++) {
      if (f[i][j][0])
        for (int k = (0); k <= (15); k++) f[i + 1][max(k, j)][0] += f[i][j][0];
      if (f[i][j][1]) {
        int xx = pos[i + 1];
        for (int k = (0); k <= (xx - 1); k++)
          f[i + 1][max(k, j)][0] += f[i][j][1];
        f[i + 1][max(xx, j)][1] += f[i][j][1];
      }
    }
  long long ans = 0;
  int xp = pos[13] * 4096 + pos[14] * 256 + pos[15] * 16 + pos[16];
  for (int j = (0); j <= (15); j++) {
    ans += f[12][j][0] * (long long)pre[65535][j];
    ans += f[12][j][1] * (long long)pre[xp][j];
  }
  return ans;
}
int main() {
  Init();
  int q;
  read(q);
  while (q--) {
    long long l = 0, r = 0;
    scanf("%s", s + 1);
    int xq;
    int len = strlen(s + 1);
    for (int i = (1); i <= (16); i++) pos[i] = 0;
    xq = len;
    for (int i = (16); i >= (1); i--) {
      if (!len) break;
      pos[i] = Get(s[len]);
      len--;
    }
    if (pos[16] == 0 && xq == 1)
      l = 0;
    else {
      pos[16]--;
      int xp = 16;
      while (pos[xp] < 0) {
        pos[xp] += 16;
        pos[xp - 1]--;
        xp--;
      }
      l = Solve();
    }
    scanf("%s", s + 1);
    len = strlen(s + 1);
    for (int i = (1); i <= (16); i++) pos[i] = 0;
    for (int i = (16); i >= (1); i--) {
      if (!len) break;
      pos[i] = Get(s[len]);
      len--;
    }
    r = Solve();
    printf("%I64d\n", r - l);
  }
  return 0;
}
