#include <bits/stdc++.h>
long long dp[20][50][252];
int bit[20], mp[2522], b[55];
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline long long dfs(int pos, int s, int mod, int e) {
  if (pos < 1) return mod % s == 0;
  if (!e && ~dp[pos][mp[s]][mod]) return dp[pos][mp[s]][mod];
  int mx = e ? bit[pos] : 9, i;
  long long ret = 0;
  for (i = 0; i <= mx; ++i)
    ret += dfs(pos - 1, i ? s / gcd(s, i) * i : s,
               pos > 1 ? ((mod << 1) + (mod << 3) + i) % 252
                       : ((mod << 1) + (mod << 3) + i),
               e && i == mx);
  return e ? ret : dp[pos][mp[s]][mod] = ret;
}
inline long long work(long long val) {
  int len = 0;
  for (; val; val /= 10) bit[++len] = val % 10;
  return dfs(len, 1, 0, 1);
}
int main() {
  long long n, m = 0;
  memset(dp, -1, sizeof dp);
  int t;
  for (t = 1; t <= 2520; ++t)
    if (2520 % t == 0) mp[t] = m, b[m++] = t;
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld", &n, &m);
    printf("%lld\n", work(m) - work(n - 1));
  }
  return 0;
}
