#include <bits/stdc++.h>
using namespace std;
const int iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e60;
template <typename T>
inline void scf(T &x) {
  bool f = 0;
  x = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') {
    f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (f) x = -x;
  return;
}
template <typename T1, typename T2>
void scf(T1 &x, T2 &y) {
  scf(x);
  return scf(y);
}
template <typename T1, typename T2, typename T3>
void scf(T1 &x, T2 &y, T3 &z) {
  scf(x);
  scf(y);
  return scf(z);
}
template <typename T1, typename T2, typename T3, typename T4>
void scf(T1 &x, T2 &y, T3 &z, T4 &w) {
  scf(x);
  scf(y);
  scf(z);
  return scf(w);
}
inline char mygetchar() {
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  return c;
}
template <typename T>
void chkmax(T &x, const T &y) {
  if (y > x) x = y;
  return;
}
template <typename T>
void chkmin(T &x, const T &y) {
  if (y < x) x = y;
  return;
}
const int N = 20;
int n, dig[N];
long long dp[N][N][2], g[65540][N][N];
long long table[65540];
int get(char c) { return c - (c >= 'a' ? 'a' - 10 : '0'); }
bool check(long long x) {
  long long ret = x, y = 0;
  while (x) {
    y |= (1ll << (x & 15));
    x >>= 4;
  }
  return (ret ^ y) < ret;
}
void MEI() {
  for (int x = 0; x < (65536); ++x) {
    if (x) table[x] = table[x - 1];
    table[x] += check(x);
  }
  for (int x = 0; x < (65536); ++x) {
    int y = x, mx = 0;
    while (y) {
      chkmax(mx, y & 15);
      y >>= 4;
    }
    for (int mx_front = 0; mx_front < (16); ++mx_front) {
      if ((x >> max(mx_front, mx)) & 1) {
        g[x][mx][mx_front]++;
      }
    }
  }
  for (int x = (1); x <= (65535); ++x)
    for (int mx1 = 0; mx1 < (16); ++mx1)
      for (int mx2 = 0; mx2 < (16); ++mx2) g[x][mx1][mx2] += g[x - 1][mx1][mx2];
  return;
}
long long solve(long long x) {
  memset((dp), (0), sizeof((dp)));
  ;
  if (x < 65536) return table[x];
  long long y = x & 65535, ans = 0;
  n = 0;
  while (x) dig[n++] = x & 15, x >>= 4;
  dp[n][0][1] = 1;
  for (int i = (n); i >= (5); --i)
    for (int mx = 0; mx < (16); ++mx)
      for (int f = 0; f < (2); ++f)
        if (dp[i][mx][f]) {
          long long &t = dp[i][mx][f];
          for (int y = 0; y < (16); ++y) {
            if (f && y > dig[i - 1]) break;
            dp[i - 1][max(mx, y)][f && y == dig[i - 1]] += t;
          }
        }
  for (int mx1 = 0; mx1 < (16); ++mx1)
    for (int mx2 = 0; mx2 < (16); ++mx2) {
      ans +=
          dp[4][mx1][0] * g[65535][mx2][mx1] + dp[4][mx1][1] * g[y][mx2][mx1];
    }
  return ans;
}
void ZI() {
  int q;
  scf(q);
  while (q--) {
    char s[N];
    long long x = 0, res;
    scanf("%s", s);
    int n = strlen(s);
    for (int i = 0; i < (n); ++i) x = x * 16 + get(s[i]);
    res = -solve(x - 1);
    scanf("%s", s);
    n = strlen(s);
    x = 0;
    for (int i = 0; i < (n); ++i) x = x * 16 + get(s[i]);
    res += solve(x);
    printf("%I64d\n", res);
  }
  return;
}
int main() {
  MEI();
  ZI();
  return 0;
}
