#include <bits/stdc++.h>
using namespace std;
int lcm[51], rlc[3000], cnt, lc;
int len, arr[20];
int vis[20][3000][50][2], tim = 0;
long long f[20][3000][50][2];
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int Lcm(int a, int b) {
  return max(1, a) * max(1, b) / gcd(max(1, a), max(1, b));
}
long long F(int now, int a, int b, int tp) {
  if (now == len + 1) return a % lcm[b] == 0;
  if (vis[now][a][b][tp] == tim || (tp == 0 && vis[now][a][b][tp] > 0))
    return f[now][a][b][tp];
  vis[now][a][b][tp] = tim;
  long long &ret = f[now][a][b][tp] = 0;
  a = 10 * a % lc;
  for (int i = 0; i <= (tp ? arr[now] : 9); i++)
    ret += F(now + 1, a + i >= lc ? a + i - lc : a + i, rlc[Lcm(lcm[b], i)],
             tp & (arr[now] == i));
  return ret;
}
long long Solve(long long num) {
  ++tim;
  len = 0;
  for (; num;) arr[++len] = num % 10, num /= 10;
  for (int i = 1; i < len - i + 1; i++) swap(arr[i], arr[len - i + 1]);
  for (int i = 19; i >= 0; i--)
    arr[i] = len - 19 + i >= 0 ? arr[len - 19 + i] : 0;
  len = 19;
  return F(1, 0, 1, 1);
}
int main() {
  for (int a = 1; a <= 8; a *= 2)
    for (int b = 1; b <= 9; b *= 3)
      for (int c = 1; c <= 5; c *= 5)
        for (int d = 1; d <= 7; d *= 7) lcm[++cnt] = a * b * c * d;
  sort(lcm + 1, lcm + 1 + cnt);
  for (int i = 1; i <= cnt; i++) rlc[lcm[i]] = i;
  lc = lcm[cnt];
  int T;
  for (scanf("%d", &T); T--;) {
    long long L, R;
    scanf("%I64d%I64d", &L, &R);
    printf("%I64d\n", Solve(R) - Solve(L - 1));
  }
}
