#include <bits/stdc++.h>
using namespace std;
long long num[25], f[25][2530][50];
int book[2530], cur;
inline int gcd(int x, int y) { return x % y ? gcd(y, x % y) : y; }
inline int lcm(int x, int y) {
  if (!y) return x;
  return x * y / gcd(x, y);
}
long long dfs(int pos, int mod, int _lcm, int limit) {
  if (!pos) return mod % _lcm == 0;
  if (!limit && ~f[pos][mod][book[_lcm]]) return f[pos][mod][book[_lcm]];
  long long ret = 0, maxx = limit ? num[pos] : 9;
  for (int i = 0; i <= maxx; i++)
    ret +=
        dfs(pos - 1, (mod * 10 + i) % 2520, lcm(_lcm, i), limit && (i == maxx));
  if (!limit) f[pos][mod][book[_lcm]] = ret;
  return ret;
}
long long solve(long long x) {
  int len = 0;
  while (x) num[++len] = x % 10, x /= 10;
  return dfs(len, 0, 1, 1);
}
int main() {
  std::ios::sync_with_stdio(0);
  memset(f, -1, sizeof(f));
  for (int i = 1; i <= 2520; i++)
    if (2520 % i == 0) book[i] = ++cur;
  int q;
  cin >> q;
  while (q--) {
    long long a, b;
    cin >> a >> b;
    cout << solve(b) - solve(a - 1) << endl;
  }
  return 0;
}
