#include <bits/stdc++.h>
using namespace std;
long long dp[20][50][2521];
int a[20];
int m[2521];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
long long dfs(int pos, int num, int prelcm, bool limit) {
  if (pos == -1) return num % prelcm == 0;
  if (!limit && dp[pos][m[prelcm]][num] != -1) return dp[pos][m[prelcm]][num];
  long long ans = 0;
  int up = limit ? a[pos] : 9;
  for (int i = 0; i <= up; i++) {
    ans += dfs(pos - 1, (num * 10 + i) % 2520, i ? lcm(prelcm, i) : prelcm,
               limit && i == a[pos]);
  }
  if (!limit) dp[pos][m[prelcm]][num] = ans;
  return ans;
}
long long solve(long long n) {
  int c = 0;
  while (n) {
    a[c++] = n % 10;
    n /= 10;
  }
  return dfs(c - 1, 0, 1, true);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  long long l, r;
  int c = 1;
  for (int i = 1; i <= 2520; i++) {
    if (2520 % i == 0) m[i] = c++;
  }
  memset((dp), (-1), sizeof(dp));
  cin >> t;
  while (t--) {
    cin >> l >> r;
    cout << solve(r) - solve(l - 1) << endl;
  }
  return 0;
}
