#include <bits/stdc++.h>
using namespace std;
int has[50] = {0,   1,   2,   3,   4,   5,   6,   7,   8,    9,   10,  12,  14,
               15,  18,  20,  21,  24,  28,  30,  35,  36,   40,  42,  45,  56,
               60,  63,  70,  72,  84,  90,  105, 120, 126,  140, 168, 180, 210,
               252, 280, 315, 360, 420, 504, 630, 840, 1260, 2520};
long long dp[25][2550][50];
int wei[30];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
long long dfs(int pos, int yushu, int index, int flag) {
  int i, j;
  if (pos == 0) {
    if (yushu % has[index] == 0)
      return 1;
    else
      return 0;
  }
  if (!flag && dp[pos][yushu][index] != -1) return dp[pos][yushu][index];
  int ed = flag ? wei[pos] : 9;
  int yushu1, lcm1, index1;
  long long ans = 0;
  for (i = 0; i <= ed; i++) {
    yushu1 = (yushu * 10 + i) % 2520;
    if (i == 0)
      index1 = index;
    else {
      int lcm = has[index];
      lcm1 = lcm * i / gcd(lcm, i);
      index1 = lower_bound(has, has + 49, lcm1) - has;
    }
    ans += dfs(pos - 1, yushu1, index1, flag && (i == ed));
  }
  if (!flag) dp[pos][yushu][index] = ans;
  return ans;
}
long long solve(long long x) {
  int i, j, len = 0;
  long long t = x;
  while (t) {
    wei[++len] = t % 10;
    t /= 10;
  }
  return dfs(len, 0, 1, 1);
}
int main() {
  int i, j, T;
  long long n, m;
  memset(dp, -1, sizeof(dp));
  scanf("%d", &T);
  while (T--) {
    scanf("%I64d%I64d", &m, &n);
    printf("%I64d\n", solve(n) - solve(m - 1));
  }
  return 0;
}
