#include <bits/stdc++.h>
using namespace std;
long long hashh[2599];
long long num[25], dp[25][2599][60];
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
long long Lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long dfs(long long len, long long mod, long long lcm, long long limit) {
  long long sum = 0;
  if (len <= 0) return (mod % lcm) == 0;
  if (!limit && dp[len][mod][hashh[lcm]] != -1) return dp[len][mod][hashh[lcm]];
  long long up = limit ? num[len] : 9;
  for (int i = 0; i <= up; i++) {
    sum += dfs(len - 1, (mod * 10 + i) % 2520, i ? Lcm(lcm, i) : lcm,
               (i == up) && limit);
  }
  if (!limit) dp[len][mod][hashh[lcm]] = sum;
  return sum;
}
long long solve(long long number) {
  long long len = 0;
  while (number) {
    num[++len] = number % 10;
    number /= 10;
  }
  return dfs(len, 0, 1, true);
}
void init() {
  memset(dp, -1, sizeof(dp));
  long long a = 0;
  for (int i = 1; i * i < 2520; i++) {
    if (2520 % i == 0) {
      hashh[i] = a++;
      hashh[2520 / i] = a++;
    }
  }
}
int main() {
  long long a, b;
  init();
  int n;
  cin >> n;
  while (n--) {
    cin >> a >> b;
    cout << solve(b) - solve(a - 1) << endl;
  }
}
