#include <bits/stdc++.h>
using namespace std;
long long d[25][50][2550];
int a[50];
int hh[2550];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long dfs(int len, int pl, int px, bool l) {
  if (len == -1) return px % pl == 0;
  if (!l && d[len][hh[pl]][px] != -1) return d[len][hh[pl]][px];
  int n = l ? a[len] : 9;
  long long sum = 0;
  for (int i = 0; i <= n; i++) {
    int nowpx = (px * 10 + i) % 2520;
    int nowpl = pl;
    if (i) nowpl = lcm(pl, i);
    sum += dfs(len - 1, nowpl, nowpx, l && i == n);
  }
  if (!l) d[len][hh[pl]][px] = sum;
  return sum;
}
long long CC(long long x) {
  int n = 0;
  while (x) {
    a[n++] = x % 10;
    x /= 10;
  }
  return dfs(n - 1, 1, 0, 1);
}
int main() {
  int u = 0;
  for (int i = 1; i <= 2520; i++) {
    if (2520 % i == 0) hh[i] = ++u;
  }
  int T;
  while (~scanf("%d", &T)) {
    memset(d, -1, sizeof(d));
    while (T--) {
      long long a, b;
      scanf("%I64d%I64d", &a, &b);
      printf("%I64d\n", CC(b) - CC(a - 1));
    }
  }
  return 0;
}
