/*************************************************
Author :supermaker
Created Time :2015/12/5 21:08:41
File Location :C:\Users\abcd\Desktop\TheEternalPoet
**************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <set>
#include <bitset>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <complex>
#include <numeric>
#include <functional>
#include <cmath>
#include <ctime>
#include <climits>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>
using namespace std;
#define CFF freopen ("aaa.txt", "r", stdin)
#define CPPFF ifstream cin ("aaa.txt")
#define DB(ccc) cout << #ccc << " = " << ccc << endl
#define PB push_back
#define MP(A, B) make_pair(A, B)
typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;
typedef pair <int, int> PII;
typedef pair <int, bool> PIB;
const int INF = 0x7F7F7F7F;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = 2 * acos (0.0);
const int maxn = 1e5 + 6666;
LL dp[22][2666][56], bit[22], cnt[2666];
LL gcd (LL vala, LL valb) {
if (valb == 0) return vala;
return gcd (valb, vala % valb);
}
LL SS (int pos, int mod, int lcm, bool flag)
{
if (pos == 0) return (mod % lcm == 0);
if (flag && dp[pos][mod][cnt[lcm]] != -1) return dp[pos][mod][cnt[lcm]];
LL res = 0;
int x = flag ? 9 : bit[pos];
for (int i = 0; i <= x; i++)
{
int ta, tb;
if (i == 0) ta = lcm;
else ta = lcm * i / gcd (lcm, i);
tb = (mod * 10 + i) % 2520;
res += SS (pos - 1, tb, ta, flag || i < x);
}
return flag ? dp[pos][mod][cnt[lcm]] = res : res;
}
LL Calc (LL val)
{
int len = 0;
while (val)
{
bit[++len] = val % 10;
val /= 10;
}
return SS (len, 0, 1, 0);
}
int main()
{
//CFF;
memset (dp, -1, sizeof (dp));
//CPPFF;
LL t, id = 0;
memset (cnt, 0, sizeof (cnt));
for (int i = 1; i <= 2520; i++)
if (2520 % i == 0)
cnt[i] = id++;
scanf ("%I64d", &t);
while (t--)
{
LL vala, valb;
scanf ("%I64d %I64d", &vala, &valb);
printf ("%I64d\n", Calc (valb) - Calc (vala - 1));
}
return 0;
}