#include <bits/stdc++.h>
using namespace std;
long long dp[30][3000][60], num[30], indexx[3000], cnt;
const long long mod = 2520LL;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long dfs(long long pos, long long presum, long long prelcm, bool flag) {
  if (pos == 0) return presum % prelcm == 0;
  if (flag && dp[pos][presum][indexx[prelcm]] != -1)
    return dp[pos][presum][indexx[prelcm]];
  long long u = flag ? 9 : num[pos];
  long long ans = 0;
  for (long long d = 0; d <= u; d++) {
    long long newsum = (presum * 10 + d) % mod;
    long long newlcm = prelcm;
    if (d) newlcm = lcm(d, newlcm);
    ans += dfs(pos - 1, newsum, newlcm, flag || d < u);
  }
  if (flag) dp[pos][presum][indexx[prelcm]] = ans;
  return ans;
}
long long solve(long long x) {
  long long len = 0;
  while (x) {
    num[++len] = x % 10;
    x /= 10;
  }
  return dfs(len, 0, 1, 0);
}
int main() {
  long long T, A, B;
  cnt = 0;
  for (long long i = 1; i <= mod; i++)
    if (mod % i == 0) indexx[i] = cnt++;
  memset(dp, -1, sizeof(dp));
  cin >> T;
  while (T--) {
    cin >> A >> B;
    cout << solve(B) - solve(A - 1) << endl;
  }
}
