#include <bits/stdc++.h>
int p[200];
int cnt;
long long dp[22][2520][55];
int bit[22];
void init() {
  cnt = 0;
  for (int i = 1; i <= 2520; i++) {
    if (2520 % i == 0) p[++cnt] = i;
  }
  memset(dp, -1, sizeof(dp));
}
int GCD(int a, int b) {
  while (a % b) {
    int tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}
int LCM(int a, int b) { return a / GCD(a, b) * b; }
int search(int x) {
  int l = 1, r = cnt;
  while (l < r) {
    int mid = (l + r) / 2;
    if (p[mid] == x) return mid;
    if (p[mid] >= x)
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}
long long dfs(int pos, int mod, int id, bool flag) {
  if (pos <= 0) return (mod % p[id] == 0);
  if (!flag && dp[pos][mod][id] != -1) return dp[pos][mod][id];
  int tmp = flag ? bit[pos] : 9;
  long long res = 0;
  for (int i = 0; i <= tmp; i++) {
    int r = (10 * mod + i) % 2520;
    int idd = id;
    if (i) idd = search(LCM(p[id], i));
    res += dfs(pos - 1, r, idd, flag && (i == tmp));
  }
  if (!flag) dp[pos][mod][id] = res;
  return res;
}
long long calc(long long x) {
  int len = 0;
  while (x) {
    bit[++len] = x % 10;
    x /= 10;
  }
  return dfs(len, 0, 1, true);
}
int main() {
  int cas;
  init();
  scanf("%d", &cas);
  while (cas--) {
    long long x, y;
    scanf("%I64d%I64d", &x, &y);
    printf("%I64d\n", calc(y) - calc(x - 1));
  }
  return 0;
}
