#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int MAXM = 1e6 + 5;
long long n, m;
long long dp[20][50][2520];
long long digit[105];
long long mod = 0;
int cnt = 0;
inline long long read() {
  long long X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
  return X * w;
}
int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }
int LCM(int a, int b) { return a / GCD(a, b) * b; }
int tong[2521] = {0};
void init() {
  for (int i = 1; i <= 2520; i++) {
    if (2520 % i == 0) {
      tong[i] = ++cnt;
    }
  }
}
long long dfs(int len, int lcm, int st, bool limit) {
  if (len == 0) {
    return st % lcm == 0;
  }
  if (!limit && ~dp[len][tong[lcm]][st]) return dp[len][tong[lcm]][st];
  int maxx = limit ? digit[len] : 9;
  long long ans = 0;
  for (int i = 0; i <= maxx; i++) {
    int L = (i == 0 ? lcm : LCM(lcm, i));
    ans += dfs(len - 1, L, (st * 10 + i) % 2520, limit && i == maxx);
  }
  if (!limit) dp[len][tong[lcm]][st] = ans;
  return ans;
}
long long solve(long long num) {
  int k = 0;
  while (num) {
    digit[++k] = num % 10;
    num /= 10;
  }
  return dfs(k, 1, 0, 1);
}
int main() {
  long long t;
  t = read();
  init();
  memset(dp, -1, sizeof dp);
  while (t--) {
    long long a, b;
    a = read(), b = read();
    printf("%I64d\n", solve(b) - solve(a - 1));
  }
}
