#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-12;
const double PI = acos(-1.0);
const int maxn = 4010;
const int maxl = 2520;
long long dp[20][50][2530];
long long a[2530];
int has[2530];
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long dfs(long long pos, long long prelcm, long long prenum, long long up) {
  if (pos == -1) return prenum % prelcm == 0;
  if (!up && dp[pos][has[prelcm]][prenum] != -1)
    return dp[pos][has[prelcm]][prenum];
  int n = up ? a[pos] : 9;
  long long res = 0;
  for (int i = 0; i <= n; i++) {
    long long nownum = (prenum * 10 + i) % maxl;
    long long nowlcm = prelcm;
    if (i) nowlcm = lcm(nowlcm, i);
    res += dfs(pos - 1, nowlcm, nownum, up && i == n);
  }
  if (!up) dp[pos][has[prelcm]][prenum] = res;
  return res;
}
long long solve(long long n) {
  int pos = 0;
  while (n > 0) {
    a[pos++] = n % 10;
    n /= 10;
  }
  return dfs(pos - 1, 1, 0, 1);
}
int main() {
  long long l, r;
  int T;
  scanf("%d", &T);
  int cnt = 0;
  for (int i = 1; i <= maxl; i++) {
    if (maxl % i == 0) {
      has[i] = cnt++;
    }
  }
  memset(dp, -1, sizeof(dp));
  while (T--) {
    scanf("%I64d%I64d", &l, &r);
    printf("%I64d\n", solve(r) - solve(l - 1));
  }
  return 0;
}
