#include <bits/stdc++.h>
using namespace std;
long long dp[65][3000][50];
long long a[65];
map<long long, long long> hashmap;
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  if (!b) return a;
  return gcd(b, a % b);
}
long long dfs(int pos, long long sum, long long pre, bool limit) {
  if (pos == -1) return (sum % (pre) == 0);
  if (!limit && dp[pos][sum][hashmap[pre]] != -1)
    return dp[pos][sum][hashmap[pre]];
  long long up = limit ? a[pos] : 9;
  long long ans = 0;
  for (long long i = 0; i <= up; ++i) {
    if (i == 0)
      ans += dfs(pos - 1, sum * 10 % 2520, pre, limit && a[pos] == i);
    else
      ans += dfs(pos - 1, (sum * 10 + i) % (long long)2520,
                 pre * i / gcd(pre, i), limit && a[pos] == i);
  }
  if (!limit) dp[pos][sum][hashmap[pre]] = ans;
  return ans;
}
long long solve(long long x) {
  int pos = 0;
  if (x == 0)
    a[0] = 0, pos = 1;
  else
    while (x) {
      a[pos++] = x % 10;
      x /= 10;
    }
  return dfs(pos - 1, 0, 1, true);
}
int main() {
  int kase;
  cin >> kase;
  memset(dp, -1, sizeof(dp));
  for (long long i = 1, j = 0; i <= 2520; ++i)
    if ((long long)2520 % i == 0) hashmap[i] = j++;
  while (kase--) {
    long long l, r;
    cin >> l >> r;
    cout << solve(r) - solve(l - 1) << endl;
  }
  return 0;
}
