#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ldb;
#define umap __gnu_pbds::cc_hash_table
#define mkp make_pair
#define prque priority_queue
#define emp emplace
#define empb emplace_back
#define empf emplace_front
#define invarg invalid_argument
#define cus_throw(msg) throw invarg(string(msg) + " at " + __FILE__ + ":" + to_string(__LINE__))
random_device rndv;
mt19937 rd(rndv());
const ll inf = 0x3f3f3f3f3f3f3f3fll;
const vector<ll> millerrabin = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
const double eps = 1e-8;
inline void enter(){putchar('\n');}
inline void space(){putchar(' ');}
inline ll readll(){ll ret=0,k=1;char c;do{c=getchar();if(c=='-'){k=-1;}}while(('0'>c)|('9'<c));do{ret=(ret<<3)+(ret<<1)+c-48;c=getchar();}while(('0'<=c)&(c<='9'));return ret*k;}
inline void read(ll&x){x=readll();}
inline char readchar(){char ret;do{ret=getchar();}while(ret<=32);return ret;}
inline void read(char&x){x=readchar();}
inline string readstr(){string ret;char c;do{c=getchar();}while(c<=32);do{ret+=c;c=getchar();}while((c>32)&(c!=EOF));return ret;}
inline void read(string&x){x=readstr();}
inline void write(ll x){if(!x){putchar('0');return;}if(x<0){putchar('-');x*=-1;}char op[20]={};ll cur=0;while(x){op[++cur]=x%10+48;x/=10;}while(cur){putchar(op[cur--]);}}
inline ostream& operator<<(ostream& out, __int128 x){if(!x){out<<"0";return out;}if(x<0){out<<"-";x*=-1;}char op[40]={};ll cur=0;while(x){op[++cur]=x%10+48;x/=10;}while(cur){out<<op[cur--];}return out;}
#pragma GCC optimize(1,2,3,"Ofast","inline")
#define ar array<ll,4>
#define lcm(x,y) (y==0?x:(x==0?y:x*y/__gcd(x,y)))
ll l, r, lim, pw10[] = {1, 10, 100, 1000, 2440, 1720, 2080, 640, 1360, 1000, 2440, 1720, 2080, 640, 1360, 1000, 2440, 1720, 2080, 640, 1360, 1000, 2440, 1720, 2080, 640, 1360, 1000};
map<ll, ll> mem;
inline ll dig(ll x, ll d) {
while (d--) x /= 10;
return x % 10;
}
inline ll code(ll x, ll y, ll z, ll w) {
return (x << 27ll) + (y << 14ll) + (z << 1ll) + w;
}
ll srh(ll d, ll rm, ll lc, bool free) {
if (d < 0) {
return (rm % lc == 0);
}
if (mem[code(d, rm, lc, free)]) {
return mem[code(d, rm, lc, free)];
}
ll ret = 0;
if (free) {
for (ll i = 0; i <= 9; ++i) {
ret += srh(d - 1, (rm + pw10[d] * i) % 2520, lcm(lc, i), 1);
}
return mem[code(d, rm, lc, free)] = ret;
}
ll dld = dig(lim, d);
for (ll i = 0; i < dld; ++i) {
ret += srh(d - 1, (rm + pw10[d] * i) % 2520, lcm(lc, i), 1);
}
ret += srh(d - 1, (rm + pw10[d] * dld) % 2520, lcm(lc, dld), 0);
return mem[code(d, rm, lc, free)] = ret;
}
ll slv(ll v) {
mem.clear();
lim = v;
return srh(20, 0, 1, 0);
}
void init() {}
int mian() {
read(l), read(r);
if (r - l < 1e6) {
ll ans = 0;
for (ll i = l; i <= r; ++i) {
bool dfkj = 1;
for (ll j = 0; j <= 19; ++j) {
if (dig(i, j) && i % dig(i, j)) {
dfkj = 0;
}
}
ans += dfkj;
}
write(ans), enter();
return 0;
}
write(slv(r) - slv(l - 1)), enter();
return 0;
}
int main() {
ll T;
read(T);
while (T--) {
init();
mian();
}
return 0;
}
; ;;;;;;;; ;
; ; ; ;
; ; ; ;
; ; ; ;
; ; ;;;;;;;;;
; ; ; ;
; ; ; ;
;;;;;;;;;;; ;;;;;;;; ; ;
; ;
; ;
; ;
; ;
; ;
; ;
; ;
; ;; ;; ;
; ;; ;; ;