#include <bits/stdc++.h>
using namespace std;
long long f[20][50][255];
int a[20], b[2550], c[255][10] = {0};
int gcd(int a, int b) {
  while (a ^= b ^= a ^= b %= a)
    ;
  return b;
}
int lc(int a, int b) { return a * b / gcd(a, b); }
long long dfs(int p, int s, int l, bool t) {
  if (!p) return l % s == 0;
  if (!t && f[p][b[s]][l] >= 0) return f[p][b[s]][l];
  int end = t ? a[p] : 9;
  long long ans = 0;
  for (int i = 0; i <= end; i++)
    ans += dfs(p - 1, i ? lc(s, i) : s, p == 1 ? l * 10 + i : c[l][i],
               i == end & t);
  if (!t) f[p][b[s]][l] = ans;
  return ans;
}
long long se(long long x) {
  int po = 0;
  for (; x; x /= 10) a[++po] = x % 10;
  return dfs(po, 1, 0, 1);
}
int main() {
  memset(f, -1, sizeof(f));
  for (int j = 0, i = 1; i <= 2520; i++) b[i] = 2520 % i ? 0 : ++j;
  for (int j = 0; j < 10; j++)
    for (int i = 0; i < 252; i++) c[i][j] = (i * 10 + j) % 252;
  int t;
  long long x, y;
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld", &x, &y);
    printf("%lld\n", se(y) - se(x - 1));
  }
}
