#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  register long long x = 0, f = 1;
  register char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
  return x * f;
}
long long t, l, r, tot, cnt;
long long f[50 + 5][2520 + 5][50 + 5], in[2520 + 5], a[2520 + 5], bas[2520 + 5];
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long dfs(long long p, long long sum, long long lcm, bool limit) {
  if (!p) return sum % lcm == 0 ? 1 : 0;
  if (!limit && f[p][sum][in[lcm]] != -1) return f[p][sum][in[lcm]];
  long long up = limit ? a[p] : 9, ans = 0;
  for (register long long i = 0; i <= up; ++i) {
    if (i == 0)
      ans += dfs(p - 1, sum * 10 % 2520, lcm, limit && (i == up));
    else
      ans += dfs(p - 1, (sum * 10 + i) % 2520, lcm * i / gcd(lcm, i),
                 limit && (i == up));
  }
  if (!limit) f[p][sum][in[lcm]] = ans;
  return ans;
}
long long calc(long long x) {
  cnt = 0;
  while (x) a[++cnt] = x % 10, x /= 10;
  return dfs(cnt, 0, 1, 1);
}
signed main() {
  t = read();
  memset(f, -1, sizeof(f));
  ;
  for (register long long i = 1; i <= 2520; ++i)
    if (2520 % i == 0) in[i] = (++(tot));
  while (t--) l = read(), r = read(), printf("%lld\n", calc(r) - calc(l - 1));
  return 0;
}
