#include <bits/stdc++.h>
using namespace std;
int num[2550], cnt = 0;
int lim[50], lct = 0;
long long f[24][2520][50];
long long l, r, T, ans;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long dp(int pos, int smm, int lcm, bool flag) {
  if (!pos) return (smm % lcm == 0);
  if (!flag && f[pos][smm][num[lcm]] != -1) return f[pos][smm][num[lcm]];
  long long res = 0;
  int limit = 9;
  if (flag) limit = lim[pos];
  for (int i = 0; i <= limit; i++) {
    int psmm = (smm * 10 + i) % 2520;
    int plcm = lcm;
    if (i) {
      plcm = lcm / gcd(lcm, i) * i;
    }
    res += dp(pos - 1, psmm, plcm, flag && i == limit);
  }
  if (!flag) f[pos][smm][num[lcm]] = res;
  return res;
}
long long solve(long long x) {
  lct = 0;
  while (x > 0) {
    lim[++lct] = x % 10;
    x /= 10;
  }
  return dp(lct, 0, 1, 1);
}
int main() {
  int i, j;
  for (int i = 1; i <= 2520; i++) {
    if (2520 % i == 0) num[i] = ++cnt;
  }
  memset(f, -1, sizeof f);
  T = read();
  while (T--) {
    l = read();
    r = read();
    ans = solve(r) - solve(l - 1);
    cout << ans << endl;
  }
  return 0;
}
