#include <bits/stdc++.h>
using namespace std;
long long dp[19][1 << 8][2520];
bool okey[1 << 8][2520];
int mul[20];
int dig[20];
int n;
long long dfs(int pos, int st, int sum) {
  if (pos == -1) {
    return okey[st][sum];
  }
  long long &ret = dp[pos][st][sum];
  if (ret != -1) return ret;
  ret = 0;
  for (int i = 0; i <= 9; i++) {
    int Nst = st;
    if (i >= 2) Nst |= (1 << (i - 2));
    int Nsum = (sum + i * mul[pos]) % 2520;
    ret += dfs(pos - 1, Nst, Nsum);
  }
  return ret;
}
long long func(long long num) {
  n = 0;
  while (num) {
    dig[n++] = num % 10;
    num /= 10;
  }
  long long ret = 0;
  int st = 0, sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < dig[i]; j++) {
      int Nst = st;
      if (j >= 2) Nst |= (1 << (j - 2));
      ret += dfs(i - 1, Nst, (sum + mul[i] * j) % 2520);
    }
    if (dig[i] >= 2) st |= (1 << (dig[i] - 2));
    sum = (sum + mul[i] * dig[i]) % 2520;
  }
  return ret + okey[st][sum];
}
void init() {
  for (int i = 0; i < 256; i++) {
    for (int j = 0; j < 2520; j++) {
      okey[i][j] = true;
      for (int d = 2; d <= 9; d++) {
        if (i & (1 << (d - 2))) {
          if (j % d != 0) {
            okey[i][j] = false;
            break;
          }
        }
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  mul[0] = 1;
  for (int i = 1; i < 20; i++) {
    mul[i] = (mul[i - 1] * 10) % 2520;
  }
}
int main() {
  init();
  int T;
  cin >> T;
  while (T--) {
    long long a, b;
    cin >> a >> b;
    cout << func(b) - func(a - 1) << endl;
  }
  return 0;
}
