#include <bits/stdc++.h>
using namespace std;
const long long mod = 2520;
long long l, r, f[21][mod + 1][50];
long long num[21], mp[mod + 1], cnt;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long dfs(long long len, long long pre, long long mod, long long lim) {
  if (!len) return pre % mod == 0;
  if (!lim && f[len][pre][mp[mod]] != -1) return f[len][pre][mp[mod]];
  long long res = 0;
  long long x = lim ? num[len] : 9;
  for (long long i = 0; i <= x; i++)
    res += dfs(len - 1, (pre * 10 + i) % 2520,
               i == 0 ? mod : mod * i / (gcd(mod, i)), lim && x == i);
  return lim ? res : f[len][pre][mp[mod]] = res;
}
long long solve(long long n) {
  long long len = 0;
  while (n) {
    num[++len] = n % 10;
    n /= 10;
  }
  return dfs(len, 0, 1, 1);
}
signed main() {
  memset(f, -1, sizeof f);
  for (long long i = 1; i <= mod; i++) {
    if (2520 % i == 0) mp[i] = ++cnt;
  }
  long long T = read();
  while (~--T) {
    l = read(), r = read();
    printf("%lld\n", solve(r) - solve(l - 1));
  }
  return 0;
}
