#include <bits/stdc++.h>
const double pi = 4.0 * atan(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
const int maxm = 40000;
const int mod = 2520;
using namespace std;
long long int n, m, tol;
int digit[20];
long long int dp[20][50][2600];
int id[2600];
void init() {
  memset(dp, -1, sizeof dp);
  memset(id, 0, sizeof id);
}
long long int gcd(long long int a, long long int b) {
  return b == 0 ? a : gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return a / gcd(a, b) * b;
}
void handle() {
  tol = 1;
  for (int i = 1; i <= mod; i++) {
    if (mod % i == 0) {
      id[i] = tol++;
    }
  }
}
long long int dfs(int pos, int presum, int prelcm, int limit) {
  if (pos == 0) return presum % prelcm == 0 ? 1 : 0;
  if (!limit && dp[pos][id[prelcm]][presum] != -1)
    return dp[pos][id[prelcm]][presum];
  int up = limit ? digit[pos] : 9;
  long long int ans = 0;
  for (int i = 0; i <= up; i++) {
    int nowsum = (presum * 10 + i) % mod;
    int nowlcm = i ? lcm(prelcm, i) : prelcm;
    ans += dfs(pos - 1, nowsum, nowlcm, limit && i == digit[pos]);
  }
  if (!limit) dp[pos][id[prelcm]][presum] = ans;
  return ans;
}
long long int solve(long long int x) {
  memset(digit, 0, sizeof digit);
  int pos = 1;
  while (x) {
    digit[pos++] = x % 10;
    x /= 10;
  }
  return dfs(pos - 1, 0, 1, true);
}
int main() {
  int T;
  scanf("%d", &T);
  init();
  handle();
  while (T--) {
    scanf("%lld%lld", &n, &m);
    long long int ans = solve(m) - solve(n - 1);
    printf("%lld\n", ans);
  }
  return 0;
}
