• + 0 comments

    My recursive solution to this problem---

    #include<bits/stdc++.h>
    #define ll long long
    #define ull unsigned long long
    #define mod 1000000007
    using namespace std;
    ull binpow(ull x,ull y)/* (x^y)%p in O(log y) */{ull res=1;while (y > 0){if(y&1)res=(res*x);y = y>>1;x=(x*x);}return res;}
    ll binpowmod(ll x,ll y,ll p)/* (x^y)%p in O(log y) */{ull res=1;x=x%p;while (y > 0){if(y&1)res=(res*x)%p;y = y>>1;x=(x*x)%p;}return res;}
    ll mod_inverse(ll n,ll p)/* Returns n^(-1) mod p */{return binpowmod(n,p-2,p);}
    int gcd(int x,int y)
    {
        if(y==0)
            return x;
        return gcd(y,x%y);
    }
    bool comp(pair<int,int> x,pair<int,int> y)
    {
        return x.first<y.first;
    }
    bool comp_pairs_by_s(pair<ll,ll> &x ,pair<ll,ll> &y)
    {
        return x.second<y.second;
    }
    bool isPowerOfTwo (ll x)  
    {  
        /* First x in the below expression is for the case when x is 0 */
        return x && (!(x&(x-1)));  
    }
    
    class cmp      //comparator for priority_queue 
    {               //declaration: priority_queue<int,vector<int>,cmp>
    public:         
        bool operator()(pair<int,int> A,pair<int,int> B)
        {
            if(abs(A.first-A.second)==abs(B.first-B.second))
                return A.first>B.first;
            return abs(A.first-A.second)<abs(B.first-B.second);
        }
    };
    int prime[10005]={0};
    void sieve(void)
    {
     int i,j;
     for(i=0;i<10005;i++)
            prime[i]=1;
     prime[0]=0,prime[1]=0;
     for(i=2;i<=sqrt(10005);i++){
         if(prime[i]){
             for(j=i*i;j<10005;j+=i){
                 prime[j]=0;
             }
         }
     }
        
    }
    void swap(ll &x,ll &y){
        int temp=x;
        x=y;
        y=temp;
    }
      
    unsigned int onesComplement(unsigned int n) 
    { 
       // Find number of bits in the given integer 
       int number_of_bits = floor(log2(n))+1; 
      
       // XOR the given integer with poe(2,  
       // number_of_bits-1 and print the result  
       return ((1 << number_of_bits) - 1) ^ n; 
    }
    
    ll cnt(ull limit,ull num=1,ull len=1){
        if(limit<=9)
            return limit+1;
        if(num>limit)
            return -1;
        if(num>=binpow(10,len) )
            return 0;
        if(num<binpow(10,len-1))
            return -1;
        if(len==1){
            ll ans=5;
            for(ll i=5;i<=9;++i)
                ans+=1+max(0LL,cnt(limit,i*(len+1),len+1));
            return ans;
        }
        ll ans=1;
        ll i=0;
        while(1){
            ll c=cnt(limit,num*(len+i),len+i);
            if(c==-1) break;
            ans+=c;
            i++;
        }
        return ans;
    }
    
    void solve()
    {   
        ull l,r;
        cin>>l>>r;
        cout<<cnt(r)-(l==0?0:cnt(l-1));
        cout<<'\n';  
    }
    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        int t;
        cin>>t;
        while(t--)
            solve();
    }