写了有些错误还没改过来
好长
#include <iostream>
#include <algorithm>
using namespace std;
long long count2(int n,int p);
long long count3(int n,int p);
long long l,r,k;
long long b[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000LL};
long long c[]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL};
long long count1(int n,int p)
{
if(n<0) return 0;
if(n<10) {
return 1+n-(n>=p);
}
int i=0;
while(b[i+1]<n) i++;
int a=n/b[i];
if(a==p){
return count1(n-b[i]+c[i],p);
}
else if(a>p) {
return (n/b[i]-1)*count1(c[i],p)+count1(n%b[i],p);
}
else{
return (n/b[i])*count1(c[i],p)+count1(n%b[i],p);
}
}
long long f2(int n,int a2,int d,int p)
{
if(n/b[d]==0&&a2==0) return 0;
if(n<10) return n+1-(n>=a2);
int m=n/b[d];
if(m==0){
return count2(n,p);
}
else{
if(m>a2)
{
return (m-1)*count2(c[d],p)+count2(n%b[d],p);
}else if(m==a2)
{
return (m)*count2(c[d],p);
}else{
return m*count2(c[d],p)+count2(n%b[d],p);
}
}
}
long long count2(int n,int p)
{
if(n<p) return 0;
if(n<100) return n+1-(n>=p);
int a=p/10;
int a2=p%10;
int i=0;
while(b[i+1]<n) i++;
if(n/b[i]>a) {
return (n/b[i]-1)*count2(c[i],p)+f2(n%b[i],a2,i-1,p);
}else if(n/b[i]==a){
return (n/b[i])*count2(c[i],p);
}else
{
return (n/b[i]-1)*count2(c[i],p)+f2(n%b[i],a2,i-1,p);
}
}
int f3(int n,int a1,int a2,int d,int p )
{
if(n/b[d]==0&&a2==0) return 0;
if(n<10) return n+1-(n>=a2);
int m=n/b[d];
if(m==0){
return count3(n,p);
}
else{
if(m>a2)
{
if(m>a1){
return (m-2)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p );
}
else if(m==a1){
return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p );
}
else{
return (m-1)*count3(c[d],p)+count3(n%b[d],p);
}
}else if(m==a2)
{
if(m>a1){
return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
}
else
{
return (m)*count3(c[d],p);
}
}else{
if(m>a1){
return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
}
else if(m==a1){
return m*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p);
}
else
return m*count3(c[d],p)+count3(n%b[d],p);
}
}
}
long long count3(int n,int p)
{
if(n<p) return 0;
if(n<100)
return n+1-(n>=p);
int a=p/10;
int a2=p%10;
int i=0;
while(b[i+1]<n) i++;
if(n/b[i]>a) {
return (n/b[i]-1)*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p);
}else if(n/b[i]==a){
return (n/b[i])*count3(c[i],p);
}else
{
return (n/b[i]-1)*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p);
}
}
long long countn(int n,int p)
{
if(n<0) return 0;
long long d=0,m=p,res=0;
while(m!=0) {m=m/10;d++;}
if(d==1)
{
//cout<<p<<endl;
res=count1(n,p);
}else{
int a=p/10;
int b=p%10;
if(a==b) res=count2(n,p);
else res=count3(n,p);
}
return res;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>l>>r>>k;
if(l>r) swap(l,r);
//
cout<<r-l+1-countn(r,k)+countn(l-1,k)<<endl;
}
return 0;
}