#include <bits/stdc++.h>
using namespace std;
long long d[100], n, to[2525];
long long f[25][2520][50], ans1, a, b, ans2;
int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
void dat() {
  int ps = 0;
  for (int i = 1; i <= 2520; i++) {
    if (2520 % i == 0) {
      to[i] = ps++;
    }
  }
}
long long o_o(int pos, int mod, int t, int limit) {
  if (pos < 1) return mod % t == 0;
  if (limit && f[pos][mod][to[t]] != -1) return f[pos][mod][to[t]];
  int end = limit ? 9 : d[pos];
  long long ret = 0;
  for (int i = 0; i <= end; i++) {
    int zlcm;
    if (i == 0)
      zlcm = t;
    else
      zlcm = lcm(t, i);
    ret += o_o(pos - 1, (mod * 10 + i) % 2520, zlcm, limit || (i != end));
  }
  if (limit) {
    f[pos][mod][to[t]] = ret;
  }
  return ret;
}
int chai(long long a) {
  int tot = 0;
  while (a > 0) {
    d[++tot] = a % 10;
    a = a / 10;
  }
  return tot;
}
void readdata() {
  scanf("%lld", &n);
  dat();
  memset(f, -1, sizeof(f));
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &a, &b);
    ans2 = 0;
    ans1 = 0;
    ans1 = o_o(chai(a - 1), 0, 1, 0);
    ans2 = o_o(chai(b), 0, 1, 0);
    printf("%lld\n", ans2 - ans1);
  }
}
int main() { readdata(); }
