#include <bits/stdc++.h>
using namespace std;
long long lcm[3000], f[20][3000][55], num[55];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long asd(long long now, long long mod, long long gbs, long long jud) {
  if (now < 0) {
    if (mod % gbs == 0)
      return 1;
    else
      return 0;
  }
  if (jud == 0 && f[now][mod][lcm[gbs]] != -1) {
    return f[now][mod][lcm[gbs]];
  }
  long long mmax = 9, mans = 0;
  if (jud == 1) mmax = num[now];
  for (long long i = 0; i <= mmax; i++) {
    mans += asd(now - 1, (mod * 10 + i) % 2520,
                i != 0 ? i * gbs / gcd(i, gbs) : gbs, jud && i == mmax);
  }
  if (jud == 0) f[now][mod][lcm[gbs]] = mans;
  return mans;
}
long long solve(long long x) {
  long long cnt = 0;
  memset(num, 0, sizeof(num));
  while (x != 0) {
    num[cnt++] = x % 10;
    x /= 10;
  }
  return asd(cnt - 1, 0, 1, 1);
}
int main() {
  int t;
  scanf("%d", &t);
  for (long long i = 1, j = 0; i <= 2520; i++) {
    if (2520 % i == 0) {
      lcm[i] = j++;
    }
  }
  memset(f, -1, sizeof(f));
  while (t--) {
    long long aa, bb;
    scanf("%lld %lld", &aa, &bb);
    long long ans = solve(bb) - solve(aa - 1);
    printf("%lld\n", ans);
  }
  return 0;
}
