#include <bits/stdc++.h>
using namespace std;
int a[3000], cnt;
long long l, r;
int shu[20], tot;
long long f[20][3000][50];
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long Lcm(long long x, long long y) { return x / gcd(x, y) * y; }
long long dfs(int len, int sum, int n, int shangxian, int head) {
  int lend = len, sumd = sum, nd = n, shangxiand = shangxian, headd = head;
  if (len == 0) {
    if (sum % n == 0)
      return 1;
    else
      return 0;
  }
  if (!shangxian && f[len][sum][a[n]] != -1) return f[len][sum][a[n]];
  int maxx = 9;
  if (shangxian) maxx = shu[len];
  long long cnt = 0;
  for (int i = 0; i <= maxx; i++) {
    if (i == 0)
      cnt += dfs(len - 1, (sum * 10) % 2520, n, shangxian && i == maxx,
                 head && i == 0);
    else
      cnt += dfs(len - 1, (sum * 10 + i) % 2520, Lcm(n, i),
                 shangxian && i == maxx, head && i == 0);
  }
  f[len][sum][a[n]] = cnt;
  return cnt;
}
long long solve(long long x) {
  tot = 0;
  if (x == 0) shu[++tot] = 0;
  while (x) {
    shu[++tot] = x % 10;
    x /= 10;
  }
  memset(f, -1, sizeof(f));
  return dfs(tot, 0, 1, 1, 1);
}
int main() {
  for (int i = 1; i <= 2520; i++)
    if (2520 % i == 0) a[i] = ++cnt;
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%lld%lld", &l, &r);
    printf("%lld\n", solve(r) - solve(l - 1));
  }
  return 0;
}
