#include <bits/stdc++.h>
using namespace std;
const int p = 2520;
int t;
long long n, m, dp[20][2600][50];
int a[20], b[2600];
int gcd(int q, int w) {
  if (!w) return q;
  return gcd(w, q % w);
}
int Lcm(int q, int w) { return (q * w) / gcd(q, w); }
long long dfs(int pos, int pre, int lcm, bool limit) {
  if (pos == 0) return pre % lcm == 0;
  if (!limit && dp[pos][pre][b[lcm]] != -1) return dp[pos][pre][b[lcm]];
  int up = limit ? a[pos] : 9;
  long long res = 0;
  for (int i = 0; i <= up; i++) {
    res += dfs(pos - 1, (pre * 10 + i) % p, i ? Lcm(lcm, i) : lcm,
               limit && i == a[pos]);
  }
  if (!limit) dp[pos][pre][b[lcm]] = res;
  return res;
}
long long solve(long long x) {
  int pos = 0;
  while (x) {
    a[++pos] = x % 10;
    x /= 10;
  }
  return dfs(pos, 0, 1, true);
}
int main() {
  memset(dp, -1, sizeof(dp));
  int cnt = 0;
  for (int i = 1; i <= p; i++) {
    if (p % i == 0) b[i] = ++cnt;
  }
  cin >> t;
  while (t--) {
    cin >> n >> m;
    cout << solve(m) - solve(n - 1) << endl;
  }
}
