#include <bits/stdc++.h>
using namespace std;
const int LCM = 2520;
int a[20];
int b[LCM + 5];
long long dp[20][50][LCM + 5];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
long long dfs(int i, int prelcm, int presum, bool e) {
  if (i < 0) return presum % prelcm == 0;
  if (!e && dp[i][b[prelcm]][presum] != -1) return dp[i][b[prelcm]][presum];
  int u = e ? a[i] : 9;
  long long res = 0;
  for (int d = 0; d <= u; ++d) {
    int nowlcm = prelcm, nowsum = (presum * 10 + d) % LCM;
    if (d) nowlcm = (prelcm * d) / gcd(prelcm, d);
    res += dfs(i - 1, nowlcm, nowsum, e && d == u);
  }
  return e ? res : dp[i][b[prelcm]][presum] = res;
}
long long solve(long long n) {
  int i = 0;
  while (n) {
    a[i++] = n % 10;
    n /= 10;
  }
  return dfs(i - 1, 1, 0, 1);
}
int main() {
  long long n, l, r;
  int cnt = 0;
  for (int i = 1; i <= LCM; ++i)
    if (LCM % i == 0) b[i] = cnt++;
  memset(dp, -1, sizeof(dp));
  while (scanf("%I64d", &n) == 1) {
    while (n--) {
      scanf("%I64d%I64d", &l, &r);
      long long a = solve(l - 1);
      long long b = solve(r);
      cout << b - a << endl;
    }
  }
  return 0;
}
