Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri(1 ≤ li ≤ ri ≤ 9 ·1018).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).
1
1 9
9
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///meek #include<bits/stdc++.h> using namespace std; using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define pb push_back inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0';ch=getchar(); }return x*f; } //**************************************** const int N=100000+50; #define mod 10000007 #define inf 10000007 #define maxn 10000 ll lcm(ll a,ll b) { ll h,l; h=__gcd(a,b); l=a*b/h; return l; }int H[N]; ll dp[20][2525][50],vis[20][2525][50],d[50]; ll dfs(int dep,int sum,int f,int G) { if(dep<0) return sum%G==0; if(f&&vis[dep][sum][H[G]]) return dp[dep][sum][H[G]]; if(f) { vis[dep][sum][H[G]]=1; ll& re=dp[dep][sum][H[G]]; re+=dfs(dep-1,(sum*10)%2520,f,G); for(int i=1;i<=9;i++) { re+=dfs(dep-1,(sum*10+i)%2520,f,lcm(G,i)); } return re; } else { ll re=0; re+=dfs(dep-1,(sum*10)%2520,0<d[dep],G); for(int i=1;i<=d[dep];i++) { re+=dfs(dep-1,(sum*10+i)%2520,i<d[dep],lcm(G,i)); }return re; } } ll cal(ll x) { if(x==0) return 1; int len=0;//mem(dp),mem(vis); while(x) d[len++]=x%10,x/=10; return dfs(len-1,0,0,1); } int main() { int j=0; for(int i=1;i<=2520;i++) { if(2520%i==0) H[i]=j++; } int T=read(); while(T--) { ll l,r; scanf("%I64d%I64d",&l,&r); printf("%I64d\n",cal(r)-cal(l-1)); } return 0; }