#include <bits/stdc++.h>
using namespace std;
int q, tot, num[20], hsh[2525];
long long a, b, dp[20][49][2520];
inline int get_lcm(int x, int y) {
  if (y == 0) return x;
  int sav_x = x, sav_y = y, r = x % y;
  while (r > 0) x = y, y = r, r = x % y;
  return sav_x * sav_y / y;
}
inline long long dfs(int len, int lcm, int MO, int flag) {
  if (len == 0) return MO % lcm == 0;
  if (flag != 0 && dp[len][hsh[lcm]][MO] != -1) return dp[len][hsh[lcm]][MO];
  long long ans = 0;
  for (int i = 0; i <= 9; i++) {
    if (flag == 0 && i > num[len]) break;
    ans += dfs(len - 1, get_lcm(lcm, i), (MO * 10 + i) % 2520,
               flag || (i < num[len]));
  }
  if (flag != 0) dp[len][hsh[lcm]][MO] = ans;
  return ans;
}
inline long long wrk(long long x) {
  int len = 0;
  while (x > 0) num[++len] = x % 10, x /= 10;
  return dfs(len, 1, 0, 0);
}
int main() {
  for (int i = 1; i <= 2520; i++)
    if (2520 % i == 0) hsh[i] = ++tot;
  memset(dp, -1, sizeof(dp));
  scanf("%d", &q);
  for (int p = 0; p < q; p++)
    scanf("%lld%lld", &a, &b), printf("%lld\n", wrk(b) - wrk(a - 1));
  return 0;
}
