#include <bits/stdc++.h>
const int L = 20;
int T, d[L], len = 0, tot = 0;
long long l, r, dp[L][50][2530];
int pos[2530];
int gcd(int a, int b) {
  while (b ^= a ^= b ^= a %= b)
    ;
  return a;
}
long long dfs(int p, bool full, int lcm, int mod) {
  if (!(~p)) {
    return mod % lcm == 0;
  }
  if (!full && ~dp[p][pos[lcm]][mod]) return dp[p][pos[lcm]][mod];
  long long res = 0;
  for (int i = 0; i <= (full ? d[p] : 9); ++i) {
    if (i == 1 || i == 0)
      res += dfs(p - 1, full && i == d[p], lcm, (mod * 10 + i) % 2520);
    else
      res += dfs(p - 1, full && i == d[p], lcm / gcd(lcm, i) * i,
                 (mod * 10 + i) % 2520);
  }
  if (!full) dp[p][pos[lcm]][mod] = res;
  return res;
}
long long calc(long long x) {
  for (int i = 0; i <= 19; ++i) d[i] = x % 10, x /= 10;
  return dfs(19, 1, 1, 0);
}
void print(int l, int r) {
  for (int i = l; i <= r; ++i) {
    bool flag = false;
    int t = i;
    while (t) {
      if (t % 10 && i % (t % 10)) {
        flag = true;
        break;
      }
      t /= 10;
    }
    if (!flag) printf("%d\n", i);
  }
}
int main() {
  scanf("%d", &T);
  for (int i = 1; i <= 2520; ++i)
    if (2520 % i == 0) pos[i] = ++tot;
  memset(dp, -1, sizeof(dp));
  while (T--) {
    scanf("%lld%lld", &l, &r);
    printf("%lld\n", calc(r) - calc(l - 1));
  }
  return 0;
}
