#include<iostream>
#include<cstring>
#include<cstdio>
#include<sstream>
using namespace std;
int del[]={0,9,90,738,5274,32490,168570,712890,2345850,5611770,8877690};
int f[]={1,1,2,6,24,120,720,5040,40320,362880,3628800};
long long int fordel(long long int k,long long int n)
{
if(k>n)
return f[k]/f[k-n];
else
return f[k]/f[n-k];
}
string itoa(int i)
{
string s;
ostringstream out;
out << i;
s = out.str();
return s;
}
int diglen(long long n)
{
int cnt=0;
while(n)
{n/=10;
cnt++;
}
return cnt;
}
long long int delicious(long long int n)
{
if(n<=10)
return n;
if(diglen(n)>10)
return del[10];
string s=itoa(n);
int k=s.size();
long long int res=del[k-1];
int b[10];
for(int i=0;i<10;i++)
b[i]=0;
for(int i=0;i<k;i++)
{
int d=s[i]-'0';
for(int j=0;j<=9;j++)
if(i==0 && j==0)
continue;
else if(b[j]==0 && j<d)
{res+=fordel(9-i,k-i-1);
// cout<<fordel(9-i,k-i-1)<<" for"<<d<<endl;
}
else if(b[j]==1 && j==d)
return res;
b[d]=1;
}
return res+1;
}
string itoa(long long int i)
{
string s;
ostringstream out;
out << i;
s = out.str();
return s;
}
int isdel(string s)
{ int c[10];
for(int i=0;i<10;i++)
c[i]=0;
for(int i=0;i<s.size();i++)
if(c[s[i]-'0']==1)
return 0;
else
c[s[i]-'0']++;
return 1;
}
int main()
{ long long int t,l,r;
cin>>t;
while(t--)
{
scanf("%lld %lld",&l,&r);
if(isdel(itoa(l))==0)
{
// cout<<delicious(l)<<" "<<delicious(r)<<endl;
printf("%lld\n",delicious(r)-delicious(l));
}
else
{
//cout<<delicious(l)<<" "<<delicious(r)<<endl;
printf("%lld\n",delicious(r)-delicious(l)+1);
}
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGNzdHJpbmc+CiNpbmNsdWRlPGNzdGRpbz4KI2luY2x1ZGU8c3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIGludCBkZWxbXT17MCw5LDkwLDczOCw1Mjc0LDMyNDkwLDE2ODU3MCw3MTI4OTAsMjM0NTg1MCw1NjExNzcwLDg4Nzc2OTB9OwogaW50IGZbXT17MSwxLDIsNiwyNCwxMjAsNzIwLDUwNDAsNDAzMjAsMzYyODgwLDM2Mjg4MDB9OwogbG9uZyBsb25nIGludCBmb3JkZWwobG9uZyBsb25nIGludCBrLGxvbmcgbG9uZyBpbnQgbikKIHsKICAgaWYoaz5uKQogICByZXR1cm4gZltrXS9mW2stbl07CiAgIGVsc2UKICAgcmV0dXJuIGZba10vZltuLWtdOwogIH0KCiAgICBzdHJpbmcgaXRvYShpbnQgaSkKICAgIHsKICAgICAgIHN0cmluZyBzOwogICAgICAgb3N0cmluZ3N0cmVhbSBvdXQ7CiAgICAgICBvdXQgPDwgaTsKICAgICAgIHMgPSBvdXQuc3RyKCk7CiAgICAgICByZXR1cm4gczsKICAgIH0KICAgaW50IGRpZ2xlbihsb25nIGxvbmcgbikKICAgIHsKCiAgICAgICAgaW50IGNudD0wOwogICAgICAgIHdoaWxlKG4pCiAgICAgICAge24vPTEwOwogICAgICAgIGNudCsrOwogICAgICAgIH0KICAgICAgICByZXR1cm4gY250OwogICAgfQoKICBsb25nIGxvbmcgaW50IGRlbGljaW91cyhsb25nIGxvbmcgaW50IG4pCiAgewogICAgICAgaWYobjw9MTApCiAgICByZXR1cm4gbjsKICAgICAgaWYoZGlnbGVuKG4pPjEwKQogICAgIHJldHVybiBkZWxbMTBdOwogICAgIHN0cmluZyBzPWl0b2Eobik7CiAgICBpbnQgaz1zLnNpemUoKTsKICAgIGxvbmcgbG9uZyBpbnQgcmVzPWRlbFtrLTFdOwogICAgIGludCBiWzEwXTsKICAgICBmb3IoaW50IGk9MDtpPDEwO2krKykKICAgICAgYltpXT0wOwoKICAgICBmb3IoaW50IGk9MDtpPGs7aSsrKQogICAgIHsKICAgICAgIGludCBkPXNbaV0tJzAnOwogICAgICAgIGZvcihpbnQgaj0wO2o8PTk7aisrKQogICAgICAgICBpZihpPT0wICYmIGo9PTApCiAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICBlbHNlIGlmKGJbal09PTAgJiYgajxkKQogICAgICAgICAge3Jlcys9Zm9yZGVsKDktaSxrLWktMSk7CiAgICAgICAgICAvLyBjb3V0PDxmb3JkZWwoOS1pLGstaS0xKTw8IiAgZm9yIjw8ZDw8ZW5kbDsKICAgICAgICAgIH0KICAgICAgICAgIGVsc2UgaWYoYltqXT09MSAmJiBqPT1kKQogICAgICAgIHJldHVybiByZXM7CiAgICAgICBiW2RdPTE7CiAgICAgICB9CiAgICAgICByZXR1cm4gcmVzKzE7CiAgICAgICB9CgogIHN0cmluZyBpdG9hKGxvbmcgbG9uZyBpbnQgaSkKICB7CiAgICBzdHJpbmcgczsKICAgIG9zdHJpbmdzdHJlYW0gb3V0OwogICAgb3V0IDw8IGk7CiAgICBzID0gb3V0LnN0cigpOwogICAgcmV0dXJuIHM7CiB9CgppbnQgaXNkZWwoc3RyaW5nIHMpCiB7IGludCBjWzEwXTsKIGZvcihpbnQgaT0wO2k8MTA7aSsrKQogIGNbaV09MDsKICBmb3IoaW50IGk9MDtpPHMuc2l6ZSgpO2krKykKICAgaWYoY1tzW2ldLScwJ109PTEpCiAgICByZXR1cm4gMDsKICAgIGVsc2UKICAgY1tzW2ldLScwJ10rKzsKICAgcmV0dXJuIDE7CiAgIH0KCiBpbnQgbWFpbigpCiB7IGxvbmcgbG9uZyBpbnQgdCxsLHI7CiAgY2luPj50OwogIHdoaWxlKHQtLSkKICB7CiAgICBzY2FuZigiJWxsZCAlbGxkIiwmbCwmcik7CiAgICBpZihpc2RlbChpdG9hKGwpKT09MCkKICAgICB7CiAgICAgIC8vIGNvdXQ8PGRlbGljaW91cyhsKTw8IiAiPDxkZWxpY2lvdXMocik8PGVuZGw7CiAgICAgIHByaW50ZigiJWxsZFxuIixkZWxpY2lvdXMociktZGVsaWNpb3VzKGwpKTsKICAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgICAvL2NvdXQ8PGRlbGljaW91cyhsKTw8IiAiPDxkZWxpY2lvdXMocik8PGVuZGw7CiAgICAgICBwcmludGYoIiVsbGRcbiIsZGVsaWNpb3VzKHIpLWRlbGljaW91cyhsKSsxKTsKICAgIH0KICB9CiAgICByZXR1cm4gMDsKIH0=