#include <bits/stdc++.h>
using namespace std;
long long a[20];
long long dp[20][260][2550];
long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); }
long long dfs(long long pos, long long has, long long sum, bool lead,
              bool limit) {
  if (pos == -1) {
    long long t = 1;
    for (long long i = 2; i < 10; i++) {
      if ((has & t) != 0) {
        if (sum % i != 0) return 0;
      }
      t <<= 1;
    }
    return 1;
  }
  if (!limit && !lead && dp[pos][has][sum] != -1) return dp[pos][has][sum];
  long long up = limit ? a[pos] : 9;
  long long ans = 0;
  for (long long i = 0; i <= up; i++) {
    if (i > 1)
      ans += dfs(pos - 1, has | (1 << (i - 2)), (sum * 10 + i) % 2520,
                 lead && i == 0, limit && i == a[pos]);
    else
      ans += dfs(pos - 1, has, (sum * 10 + i) % 2520, lead && i == 0,
                 limit && i == a[pos]);
  }
  if (!limit && !lead) dp[pos][has][sum] = ans;
  return ans;
}
long long solve(long long x) {
  long long pos = 0;
  while (x) {
    a[pos++] = x % 10;
    x /= 10;
  }
  return dfs(pos - 1, 0, 0, true, true);
}
int main() {
  long long li, ri;
  long long t;
  cin >> t;
  memset(dp, -1, sizeof dp);
  while (t--) {
    scanf("%I64d%I64d", &li, &ri);
    long long a = solve(ri);
    long long b = solve(li - 1);
    printf("%I64d\n", a - b);
  }
  return 0;
}
