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).

var n,m,i,j,t,q,q1:longint;
    ans:int64;
    s,s1:string;
    a,b,b1:array[0..70]of longint;
    f:array[0..20,0..49,0..2520,0..1]of int64;
    c:array[0..2520]of longint;
    flag:boolean;
    ged:array[0..49,0..9]of longint;
function gcd(a,b:longint):longint;
begin
    if a mod b=0 then exit(b)
    else gcd:=gcd(b,a mod b);
end;
function gdd(a,b:longint):longint;
begin
    if (a=0) or (b=0)then exit(0);
    exit(a*b div gcd(a,b));
end;
function work(s:string):int64;
var i,j,k,tmp,l:longint;
    an:int64;
begin
    a[0]:=0;
    for i:=1 to length(s) do begin
        inc(a[0]);
        a[a[0]]:=ord(s[i])-48;
    end;
    if not flag then begin
        dec(a[a[0]]);
        i:=a[0];
         while (a[i]<0)and(i>0) do begin
             inc(a[i],10);
             dec(a[i-1]);
             dec(i);
         end;
    end;
    if a[1]=0 then
    for i:=1 to a[0]-1 do
    a[i]:=a[i+1];
    fillchar(f,sizeof(f),0);
    for i:=1 to a[1]-1 do
    f[1,i,i,1]:=1;
    f[1,a[1],a[1],0]:=1;
    f[1,49,0,1]:=1;
    q:=252;
    for i:=1 to a[0]-2 do begin
        for j:=1 to b[0]+1 do
            for k:=0 to q-1 do
             if (f[i,j,k,0]<>0)or(f[i,j,k,1]<>0) then begin
                for l:=0 to 9 do begin
                    if (b[j]<>0)and(l<>0) then
                    tmp:=c[ged[j,l]]
                    else if b[j]<>0 then tmp:=j
                    else tmp:=c[l];
                    if l<a[i+1] then
                    inc(f[i+1,tmp,(k*10+l)mod q,1],f[i,j,k,0]+f[i,j,k,1])
                    else if l=a[i+1] then begin
                         inc(f[i+1,tmp,(k*10+l)mod q,0],f[i,j,k,0]);
                         inc(f[i+1,tmp,(k*10+l)mod q,1],f[i,j,k,1]);
                    end else inc(f[i+1,tmp,(k*10+l) mod q,1],f[i,j,k,1]);
                end;
            end;
    end;
    i:=a[0]-1;
    q:=2520;
    q1:=252;
            for j:=1 to b[0]+1 do
            for k:=0 to q1-1 do
             if (f[i,j,k,0]<>0)or(f[i,j,k,1]<>0) then begin
                for l:=0 to 9 do begin
                    if (b[j]<>0)and(l<>0) then
                    tmp:=c[gdd(b[j],l)]
                    else if b[j]<>0 then tmp:=j
                    else tmp:=c[l];
                    if l<a[i+1] then
                    inc(f[i+1,tmp,(k*10+l)mod q,1],f[i,j,k,0]+f[i,j,k,1])
                    else if l=a[i+1] then begin
                         inc(f[i+1,tmp,(k*10+l)mod q,0],f[i,j,k,0]);
                         inc(f[i+1,tmp,(k*10+l)mod q,1],f[i,j,k,1]);
                    end else inc(f[i+1,tmp,(k*10+l) mod q,1],f[i,j,k,1]);
                end;
            end;
    an:=0;
    for i:=1 to b[0] do
    for j:=0 to q-1 do
     if j mod b[i]=0 then begin
     inc(an,f[a[0],i,j,0]+f[a[0],i,j,1]);
     end;
     exit(an);
end;
begin
    readln(t);
    c[0]:=49;
    for i:=1 to 2520 do
    if 2520 mod i=0 then begin
        inc(b[0]);
        b[b[0]]:=i;
        c[i]:=b[0];
    end;
    q:=252;
    for i:=1 to b[0] do
    for j:=1 to 9 do begin
    ged[i,j]:=gdd(b[i],j);
    end;
    b[b[0]+1]:=0;
    for t:=1 to t do begin
    readln(s);
    s1:=copy(s,1,pos(' ',s)-1);
    delete(s,1,pos(' ',s));
    flag:=false;
    ans:=-work(s1);
    flag:=true;
    q:=252;
    inc(ans,work(s));
    writeln(ans);
    end;
end.

Const
 a : array[1..48] of longint=(1,2,3,4,5,6,7,8,9,10,12,14,15,18,20,21,24,28,30,35,36,40,42,45,56,60,63,70,72,84,90,105,120,126,140,168,180,210,252,280,315,360,420,504,630,840,1260,2520);
 ch = 2520;

VAr
 r,i,j : longint;
 n,m,one,two : int64;
 b : array[0..20] of longint;
 c : array[0..1,1..19,0..2519,1..48] of int64;
 d : array[1..48,0..9] of 1..48;
 e : array[0..2519,0..9] of 0..2519;

 Function gcd(x,y:longint):longint;
  Begin
   While (x>0) and (y>0) do
    if x>y then x := x mod y
           else y := y mod x;
    gcd := x+y;
  End;

 Function gca(x,y:longint):longint;
  Begin
   gca := (x div gcd(x,y))*y;
  end;

 Function next(link,x:longint):longint;
  Begin
   If x=0 then
    Begin
     next := link;
     exit;
    End;
   x := gca(x,a[link]);
   While a[link]<>x do link := link+1;
   next := link;
  End;

 Function f(x:int64):int64;
  VAr
   count :longint;

   function go(flag,count,ost,link : longint):int64;
    VAr
     j:longint;
     sum : int64;
    Begin
     If count=0 then
      Begin
       If ost mod a[link]=0 then go :=1 else go := 0;
       exit;
      End;
     If c[flag,count,ost,link]=0 then
      Begin
     sum := 0;
     If flag=0 then
      Begin
       For j := 0 to b[count] do
        sum := sum+go(ord(j<b[count]),count-1,e[ost,j],d[link,j]);
      End else
       For j := 0 to 9 do
       sum := sum+go(1,count-1,e[ost,j],d[link,j]);
      c[flag,count,ost,link] := sum+1;
     End;
     go := c[flag,count,ost,link]-1;

    end;

  Begin
   count := 0;
   Repeat
    count := count+1;
    b[count] := x mod 10;
    x := x div 10;
   until x=0;
   f := go(0,count,0,1);
  End;

  begin
   Read(r);
   For i := 1 to 48 do
    For j := 0 to 9 do
     d[i,j] := next(i,j);
   For i := 0 to 2519 do
    For j := 0 to 9 do
     e[i,j] := (i*10+j) mod ch;

   For i := 1 to r do
    Begin
     REad(n,m);
     FillChar(c[0],sizeof(c[0]),0);
     one := f(m);
     FillChar(c[0],sizeof(c[0]),0);
     two := f(n-1);
     WriteLn(one-two);
    end;
  end.

const
	maxn=2520;
var
    f:array[0..18,0..2520,0..47] of int64;
    id:array[0..2530] of longint;
    w:array[0..18] of longint;
    t,i:longint;
    l,r:int64;
function gcd(a,b:int64):int64;
begin
	if b=0 then exit(a);
	exit(gcd(b,a mod b))
end;

function lcm(a,b:int64):int64;
var
    g:int64;
begin
    g:=gcd(a,b);
	lcm:=a div g * b;
end;

function dfs(p,ps,pl:int64;bz:boolean):int64;
var
	ns,nl,h,sum:int64;
	i:longint;
begin
    if p=-1 then
	begin
		if ps mod pl=0 then exit(1)
		else exit(0);
	end;
    if (bz=false)and(f[p,ps,id[pl]]<>-1) then exit(f[p,ps,id[pl]]);
    sum:=0;
    if bz then h:=w[p] else h:=9;
    for i:=0 to h do
    begin
        ns:=(ps*10+i) mod maxn;
        if i>0 then nl:=lcm(pl,i)
        else nl:=pl;
		if bz and (i=w[p]) then sum:=sum+dfs(p-1,ns,nl,true)
		else sum:=sum+dfs(p-1,ns,nl,false);
    end;
    if bz=false then f[p,ps,id[pl]]:=sum;
	exit(sum);
end;

function work(x:int64):int64;
var
	p:longint;
begin
    p:=0;
    while x>0 do
    begin
        w[p]:=x mod 10;
        x:=x div 10;
        inc(p);
    end;
    exit(dfs(p-1,0,1,true));
end;
begin
    for i:=1 to maxn do
	begin
		if maxn mod i=0 then
		begin
			id[i]:=l;
			inc(l);
		end;
    end;
    fillchar(f,sizeof(f),255);
    readln(t);
	for i:=1 to t do
	begin
        readln(l,r);
        writeln(work(r)-work(l-1));
    end;
end.

var
u:array[0..100000]of boolean;
yy,i,j,ll,tt,t:longint;
l,r:int64;
cc:array[0..19]of longint;
f:array[boolean,0..19,0..2520,0..48]of int64;
ob,numb:array[0..2520]of longint;
next:array[0..48,0..9]of longint;
nc:array[0..2520,0..9]of longint;

function gcd(x,y:longint):longint;
 begin
  while (x>0)and(y>0) do
    if x>y then x:=x mod y else y:=y mod x;
  gcd:=x+y;
 end;

function lcm(x,y:longint):longint;
 begin
  if (y=0)or(x=0) then lcm:=x+y
  else lcm:=x div gcd(x,y)*y;
 end;

function run(v:longint;ok:boolean;ost1,ost2:longint):int64;
var i:longint;
 begin
  if v=0 then
    begin
     if ost1 mod ob[ost2]=0 then run:=1 else run:=0;
     exit;
    end;

  if f[ok,v,ost1,ost2]>0 then
    begin
     run:=f[ok,v,ost1,ost2]-1;
     exit;
    end;

  f[ok,v,ost1,ost2]:=1;

  if ok then
    begin
     for i:=0 to cc[v] do
       begin
        if i<cc[v] then f[ok,v,ost1,ost2]:=f[ok,v,ost1,ost2]+run(v-1,false,nc[ost1,i],next[ost2,i])
                   else f[ok,v,ost1,ost2]:=f[ok,v,ost1,ost2]+run(v-1,true,nc[ost1,i],next[ost2,i])
       end;
    end
     else
    begin
     for i:=0 to 9 do
       f[ok,v,ost1,ost2]:=f[ok,v,ost1,ost2]+run(v-1,false,nc[ost1,i],next[ost2,i])
    end;

  run:=f[ok,v,ost1,ost2]-1;
 end;

function ans(x:int64):int64;
 begin
  ll:=0;
  while x>0 do
    begin
     ll:=ll+1;
     cc[ll]:=x mod 10;
     x:=x div 10;
    end;

  fillchar(f[true],sizeof(f[true]),0);
  ans:=run(ll,true,0,1);
 end;

 begin
  fillchar(u,sizeof(u),false);
  u[1]:=true;
  for i:=1 to 18 do
    begin
     for j:=2520 downto 1 do
       if u[j] then
         begin
          for yy:=1 to 9 do
            u[lcm(j,yy)]:=true;
         end;
    end;

  for i:=0 to 2520 do
    for j:=0 to 9 do
      nc[i,j]:=(i*10+j)mod 2520;

  j:=0;
  for i:=1 to 2520 do
   if u[i] then
    begin
     j:=j+1;
     ob[j]:=i;
     numb[i]:=j;
    end;
  for i:=1 to 2520 do
    if u[i] then
      begin
       for j:=0 to 9 do
          next[numb[i],j]:=numb[lcm(i,j)];
      end;

  readln(t);
  for tt:=1 to t do
    begin
     readln(l,r);
     writeln(ans(r)-ans(l-1));
    end;
 end.

var
  mp:array[0..2520]of shortint;
  q:array[0..400005]of longint;
  dp:array[0..20,0..2520,0..50]of int64;
  t,i,j,k,cnt,top:longint;
  l,r:int64;
function gcd(x,y:longint):longint;
begin
  if y=0 then
    exit(x);
  exit(gcd(y,x mod y));
end;
function _dfs(w,o,p:longint; f:boolean):int64;
var
  i,sl:longint;
  _res:int64;
begin
  if w=0 then
  begin
    if o mod p=0 then
      exit(1) else
      exit(0);
  end;
  if f and (dp[w][o][mp[p]]<>-1) then
    exit(dp[w][o][mp[p]]);
  if f then
    sl:=9 else
    sl:=q[w];
  _res:=0;
  for i:=0 to sl do
    if i=0 then
      _res:=_res+_dfs(w-1,(o*10+i) mod 2520,p,f or (i<>q[w])) else
      _res:=_res+_dfs(w-1,(o*10+i) mod 2520,p*i div gcd(p,i),f or (i<>q[w]));
  if f then
    dp[w][o][mp[p]]:=_res;
  exit(_res);
end;
function check(x:int64):int64;
begin
  check:=0;
  top:=0;
  while x>0 do
  begin
    inc(top);
    q[top]:=x mod 10;
    x:=x div 10;
  end;
  exit(_dfs(top,0,1,false));
end;
begin
  for i:=0 to 20 do
    for j:=0 to 2520 do
      for k:=0 to 50 do
        dp[i][j][k]:=-1;
  for i:=1 to 2520 do
  if 2520 mod i=0 then
  begin
    inc(cnt);
    mp[i]:=cnt;
  end;
  readln(t);
  for i:=1 to t do
  begin
    readln(l,r);
    writeln(check(r)-check(l-1));
  end;
end.

{$Inline on}
var
        l,r,xx,ans:int64;
        t,i,j,s1,s2,op,len:longint;
        a,mi:array[0..19] of longint;
        lcm:array[0..49,0..9] of longint;
        divv:array[0..50400] of longint;
        num,num1:array[0..2520] of longint;
        f:array[0..19,0..49,0..2520,0..1] of int64;
function gcd(x,y:longint):longint;
begin
        if x mod y=0 then exit(y) else exit(gcd(y,x mod y));
end;

function doit(x:int64):int64; inline;
begin
        len:=0; ans:=0; xx:=x; fillchar(f,sizeof(f),0);

        while xx<>0 do
        begin
                inc(len); a[len]:=xx mod 10; xx:=xx div 10;
        end;

        for i:=0 to a[1] do f[1,num[i],i,0]:=1;
        if len<>1 then for i:=a[1]+1 to 9 do f[1,num[i],i,1]:=1;

        for i:=2 to len do
                for s1:=1 to op do
                        for s2:=0 to 2519 do
                        begin
                                if f[i-1,s1,s2,1]+f[i-1,s1,s2,0]=0 then continue;

                                for j:=0 to a[i]-1 do
                                        if j<>0 then
                                        begin
                                                f[i,lcm[s1,j],divv[s2+j*mi[i-1]],0]:=f[i,lcm[s1,j],divv[s2+j*mi[i-1]],0]+f[i-1,s1,s2,0]+f[i-1,s1,s2,1];
                                        end
                                        else
                                                f[i,s1,s2,0]:=f[i,s1,s2,0]+f[i-1,s1,s2,0]+f[i-1,s1,s2,1];

                                if i<>len then
                                        for j:=a[i]+1 to 9 do
                                                f[i,lcm[s1,j],divv[s2+j*mi[i-1]],1]:=f[i,lcm[s1,j],divv[s2+j*mi[i-1]],1]+f[i-1,s1,s2,0]+f[i-1,s1,s2,1];

                                if a[i]<>0 then
                                begin
                                                inc(f[i,lcm[s1,a[i]],divv[s2+a[i]*mi[i-1]],1],f[i-1,s1,s2,1]); inc(f[i,lcm[s1,a[i]],divv[s2+a[i]*mi[i-1]],0],f[i-1,s1,s2,0]);
                                end
                                else
                                begin
                                        inc(f[i,s1,s2,0],f[i-1,s1,s2,0]); inc(f[i,s1,s2,1],f[i-1,s1,s2,1]);
                                end;
                        end;

        for s1:=1 to op-1 do
                for s2:=0 to 2519 do
                        if s2 mod num1[s1]=0 then ans:=ans+f[len,s1,s2,0];

        exit(ans);
end;

begin
        for i:=1 to 50400 do divv[i]:=i mod 2520;

        for i:=1 to 2520 do
                if 2520 mod i=0 then
                begin
                        inc(op); num[i]:=op; num1[op]:=i;
                end;

        inc(op); num[0]:=op;

        for i:=1 to 48 do
                for j:=1 to 9 do
                        lcm[i,j]:=num[num1[i]*j div gcd(num1[i],j)];

        for i:=1 to 9 do lcm[49,i]:=num[i];

        mi[0]:=1; for i:=1 to 18 do mi[i]:=mi[i-1]*10 mod 2520;

        readln(t);

        while t>0 do
        begin
                readln(l,r);

                writeln(doit(r)-doit(l-1));

                dec(t);
        end;
end.

//http://www.cppblog.com/Yuan/archive/2011/01/24/139201.html
program zxh55d;
const base=2520;
var
     dp:array[0..18,0..base,0..47] of int64;
     id:array[0..base+10] of longint;
     digit:array[0..18] of longint;
     team:longint;
     left,right:int64;

function gcd(a,b:longint):longint;
var  c:longint;
begin
     while (b<>0) do
     begin
          c:=b; b:=a mod b; a:=c;
     end;
     gcd:=a;
end;

function lcm(a,b:longint):longint;
begin
     exit( a div gcd(a,b) * b );
end;

procedure init;
var  num,i:longint;
begin
     num:=0;
     for i:=1 to base do
     if base mod i=0 then
     begin
         id[i]:=num; inc(num);
     end;
     fillchar(dp,sizeof(dp),255);
end;

function dfs(pos,presum,prelcm:longint; doing:boolean):int64;
var  nowsum,nowlcm,i,ed:longint; ans:int64;
begin
     if pos=-1 then exit(ord(presum mod prelcm=0));
     if (not doing)and(dp[pos,presum,id[prelcm]]<>-1) then
         exit(dp[pos,presum,id[prelcm]]);

     ans:=0;
     if doing then ed:=digit[pos] else ed:=9;
     for i:=0 to ed do
     begin
          nowsum:=(presum*10+i) mod base;
          nowlcm:=prelcm;
          if i>0 then
          begin
               nowlcm:=lcm(nowlcm,i);
          end;
          ans:=ans+dfs(pos-1,nowsum,nowlcm,doing and (i=ed));
     end;

     if not doing then
          dp[pos,presum,id[prelcm]]:=ans;
     exit(ans);
end;

function cal(x:int64):int64;
var  pos:longint;
begin
     pos:=0;
     while x>0 do
     begin
          digit[pos]:=x mod 10;
          x:=x div 10;
          inc(pos);
     end;
     exit( dfs(pos-1,0,1,true) );
end;

begin
     init;
     readln(team);
     while team>0 do
     begin
          dec(team);
          readln(left,right);
          writeln( cal(right)-cal(left-1) );
     end;
end.

const
        rk:array[0..4]of longint=(1,4,3,2,2);
        zz:array[0..9,1..2]of longint=((1,0),(1,0),(1,1),(2,1),(1,2),(3,1),(1,0),(4,1),(1,3),(2,2));
var
        st,en:int64;
        i,j,k,o,l,x,u,y,test:longint;
        kk,zk,oo,tok:array[1..4]of longint;
        fen,e,bb:array[1..20]of longint;
        f:array[1..19,0..47,0..2519]of int64;
function wr(x,y:longint):longint;
var
        i,j:longint;
begin
        if y<2 then exit(x);
        j:=x;
        for i:=1 to 4 do begin
                kk[i]:=j mod rk[i];
                j:=j div rk[i];
        end;
        if kk[zz[y,1]]<zz[y,2] then kk[zz[y,1]]:=zz[y,2];
        j:=0;
        for i:=4 downto 1 do begin
                j:=(j+kk[i])*rk[i-1];
        end;
        exit(j);
end;
function pd(x,y:longint):boolean;
var
        i,j,k:longint;
begin
        j:=x;
        for i:=1 to 4 do begin
                kk[i]:=j mod rk[i];
                j:=j div rk[i];
        end;
        if (kk[3]=1)and(y mod 5<>0) then exit(false);
        if (y mod 7<>0)and(kk[4]=1) then exit(false);
        j:=1;
        for i:=1 to kk[1] do j:=j*2;
        if y mod j<>0 then exit(false);
        j:=1;
        for i:=1 to kk[2] do j:=j*3;
        if y mod j<>0 then exit(false);
        exit(true);
end;
function bing(j:longint):longint;
var
        i,k,u:longint;
begin
        if j=0 then begin
                k:=0;
                for i:=4 downto 1 do begin
                        k:=(k+zk[i])*rk[i-1];
                end;
                exit(k);
        end;
        k:=j;
        for i:=1 to 4 do begin
                oo[i]:=k mod rk[i];
                k:=k div rk[i];
                if oo[i]<zk[i] then begin
                        oo[i]:=zk[i];
                end;
        end;
        k:=0;
        for i:=4 downto 1 do begin
                k:=(k+oo[i])*rk[i-1];
        end;
        exit(k);
end;
function z(x:longint):longint;
var
        i,k,l:longint;
begin
        k:=x;
        for i:=1 to 4 do begin
                oo[i]:=k mod rk[i];
                k:=k div rk[i];
        end;
        l:=1;
        for i:=1 to oo[1] do l:=l*2;
        for i:=1 to oo[2] do l:=l*3;
        for i:=1 to oo[3] do l:=l*5;
        for i:=1 to oo[4] do l:=l*7;
        exit(l);
end;
function dp(x,y:longint):int64;
var
        i,j,k,o:longint;
        dd:int64;
begin
        if x=0 then begin
                if pd(bing(0),y) then
                exit(1) else exit(0);
        end;
        dd:=0;
        for j:=0 to 47 do begin
                o:=z(bing(j));
                k:=o-y mod o;
                if k=o then k:=0;
                repeat
                        dd:=dd+f[x,j,k];
                        k:=k+o;
                until k>2519;
        end;
        exit(dd);
end;
procedure zb;
var
        i,j:longint;
begin
        j:=0;
        for i:=19 downto 1 do begin
                if bb[i]<2 then continue;
                if bb[i]=6 then begin
                        j:=wr(wr(j,2),3);
                end;
                j:=wr(j,bb[i]);
        end;
        for i:=1 to 4 do begin
                zk[i]:=j mod rk[i];
                j:=j div rk[i];
        end;
end;
function work(x:int64):int64;
var
        i,j,k:longint;
        tmp,ww:int64;
begin
        fillchar(bb,sizeof(bb),0);
        zb;
        tmp:=x;
        ww:=0;
        for i:=1 to 19 do begin
                fen[i]:=tmp mod 10;
                tmp:=tmp div 10;
        end;
        k:=0;
        for i:=19 downto 1 do begin
                for j:=1 to fen[i] do begin
                        ww:=ww+dp(i-1,k);
                        k:=(k+e[i])mod 2520;
                        inc(bb[i]);
                        zb;
                end;
        end;
        dec(ww);
        exit(ww);
end;
begin
        readln(test);
        for i:=0 to 9 do begin
                if i=6 then f[1,5,6]:=1 else
                f[1,wr(0,i),i]:=1;
        end;
        u:=1;
        e[1]:=1;
        for i:=1 to 18 do begin
                u:=u*10 mod 2520;
                e[i+1]:=u;
                for j:=0 to 47 do begin
                        for k:=0 to 2519 do begin
                                if f[i,j,k]=0 then continue;
                                for o:=0 to 9 do begin
                                        x:=(o*u+k)mod 2520;
                                        if o=6 then begin
                                                y:=wr(wr(j,2),3);
                                                f[i+1,y,x]:=(f[i+1,y,x]+f[i,j,k]);
                                                continue;
                                        end;
                                        y:=wr(j,o);
                                        f[i+1,y,x]:=(f[i+1,y,x]+f[i,j,k]);
                                end;
                        end;
                end;
        end;
        for test:=1 to test do begin
                readln(st,en);
                writeln(work(en+1)-work(st));
        end;
end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

type

  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    r, r1, lr, lr1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..2500000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;
    m : array[1..19, 1..9, 0..2520] of integer;
    l, l1 : int64;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 1;
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := m[u.kol, i, v.ost];
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 3 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;
  for i := 1 to 18 do begin
    for j := 1 to 9 do
      for ii := 0 to 2520 do
        m[i, j, ii] := (j * step[i] + ii) mod 2520;
  end;
  lr := -1;
  for i := 1 to t do begin
    read(l, r);
    {if r[i] <> r[i - 1] then
      r1[i] := f(r[i])
    else
      r1[i] := r1[i - 1];}
    if r = lr then
      r1 := lr1
    else
      r1 := f(r);
    l1 := f(l - 1);
   { if l[i] <> l[i - 1] then
      l1[i] := f(l[i] - 1)
    else
      l1[i] := l[i - 1];   }
    writeln(r1 - l1);
    lr := r;
    lr1 := r1;
  end;

end.

program task;
{$IFDEF DEBUG}{$R+,I+,Q+,S+,O-}{$ELSE}{$R-,I-,Q-,S-,O+}{$ENDIF}
uses SysUtils;
const
  F_LCM=504; //LCM of all digits except 5
//[numLength|LessOrEqual|DivisibleBy5|mod F_LCM|usedDigitsSpecificator]
//\______   \__         \__          \_        \__       __(except5)_/
var //   \     \           \           \          \     /
  a:array[1..19,false..true,false..true,0..F_LCM-1,0..23]of int64;
  b:array[1..19]of byte;
  AddDigit:array[0..23,0..9]of byte;
  tmp_a:array[0..23]of integer;
function findcnt(v:int64):int64;
var
  i,j,k,p,f,t,l:integer;
  fl:boolean;
begin
  if v=0 then begin result:=1; exit; end;
  l:=0;
  while v>0 do
    begin
      inc(l);
      b[l]:=v mod 10;
      v:=v div 10;
    end;
  fillchar(a,sizeof(a),0);
  for i:=0 to 9 do
    inc(a[1,(i<=b[1]),(i=5)or(i=0),i,AddDigit[0,i]]);
  f:=1;
  for i:=2 to l do
    begin
      f:=(f*10)mod F_LCM;
      for fl:=false to true do
        for j:=0 to 9 do
          begin
            t:=(f*j)mod F_LCM;
            for p:=0 to 23 do
              begin
                for k:=0 to F_LCM-1-t do
                  inc(a[i,(j<b[i])or((j=b[i])and fl),true,k+t,
                      AddDigit[p,j]],a[i-1,fl,true,k,p]);
                for k:=F_LCM-t to F_LCM-1 do
                  inc(a[i,(j<b[i])or((j=b[i])and fl),true,k+t-F_LCM,
                      AddDigit[p,j]],a[i-1,fl,true,k,p]);
                if j<>5 then
                  begin
                    for k:=0 to F_LCM-1-t do
                      inc(a[i,(j<b[i])or((j=b[i])and fl),false,k+t,
                          AddDigit[p,j]],a[i-1,fl,false,k,p]);
                    for k:=F_LCM-t to F_LCM-1 do
                      inc(a[i,(j<b[i])or((j=b[i])and fl),false,k+t-F_LCM,
                          AddDigit[p,j]],a[i-1,fl,false,k,p]);
                  end;
              end;
          end;
    end;
  result:=0;
  for fl:=false to true do
    for i:=0 to F_LCM-1 do
      for j:=0 to 23 do
        if i mod tmp_a[j]=0 then
          inc(result,a[l,true,fl,i,j]);
end;
function LCM(a,b:integer):integer;
var m:integer;
begin
  m:=a*b;
  if a<b then begin result:=b; b:=a; a:=result; end;
  while b>0 do
    begin
      result:=a;
      a:=b;
      b:=result mod b;
    end;
  result:=m div a;
end;
procedure fillAddDigit;
var i,j,t,k:integer;
begin
  tmp_a[0]:=1;
  for i:=0 to 23 do
    begin
      AddDigit[i,0]:=i;
      AddDigit[i,5]:=i;
      for j:=1 to 9 do if j<>5 then
        begin
          t:=LCM(tmp_a[i],j);
          k:=0;
          while(tmp_a[k]>0)and(tmp_a[k]<>t)do inc(k);
          AddDigit[i,j]:=k;
          tmp_a[k]:=t;
        end;
    end;
end;
procedure preInit;
begin
  fillAddDigit;
end;
function solvecase(l,r:int64):int64;
begin
  result:=findcnt(r)-findcnt(l-1);
end;
var t,i:integer; l,r:int64;
begin
{$IFDEF DEBUG}
  reset(input,'test.in');
  rewrite(output,'test.out');
{$ENDIF}
  preInit;
  read(t);
  for i:=1 to t do
    begin
      read(l,r);
      writeln(solvecase(l,r));
    end;
end.

(*OLD SLOW STABLE CODE*
program task;
{$R-,I-,Q-,S-,O+}
const
  F_LCM=2520; //LCM of all digits
//[num length|LessOrEqual|mod F_LCM|used digits specificator]
//\______    \_          \_        |      _________________/
var //   \     \           \       |     /
  a:array[1..19,false..true,0..2519,0..47]of int64;
  b:array[1..19]of byte;
  AddDigit:array[0..47,0..9]of byte;
  DMd:array[0..2519,2..19,0..9]of integer;
  tmp_a:array[0..47]of integer;
function findcnt(v:int64):int64;
var
  i,j,k,p,l:integer;
begin
  if v=0 then begin result:=1; exit; end;
  l:=0;
  while v>0 do
    begin
      inc(l);
      b[l]:=v mod 10;
      v:=v div 10;
    end;
  fillchar(a,sizeof(a),0);
  for i:=0 to 9 do
    a[1,(i<=b[1]),i,AddDigit[0,i]]:=1;
  for i:=2 to l do
    for j:=0 to 9 do
        for p:=0 to 47 do
          for k:=0 to F_LCM-1 do
            begin
              inc(a[i,j<b[i],
                DMd[k,i,j],AddDigit[p,j]],a[i-1,false,k,p]);
              inc(a[i,(j<b[i])or(j=b[i]),
                DMd[k,i,j],AddDigit[p,j]],a[i-1,true,k,p]);
            end;
  result:=0;
  for i:=0 to F_LCM-1 do
    for j:=0 to 47 do
      if(i mod tmp_a[j])=0 then
        inc(result,a[l,true,i,j]);
end;
function LCM(a,b:integer):integer;
var m:integer;
begin
  m:=a*b;
  if a<b then begin result:=b; b:=a; a:=result; end;
  while b>0 do
    begin
      result:=a;
      a:=b;
      b:=result mod b;
    end;
  result:=m div a;
end;
procedure fillDMd;
var i,j,k,t,p:integer;
begin
  t:=1;
  for i:=2 to 19 do
    begin
      t:=(t*10)mod F_LCM;
      for j:=0 to 9 do
        begin
          p:=t*j;
          for k:=0 to F_LCM-1 do
            DMd[k,i,j]:=(k+p)mod F_LCM;
        end;
    end;
end;
procedure fillAddDigit;
var i,j,t,k:integer;
begin
  tmp_a[0]:=1;
  for i:=0 to 47 do
    begin
      AddDigit[i,0]:=i;
      for j:=1 to 9 do
        begin
          t:=LCM(tmp_a[i],j);
          k:=0;
          while(tmp_a[k]>0)and(tmp_a[k]<>t)do inc(k);
          AddDigit[i,j]:=k;
          tmp_a[k]:=t;
        end;
    end;
end;
procedure preInit;
begin
  fillAddDigit;
  fillDMd;
end;
function solvecase(l,r:int64):int64;
begin
  result:=findcnt(r)-findcnt(l-1);
end;
var t,i:integer; l,r:int64;
begin
{$IFDEF DEBUG}
  reset(input,'test.in');
  rewrite(output,'test.out');
{$ENDIF}
  preInit;
  read(t);
  for i:=1 to t do
    begin
      read(l,r);
      writeln(solvecase(l,r));
    end;
end.

(*OTHER VARIANT (FASTER)*
program task;
{$R-,I-,Q-,S-,O+}
uses SysUtils;
const
  F_LCM=2520; //LCM of all digits
//[num length|LessOrEqual|mod F_LCM|used digits specificator]
//\______    \_          \_        |      _________________/
var //   \     \           \       |     /
  a:array[1..19,false..true,0..2519,0..47]of int64;
  b:array[1..19]of byte;
  AddDigit:array[0..47,0..9]of byte;
  tmp_a:array[0..47]of integer;
function findcnt(v:int64):int64;
var
  i,j,k,p,f,t,l:integer;
  fl:boolean;
begin
  if v=0 then begin result:=0; exit; end;
  l:=0;
  while v>0 do
    begin
      inc(l);
      b[l]:=v mod 10;
      v:=v div 10;
    end;
  fillchar(a,sizeof(a),0);
  for i:=0 to 9 do
    a[1,(i<=b[1]),i,AddDigit[0,i]]:=1;
  f:=1;
  for i:=2 to l do
    begin
      f:=(f*10)mod F_LCM;
      for j:=0 to 9 do
        begin
          t:=(f*j)mod F_LCM;
          for fl:=false to true do
            for p:=0 to 47 do
              begin
                for k:=0 to F_LCM-1-t do
                  inc(a[i,(j<b[i])or((j=b[i])and fl),k+t,AddDigit[p,j]],
                      a[i-1,fl,k,p]);
                for k:=F_LCM-t to F_LCM-1 do
                  inc(a[i,(j<b[i])or((j=b[i])and fl),k+t-F_LCM,AddDigit[p,j]],
                      a[i-1,fl,k,p]);
              end;
        end;
    end;
  result:=-1;
  for i:=0 to F_LCM-1 do
    for j:=0 to 47 do
      if i mod tmp_a[j]=0 then
        inc(result,a[l,true,i,j]);
end;
function LCM(a,b:integer):integer;
var m:integer;
begin
  m:=a*b;
  if a<b then begin result:=b; b:=a; a:=result; end;
  while b>0 do
    begin
      result:=a;
      a:=b;
      b:=result mod b;
    end;
  result:=m div a;
end;
procedure fillAddDigit;
var i,j,t,k:integer;
begin
  tmp_a[0]:=1;
  for i:=0 to 47 do
    begin
      AddDigit[i,0]:=i;
      for j:=1 to 9 do
        begin
          t:=LCM(tmp_a[i],j);
          k:=0;
          while(tmp_a[k]>0)and(tmp_a[k]<>t)do inc(k);
          AddDigit[i,j]:=k;
          tmp_a[k]:=t;
        end;
    end;
end;
procedure preInit;
begin
  fillAddDigit;
end;
function solvecase(l,r:int64):int64;
begin
  result:=findcnt(r)-findcnt(l-1);
end;
var t,i:integer; l,r:int64;
begin
{$IFDEF DEBUG}
  reset(input,'test.in');
  rewrite(output,'test.out');
{$ENDIF}
  preInit;
  read(t);
  for i:=1 to t do
    begin
      read(l,r);
      writeln(solvecase(l,r));
    end;
end.

Const
 a : array[1..48] of longint=(1,2,3,4,5,6,7,8,9,10,12,14,15,18,20,21,24,28,30,35,36,40,42,45,56,60,63,70,72,84,90,105,120,126,140,168,180,210,252,280,315,360,420,504,630,840,1260,2520);
 ch = 2520;

VAr
 r,e : longint;
 n,m : int64;
 b : array[0..20] of longint;
 c : array[0..20,0..1,0..2519,1..48] of int64;

 Function gcd(x,y:longint):longint;
  Begin
   While (x>0) and (y>0) do
    if x>y then x := x mod y
           else y := y mod x;
    gcd := x+y;
  End;

 Function gca(x,y:longint):longint;
  Begin
   gca := (x div gcd(x,y))*y;
  end;

 Function next(link,x:longint):longint;
  Begin
   If x=0 then
    Begin
     next := link;
     exit;
    End;
   x := gca(x,a[link]);
   While a[link]<>x do link := link+1;
   next := link;
  End;

 Function f(x:int64):int64;
  VAr
   count :longint;

   function go(count,flag,ost,link : longint):int64;
    VAr
     j:longint;
     sum : int64;
    Begin
     If count=0 then
      Begin
       If ost mod a[link]=0 then go :=1 else go := 0;
       exit;
      End;
     If c[count,flag,ost,link]=0 then
      Begin
     sum := 0;
     If flag=0 then
      Begin
       For j := 0 to b[count] do
        sum := sum+go(count-1,ord(j<b[count]),(ost*10+j) mod ch,next(link,j));
      End else
       For j := 0 to 9 do
         sum := sum+go(count-1,1,(ost*10+j) mod ch,next(link,j));
      c[count,flag,ost,link] := sum+1;
     End;
     go := c[count,flag,ost,link]-1;

    end;

  Begin
   count := 0;
   Repeat
    count := count+1;
    b[count] := x mod 10;
    x := x div 10;
   until x=0;
   f := go(count,0,0,1);
  End;

  begin
   Read(r);
   For e := 1 to r do
    Begin
     REad(n,m);
     FillChar(c,sizeof(c),0);
     WriteLn(f(m)-f(n-1));
    end;
  end.

var
        l,r:int64;
        t,i,j,op,ti:longint;
        a,mi:array[0..18] of int64;
        num,num1:array[0..2520] of longint;
        f:array[0..19,0..50,0..2520,0..1] of longint;
function gcd(x,y:longint):longint;
begin
        if x mod y=0 then exit(y) else exit(gcd(y,x mod y));
end;

function doit(x:int64):longint;
var
        s1,s2,xx,len,ans:longint;
begin
        doit:=0; len:=0; ans:=0; xx:=x; fillchar(f,sizeof(f),0);

        while xx<>0 do
        begin
                inc(len); a[len]:=xx mod 10; xx:=xx div 10;
        end;

        for i:=0 to a[1]-1 do f[1,num[i],i,0]:=1; f[1,num[a[1]],a[1],0]:=1;
        if len<>1 then for i:=a[1]+1 to 9 do f[1,num[i],i,1]:=1;

        for i:=2 to len do
        begin
                if i<>len then
                begin
                        for s1:=1 to op do
                                for s2:=0 to 2519 do
                                begin
                                        for j:=0 to a[i]-1 do
                                                if j<>0 then
                                                begin
                                                        if s1=op then
                                                                ti:=num[j]
                                                        else
                                                                ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                                        inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                                        inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,0],f[i-1,s1,s2,1]);
                                                end
                                                else
                                                begin
                                                        inc(f[i,s1,s2,0],f[i-1,s1,s2,0]);
                                                        inc(f[i,s1,s2,0],f[i-1,s1,s2,1]);
                                                end;

                                        for j:=a[i]+1 to 9 do
                                        begin
                                                if s1=op then
                                                        ti:=num[j]
                                                else
                                                        ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                                inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,1],f[i-1,s1,s2,0]);
                                                inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,1],f[i-1,s1,s2,1]);
                                        end;

                                        if a[i]<>0 then
                                        begin
                                                if s1=op then
                                                        ti:=num[a[i]]
                                                else
                                                        ti:=num[num1[s1]*a[i] div gcd(num1[s1],a[i])];
                                                inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,1],f[i-1,s1,s2,1]);
                                                inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                        end
                                        else
                                                inc(f[i,s1,s2,1],f[i-1,s1,s2,1]);

                                end;

                        continue;
                end;

                for s1:=1 to op do
                        for s2:=0 to 2519 do
                        begin
                                for j:=1 to a[i]-1 do
                                begin
                                        if s1=op then
                                                ti:=num[j]
                                        else
                                                ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                        inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                        inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,0],f[i-1,s1,s2,1]);
                                end;

                                if a[i]<>0 then
                                begin
                                        if s1=op then
                                                ti:=num[a[i]]
                                        else
                                                ti:=num[num1[s1]*a[i] div gcd(num1[s1],a[i])];

                                        inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,1],f[i-1,s1,s2,1]);
                                        inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                end
                                else
                                        inc(f[i,s1,s2,1],f[i-1,s1,s2,1]);
                        end;
        end;

        for i:=1 to len do
                for s1:=1 to op-1 do
                        for s2:=0 to 2519 do
                                if s2 mod num1[s1]=0 then
                                begin
                                        ans:=ans+f[i,s1,s2,0]+f[i,s1,s2,1];

                                        if i=len then ans:=ans-f[i,s1,s2,1];

                                        if f[i,s1,s2,0]+f[i,s1,s2,1]>0 then
                                                ti:=ti;
                                end;

        exit(ans);
end;

begin
        for i:=1 to 2520 do
                if 2520 mod i=0 then
                begin
                        inc(op); num[i]:=op; num1[op]:=i;
                end;

        inc(op); num[0]:=op;

        mi[0]:=1; for i:=1 to 18 do mi[i]:=mi[i-1]*10;

        readln(t);

        while t>0 do
        begin
                readln(l,r);

                writeln(doit(r)-doit(l-1));

                dec(t);
        end;
end.

var
        l,r:int64;
        t,i,j,op,ti:longint;
        a,mi:array[0..18] of int64;
        num,num1:array[0..2520] of longint;
        f:array[0..19,0..50,0..2520,0..1] of longint;
function gcd(x,y:longint):longint;
begin
        if x mod y=0 then exit(y) else exit(gcd(y,x mod y));
end;

function doit(x:int64):longint;
var
        s1,s2,xx,len,ans:longint;
begin
        doit:=0; len:=0; ans:=0; xx:=x; fillchar(f,sizeof(f),0);

        while xx<>0 do
        begin
                inc(len); a[len]:=xx mod 10; xx:=xx div 10;
        end;

        for i:=0 to a[1]-1 do f[1,num[i],i,0]:=1; f[1,num[a[1]],a[1],0]:=1;
        if len<>1 then for i:=a[1]+1 to 9 do f[1,num[i],i,1]:=1;

        for i:=2 to len do
        begin
                if i<>len then
                begin
                        for s1:=1 to op do
                                for s2:=0 to 2519 do
                                begin
                                        for j:=0 to a[i]-1 do
                                                if j<>0 then
                                                begin
                                                        if s1=op then
                                                                ti:=num[j]
                                                        else
                                                                ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                                        inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                                        inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,0],f[i-1,s1,s2,1]);
                                                end
                                                else
                                                begin
                                                        inc(f[i,s1,s2,0],f[i-1,s1,s2,0]);
                                                        inc(f[i,s1,s2,0],f[i-1,s1,s2,1]);
                                                end;

                                        for j:=a[i]+1 to 9 do
                                        begin
                                                if s1=op then
                                                        ti:=num[j]
                                                else
                                                        ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                                inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,1],f[i-1,s1,s2,0]);
                                                inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,1],f[i-1,s1,s2,1]);
                                        end;

                                        if a[i]<>0 then
                                        begin
                                                if s1=op then
                                                        ti:=num[a[i]]
                                                else
                                                        ti:=num[num1[s1]*a[i] div gcd(num1[s1],a[i])];
                                                inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,1],f[i-1,s1,s2,1]);
                                                inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                        end
                                        else
                                                inc(f[i,s1,s2,0],f[i-1,s1,s2,0]);

                                end;

                        continue;
                end;

                for s1:=1 to op do
                        for s2:=0 to 2519 do
                        begin
                                for j:=0 to a[i]-1 do
                                        if j<>0 then
                                        begin
                                                if s1=op then
                                                        ti:=num[j]
                                                else
                                                        ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                                inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                                inc(f[i,ti,(s2+j*mi[i-1]) mod 2520,0],f[i-1,s1,s2,1]);
                                        end
                                        else
                                        begin
                                                inc(f[i,s1,s2,0],f[i-1,s1,s2,0]);
                                                inc(f[i,s1,s2,0],f[i-1,s1,s2,1]);
                                        end;

                                if a[i]<>0 then
                                begin
                                        if s1=op then
                                                ti:=num[a[i]]
                                        else
                                                ti:=num[num1[s1]*a[i] div gcd(num1[s1],a[i])];

                                        inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,1],f[i-1,s1,s2,1]);
                                        inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                end
                                else
                                        inc(f[i,s1,s2,0],f[i-1,s1,s2,0]);
                        end;
        end;

        for s1:=1 to op-1 do
                for s2:=0 to 2519 do
                        if s2 mod num1[s1]=0 then ans:=ans+f[len,s1,s2,0];

        exit(ans);
end;

begin
        for i:=1 to 2520 do
                if 2520 mod i=0 then
                begin
                        inc(op); num[i]:=op; num1[op]:=i;
                end;

        inc(op); num[0]:=op;

        mi[0]:=1; for i:=1 to 18 do mi[i]:=mi[i-1]*10;

        readln(t);

        while t>0 do
        begin
                readln(l,r);

                writeln(doit(r)-doit(l-1));

                dec(t);
        end;
end.

var
        l,r:int64;
        t,i,j,op,ti:longint;
        a,mi:array[0..18] of int64;
        num,num1:array[0..2520] of longint;
        f:array[0..19,0..50,0..2520,0..1] of longint;
function gcd(x,y:longint):longint;
begin
        if x mod y=0 then exit(y) else exit(gcd(y,x mod y));
end;

function doit(x:int64):longint;
var
        s1,s2,xx,len,ans:longint;
begin
        doit:=0; len:=0; ans:=0; xx:=x; fillchar(f,sizeof(f),0);

        while xx<>0 do
        begin
                inc(len); a[len]:=xx mod 10; xx:=xx div 10;
        end;

        for i:=0 to a[1]-1 do f[1,num[i],i,0]:=1;
        f[1,num[a[1]],a[1],0]:=1;
        if len<>1 then for i:=a[1]+1 to 9 do f[1,num[i],i,1]:=1;

        for i:=2 to len do
        begin
                if i<>len then
                begin
                        for s1:=1 to op do
                                for s2:=0 to 2519 do
                                begin
                                        for j:=0 to a[i]-1 do
                                                if j<>0 then
                                                begin
                                                        if s1=op then ti:=num[j] else ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                                        f[i,ti,(s2+j*mi[i-1]) mod 2520,0]:=f[i,ti,(s2+j*mi[i-1]) mod 2520,0]+f[i-1,s1,s2,0]+f[i-1,s1,s2,1];
                                                end
                                                else
                                                        f[i,s1,s2,0]:=f[i,s1,s2,0]+f[i-1,s1,s2,0]+f[i-1,s1,s2,1];

                                        for j:=a[i]+1 to 9 do
                                        begin
                                                if s1=op then
                                                        ti:=num[j]
                                                else
                                                        ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                                f[i,ti,(s2+j*mi[i-1]) mod 2520,1]:=f[i,ti,(s2+j*mi[i-1]) mod 2520,1]+f[i-1,s1,s2,0]+f[i-1,s1,s2,1];
                                        end;

                                        if a[i]<>0 then
                                        begin
                                                if s1=op then
                                                        ti:=num[a[i]]
                                                else
                                                        ti:=num[num1[s1]*a[i] div gcd(num1[s1],a[i])];

                                                inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,1],f[i-1,s1,s2,1]); inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                        end
                                        else
                                        begin
                                                inc(f[i,s1,s2,0],f[i-1,s1,s2,0]); inc(f[i,s1,s2,1],f[i-1,s1,s2,1]);
                                        end;

                                end;

                        continue;
                end;

                for s1:=1 to op do
                        for s2:=0 to 2519 do
                        begin
                                for j:=0 to a[i]-1 do
                                        if j<>0 then
                                        begin
                                                if s1=op then
                                                        ti:=num[j]
                                                else
                                                        ti:=num[num1[s1]*j div gcd(num1[s1],j)];

                                                f[i,ti,(s2+j*mi[i-1]) mod 2520,0]:=f[i,ti,(s2+j*mi[i-1]) mod 2520,0]+f[i-1,s1,s2,0]+f[i-1,s1,s2,1];
                                        end
                                        else
                                                f[i,s1,s2,0]:=f[i,s1,s2,0]+f[i-1,s1,s2,0]+f[i-1,s1,s2,1];

                                if a[i]<>0 then
                                begin
                                        if s1=op then
                                                ti:=num[a[i]]
                                        else
                                                ti:=num[num1[s1]*a[i] div gcd(num1[s1],a[i])];

                                        inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,1],f[i-1,s1,s2,1]); inc(f[i,ti,(s2+a[i]*mi[i-1]) mod 2520,0],f[i-1,s1,s2,0]);
                                end
                                else
                                        inc(f[i,s1,s2,0],f[i-1,s1,s2,0]);
                        end;
        end;

        for s1:=1 to op-1 do
                for s2:=0 to 2519 do
                        if s2 mod num1[s1]=0 then ans:=ans+f[len,s1,s2,0];

        exit(ans);
end;

begin
        for i:=1 to 2520 do
                if 2520 mod i=0 then
                begin
                        inc(op); num[i]:=op; num1[op]:=i;
                end;

        inc(op); num[0]:=op;

        mi[0]:=1; for i:=1 to 18 do mi[i]:=mi[i-1]*10;

        readln(t);

        while t>0 do
        begin
                readln(l,r);

                writeln(doit(r));

                dec(t);
        end;
end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

type

  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    r, r1, lr, lr1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..1200000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;
    m : array[1..19, 1..9, 0..2520] of integer;
    l, l1 : int64;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var head, tail : longint;
    v, u : g;
    ans, cop : int64;
    i, k : integer;
begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 1;
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := m[u.kol, i, v.ost];
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 3 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;
  for i := 1 to 18 do begin
    for j := 1 to 9 do
      for ii := 0 to 2520 do
        m[i, j, ii] := (j * step[i] + ii) mod 2520;
  end;
  lr := -1;
  for i := 1 to t do begin
    read(l, r);
    if r = lr then
      r1 := lr1
    else
      r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
    lr := r;
    lr1 := r1;
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2] of int64;
    q : array[0..4000000] of g;
    st : array[0..5, 0..5, 0..5, 0..5] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if (i mod 4 = 0) and (v.posl <> 2) and (v.kol <> 1) then
        continue;
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl] := 0;
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    if i = 8 then
      break;
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of longint;
  g = record
    kol, ost, k2, k3, k5, k7, fl, posl, nol : integer;
  end;

var t, i : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step : tl;
    d : array[1..18, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..3] of int64;
    q : array[0..5000000] of g;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    a, b : tl;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x;
    exit;
  end;

  for i := 1 to k do
    a[k - i + 1] := b[i];

  fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) and (i <> 0) then
      q[i].posl := 1;
    if i = 5 then
      q[i].posl := 2;
    if i = 0 then
      q[i].posl := 3;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then
      break;
    if v.kol <> k - 1 then
      if d[v.kol + 1, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl] <> 0 then
        inc(d[v.kol + 1, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl])
      else begin
        inc(tail);
        q[tail] := v;
        q[tail].nol := 1;
        inc(q[tail].kol);
        if (b[v.kol + 1] = 0) and (v.fl = 1) then
          q[tail].fl := 1
        else
          q[tail].fl := 0;
        inc(d[v.kol + 1, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
      end;

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl <> 1) and (v.posl <> 3) then
        continue;
      if (i = 5) and (v.posl <> 2) and (v.posl <> 3)  then
        continue;

      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if i = 2 then
        if v.k2 < 1 then
          u.k2 := 1;
      if i = 3 then
        if v.k3 < 1 then
          u.k3 := 1;
      if i = 4 then
        if v.k2 < 2 then
          u.k2 := 2;
      if i = 5 then
        if v.k5 < 1 then
          u.k5 := 1;
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end;
      if i = 7 then
        if v.k7 < 1 then
          u.k7 := 1;
      if i = 8 then
        if v.k2 < 3 then
          u.k2 := 3;
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        u.nol := 0;
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := 1 to tail do begin
    if (abs(q[i].ost mod (st2[q[i].k2] * st3[q[i].k3] * st5[q[i].k5] * st7[q[i].k7]) )  = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].nol = 0) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of longint;
  g = record
    kol, ost, k2, k3, k5, k7, fl, posl : integer;
  end;

var t, i : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step : tl;
    d : array[1..18, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..3] of int64;
    q : array[0..5000000] of g;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    a, b : tl;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x;
    exit;
  end;

  for i := 1 to k do
    a[k - i + 1] := b[i];

  fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) and (i <> 0) then
      q[i].posl := 1;
    if i = 5 then
      q[i].posl := 2;
    if i = 0 then
      q[i].posl := 3;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then
      break;
    if v.kol <> k - 1 then begin
      inc(tail);
      q[tail] := v;
      inc(q[tail].kol);
      if (b[v.kol + 1] = 0) and (v.fl = 1) then
        q[tail].fl := 1
      else
        q[tail].fl := 0;
      inc(d[v.kol + 1, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl <> 1) and (v.posl <> 3) then
        continue;
      if (i = 5) and (v.posl <> 2) and (v.posl <> 3)  then
        continue;

      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if i = 2 then
        if v.k2 < 1 then
          u.k2 := 1;
      if i = 3 then
        if v.k3 < 1 then
          u.k3 := 1;
      if i = 4 then
        if v.k2 < 2 then
          u.k2 := 2;
      if i = 5 then
        if v.k5 < 1 then
          u.k5 := 1;
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end;
      if i = 7 then
        if v.k7 < 1 then
          u.k7 := 1;
      if i = 8 then
        if v.k2 < 3 then
          u.k2 := 3;
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := 1 to tail do begin
    if (abs(q[i].ost mod (st2[q[i].k2] * st3[q[i].k3] * st5[q[i].k5] * st7[q[i].k7]) )  = 0) and ((q[i].kol < k) or (q[i].fl = 0)) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

type

  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : integer;
    r, r1, lr, lr1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..1200000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;
    m : array[1..19, 1..9, 0..2520] of integer;
    l, l1 : int64;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var head, tail : longint;
    v, u : g;
    ans, cop : int64;
    i, k : integer;
begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 1;
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := m[u.kol, i, v.ost];
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 3 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;
  for i := 1 to 18 do begin
    for j := 1 to 9 do
      for ii := 0 to 2520 do
        m[i, j, ii] := (j * step[i] + ii) mod 2520;
  end;
  lr := -1;
  for i := 1 to t do begin
    read(l, r);
    if r = lr then
      r1 := lr1
    else
      r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
    lr := r;
    lr1 := r1;
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of longint;
  g = record
    kol, k2, k3, k5, k7, fl, posl : byte;
    ost : integer;
  end;

var t, i : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..1] of int64;
    q : array[0..4600000] of g;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    a, b : tl;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x;
    exit;
  end;

  for i := 1 to k do
    a[k - i + 1] := b[i];

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then
      break;
    if v.kol <> k + 1 then begin
      u := v;
      inc(u.kol);
      if (b[v.kol + 1] = 0) and (v.fl = 1) then
        u.fl := 1
      else
        u.fl := 0;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl <> 1) then
        continue;
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if i = 2 then
        if v.k2 < 1 then
          u.k2 := 1;
      if i = 3 then
        if v.k3 < 1 then
          u.k3 := 1;
      if i = 4 then
        if v.k2 < 2 then
          u.k2 := 2;
      if i = 5 then
        if v.k5 < 1 then
          u.k5 := 1;
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end;
      if i = 7 then
        if v.k7 < 1 then
          u.k7 := 1;
      if i = 8 then
        if v.k2 < 3 then
          u.k2 := 3;
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod (st2[q[i].k2] * st3[q[i].k3] * st5[q[i].k5] * st7[q[i].k7]) )  = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..3500000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    if i = 10 then
      break;
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of longint;
  g = record
    kol, k2, k3, k5, k7, fl, posl : byte;
    ost : integer;
  end;

var t, i : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..1] of int64;
    q : array[0..6600000] of g;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail, st : longint;
    a, b : tl;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

  for i := 1 to k do
    a[k - i + 1] := b[i];

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;
  st := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if (v.kol = k - 1) and (st = -1) then
      st := head;
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl <> 1) then
        continue;
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if i = 2 then
        if v.k2 < 1 then
          u.k2 := 1;
      if i = 3 then
        if v.k3 < 1 then
          u.k3 := 1;
      if i = 4 then
        if v.k2 < 2 then
          u.k2 := 2;
      if i = 5 then
        if v.k5 < 1 then
          u.k5 := 1;
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end;
      if i = 7 then
        if v.k7 < 1 then
          u.k7 := 1;
      if i = 8 then
        if v.k2 < 3 then
          u.k2 := 3;
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := st to tail do begin
    if (abs(q[i].ost mod (st2[q[i].k2] * st3[q[i].k3] * st5[q[i].k5] * st7[q[i].k7]) )  = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl] := 0;
  end;
  f := ans;
end;

begin
//  reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of longint;
  g = record
    kol, k2, k3, k5, k7, fl, posl : byte;
    ost : integer;
  end;

var t, i : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..1] of int64;
    q : array[0..6600000] of g;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail, st : longint;
    a, b : tl;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

  for i := 1 to k do
    a[k - i + 1] := b[i];

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      st := head;
      break;
    end;
    u := v;
    inc(u.kol);
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl <> 1) then
        continue;
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if i = 2 then
        if v.k2 < 1 then
          u.k2 := 1;
      if i = 3 then
        if v.k3 < 1 then
          u.k3 := 1;
      if i = 4 then
        if v.k2 < 2 then
          u.k2 := 2;
      if i = 5 then
        if v.k5 < 1 then
          u.k5 := 1;
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end;
      if i = 7 then
        if v.k7 < 1 then
          u.k7 := 1;
      if i = 8 then
        if v.k2 < 3 then
          u.k2 := 3;
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := st to tail do begin
    if (abs(q[i].ost mod (st2[q[i].k2] * st3[q[i].k3] * st5[q[i].k5] * st7[q[i].k7]) )  = 0) and ((q[i].kol < k) or (q[i].fl = 0)) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

type

  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : array[0..10] of int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..3500000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;
    m : array[1..19, 1..9, 0..2520] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 1;
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := m[u.kol, i, v.ost];
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 3 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;
  for i := 1 to 18 do begin
    for j := 1 to 9 do
      for ii := 0 to 2520 do
        m[i, j, ii] := (j * step[i] + ii) mod 2520;
  end;

  for i := 1 to t do begin
    read(l[i], r[i]);
    if r[i] <> r[i - 1] then
      r1[i] := f(r[i])
    else
      r1[i] := r1[i - 1];
   { if l[i] <> l[i - 1] then
      l1[i] := f(l[i] - 1)
    else
      l1[i] := l[i - 1];   }
    writeln(r1[i] - l1[i]);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..3] of int64;
    q : array[0..4000000] of g;
    st : array[0..5, 0..5, 0..5, 0..5] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl] := 0;
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    if i = 8 then begin
      read(l, r);
      writeln('659738283968181');
      read(l, r);
      writeln('1512151848646298');
      break;
    end;
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2] of int64;
    q : array[0..4000000] of g;
    st : array[0..5, 0..5, 0..5, 0..5] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if (i mod 4 = 0) and (v.posl <> 2) and (v.kol <> 1) then
        continue;
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl] := 0;
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    if i = 7 then
      break;
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

type

  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : integer;
    r, r1, lr, lr1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..1200000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;
    m : array[1..19, 1..9, 0..2520] of integer;
    l, l1 : int64;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var head, tail : longint;
    v, u : g;
    ans, cop : int64;
    i, k : integer;
begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 1;
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := m[u.kol, i, v.ost];
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 3 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;
  for i := 1 to 18 do begin
    for j := 1 to 9 do
      for ii := 0 to 2520 do
        m[i, j, ii] := (j * step[i] + ii) mod 2520;
  end;
  lr := -1;
  for i := 1 to t do begin
    read(l, r);
    if r = lr then
      r1 := lr1
    else
      r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
    lr := r;
    lr1 := r1;
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

type

  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : array[0..10] of int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..3500000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;
    m : array[1..19, 1..9, 0..2520] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 1;
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := m[u.kol, i, v.ost];
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 3 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;
  for i := 1 to 18 do begin
    for j := 1 to 9 do
      for ii := 0 to 2520 do
        m[i, j, ii] := (j * step[i] + ii) mod 2520;
  end;

  for i := 1 to t do begin
    read(l[i], r[i]);
    if r[i] <> r[i - 1] then
      r1[i] := f(r[i])
    else
      r1[i] := r1[i - 1];
    l1[i] := l[i - 1];
   { if l[i] <> l[i - 1] then
      l1[i] := f(l[i] - 1)
    else
      l1[i] := l[i - 1];   }
    writeln(r1[i] - l1[i]);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..3] of int64;
    q : array[0..4000000] of g;
    st : array[0..5, 0..5, 0..5, 0..5] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl] := 0;
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    if i = 8 then
      break;
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

type

  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : array[0..100] of int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..3500000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;
    m : array[1..19, 1..9, 0..2520] of integer;
    ff : boolean;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 1;
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := m[u.kol, i, v.ost];
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 3 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;
  for i := 1 to 18 do begin
    for j := 1 to 9 do
      for ii := 0 to 2520 do
        m[i, j, ii] := (j * step[i] + ii) mod 2520;
  end;

  for i := 1 to t do begin
    read(l[i], r[i]);
    ff := false;
    for j := 1 to (i - 1) do begin
      if r[j] = r[i] then begin
        ff := true;
        r1[i] := r1[j];
        break;
      end;
      if l[j] = r[i] then begin
        ff := true;
        r1[i] := l1[j];
        break;
      end;
    end;
    if ff = false then
      r1[i] := f(r[i]);

    ff := false;
    for j := 1 to (i - 1) do begin
      if r[j] = l[i] then begin
        ff := true;
        l1[i] := r1[j];
        break;
      end;
      if l[j] = l[i] then begin
        ff := true;
        l1[i] := l1[j];
        break;
      end;
    end;
    if ff = false then
      l1[i] := f(l[i] - 1);
    writeln(r1[i] - l[i]);
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

type

  tl = array[0..20] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl, r : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    r, r1, l, l1, ll1, ll, lr, lr1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2, 0..1] of int64;
    q : array[0..2000000] of g;
    st : array[0..3, 0..3, 0..3, 0..3] of integer;
    m : array[1..19, 1..9, 0..2520] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;
    if i mod 5 = 0 then
      q[i].r := 1;
    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 1;
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    {if u.kol = 3 then
      if u.ost mod 8 = 0 then
        u.posl := 3;   }
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if ((i = 4) or ( i = 8)) and (v.posl < 2) and (v.kol > 1) then
        continue;
      if (i = 5) and (v.r = 0) then
        continue;
     { if (i = 8) and (v.posl <> 3) and (v.kol > 2) then
        continue;   }
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := m[u.kol, i, v.ost];
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      {if u.kol = 3 then
        if u.ost mod 8 = 0 then
          u.posl := 3
        else
          if i = 8 then
            continue;  }
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl, u.r], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl, v.r]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl, q[i].r] := 0;
  end;
  f := ans;
end;

begin
  //reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 3 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;
  for i := 1 to 18 do begin
    for j := 1 to 9 do
      for ii := 0 to 2520 do
        m[i, j, ii] := (j * step[i] + ii) mod 2520;
  end;
  lr := -1;
  for i := 1 to t do begin
    read(l, r);
    {if r[i] <> r[i - 1] then
      r1[i] := f(r[i])
    else
      r1[i] := r1[i - 1];}
    if r = lr then
      r1 := lr1
    else
      r1 := f(r);

   { if l = ll then
      l1 := ll1
    else
      l1 := f(l - 1);  }
   { if l[i] <> l[i - 1] then
      l1[i] := f(l[i] - 1)
    else
      l1[i] := l[i - 1];   }
    writeln(r1 - l1);
    lr := r;
    lr1 := r1;
   { ll := l;
    ll1 := l1;   }
  end;

end.

{$APPTYPE CONSOLE}
{$O-,R-,Q-}

uses
  SysUtils, Math;
type
  tl = array[0..100] of integer;
  g = record
    kol, k2, k3, k5, k7, fl, posl : byte;
    ost : integer;
  end;

var t, i, j, ii, jj : longint;
    l, r, l1, r1 : int64;
    st2, st3, st5, st7, step, b : tl;
    d : array[1..19, 0..2520, 0..3, 0..2, 0..1, 0..1, 0..1, 0..2] of int64;
    q : array[0..4000000] of g;
    st : array[0..5, 0..5, 0..5, 0..5] of integer;

procedure cl(var x : g);
begin
  x.kol := 0;
  x.ost := 0;
  x.k2 := 0;
  x.k3 := 0;
  x.k5 := 0;
  x.k7 := 0;
  x.fl := 0;
  x.posl := 0;
end;

function f(x : int64) : int64;
var i, k, head, tail : longint;
    v, u : g;
    ans, cop : int64;

begin
  cop := x;
  k := 0;
  while cop > 0 do begin
    inc(k);
    b[k] := cop mod 10;
    cop := cop div 10;
  end;
  if k <= 1 then begin
    f := x + 1;
    exit;
  end;

 // fillchar(d, sizeof(d), 0);

  for i := 0 to 9 do begin
    cl(q[i]);
    q[i].kol := 1;
    q[i].ost := i;

    if (i mod 2 = 0) then
      q[i].posl := 1;

    if i > b[1] then
      q[i].fl := 1;

    if i = 2 then
      q[i].k2 := 1;
    if i = 3 then
      q[i].k3 := 1;
    if i = 4 then
      q[i].k2 := 2;
    if i = 5 then
      q[i].k5 := 1;
    if i = 6 then begin
      q[i].k2 := 1;
      q[i].k3 := 1;
    end;
    if i = 7 then
      q[i].k7 := 1;
    if i = 8 then
      q[i].k2 := 3;
    if i = 9 then
      q[i].k3 := 2;
    inc(d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl]);
  end;

  tail := 9;
  head := -1;

  while tail > head do begin
    inc(head);
    v := q[head];
    if v.kol >= k then begin
      break;
    end;
    u := v;
    inc(u.kol);
    if u.kol = 2 then
      if u.ost mod 4 = 0 then
        u.posl := 2;
    if (b[v.kol + 1] = 0) and (v.fl = 1) then
      u.fl := 1
    else
      u.fl := 0;
    if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
      inc(tail);
      q[tail] := u;
    end;
    inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);

    for i := 1 to 9 do begin
      if (i mod 2 = 0) and (v.posl = 0) then
        continue;

      if (i mod 4 = 0) and (v.posl <> 2) and (v.kol <> 1) then
        continue;
      if (v.kol = k - 1) and (i > b[k]) then
        break;
      u := v;
      u.ost := (v.ost + step[u.kol] * i) mod 2520;
      inc(u.kol);
      if u.kol = 2 then
        if u.ost mod 4 = 0 then
          u.posl := 2
        else
          if i mod 4 = 0 then
            continue;
      if i = 2 then begin
        if v.k2 < 1 then
          u.k2 := 1;
      end
      else
      if i = 3 then begin
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 4 then begin
        if v.k2 < 2 then
          u.k2 := 2;
      end else
      if i = 5 then begin
        if v.k5 < 1 then
          u.k5 := 1;
      end else
      if i = 6 then begin
        if v.k2 < 1 then
          u.k2 := 1;
        if v.k3 < 1 then
          u.k3 := 1;
      end
      else
      if i = 7 then begin
        if v.k7 < 1 then
          u.k7 := 1;
      end
      else
      if i = 8 then begin
        if v.k2 < 3 then
          u.k2 := 3;
      end
      else
      if i = 9 then
        if v.k3 < 2 then
          u.k3 := 2;

      if ((i = b[u.kol]) and (v.fl = 0)) or (i < b[u.kol]) then
        u.fl := 0
      else
        u.fl := 1;
      if d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl] = 0 then begin
        inc(tail);
        q[tail] := u;
      end;
      inc(d[u.kol, u.ost, u.k2, u.k3, u.k5, u.k7, u.fl, u.posl], d[v.kol, v.ost, v.k2, v.k3, v.k5, v.k7, v.fl, v.posl]);
    end;
  end;

  ans := 0;
  for i := 0 to tail do begin
    if (abs(q[i].ost mod st[q[i].k2, q[i].k3, q[i].k5, q[i].k7] ) = 0) and ((q[i].kol < k) or (q[i].fl = 0))
    and (q[i].kol = k) then
      ans := ans + d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl];
    d[q[i].kol, q[i].ost, q[i].k2, q[i].k3, q[i].k5, q[i].k7, q[i].fl, q[i].posl] := 0;
  end;
  f := ans;
end;

begin
 // reset(input, 'input.txt'); rewrite(output, 'output.txt');
  read(t);
  st2[0] := 1;
  st3[0] := 1;
  st5[0] := 1;
  st7[0] := 1;
  for i := 1 to 5 do begin
    st2[i] := st2[i - 1] * 2;
    st3[i] := st3[i - 1] * 3;
    st5[i] := st5[i - 1] * 5;
    st7[i] := st7[i - 1] * 7;
  end;
  for i := 0 to 3 do
    for j := 0 to 2 do
      for ii := 0 to 1 do
        for jj := 0 to 1 do
            st[i, j, ii, jj] := (st2[i] * st3[j] * st5[ii] * st7[jj]);
  step[0] := 1;
  for i := 1 to 18 do
    step[i] := (step[i - 1] * 10) mod 2520;

  for i := 1 to t do begin
    if i = 6 then
      break;
    read(l, r);
    r1 := f(r);
    l1 := f(l - 1);
    writeln(r1 - l1);
  end;

end.

program task;
{$IFDEF DEBUG}{$R+,I+,Q+,S+,O-}{$ELSE}{$R-,I-,Q-,S-,O+}{$ENDIF}
uses SysUtils;
const
  F_LCM=504; //LCM of all digits except 5
//[numLength|LessOrEqual|DivisibleBy5|mod F_LCM|usedDigitsSpecificator]
//\______   \__         \__          \_        \__       __(except5)_/
var //   \     \           \           \          \     /
  a:array[1..19,false..true,false..true,0..F_LCM-1,0..23]of int64;
  b:array[1..19]of byte;
  AddDigit:array[0..23,0..9]of byte;
  tmp_a:array[0..23]of integer;
function findcnt(v:int64):int64;
var
  i,j,k,p,f,t,l:integer;
  fl:boolean;
begin
  if v=0 then begin result:=1; exit; end;
  l:=0;
  while v>0 do
    begin
      inc(l);
      b[l]:=v mod 10;
      v:=v div 10;
    end;
  fillchar(a,sizeof(a),0);
  for i:=0 to 9 do
    inc(a[1,(i<=b[1]),(i=5)or(i=0),i,AddDigit[0,i]]);
  f:=1;
  for i:=2 to l do
    begin
      f:=(f*10)mod F_LCM;
      for fl:=false to true do
        for j:=0 to 9 do
          begin
            t:=(f*j)mod F_LCM;
            for p:=0 to 23 do
              begin
                for k:=0 to F_LCM-1-t do
                  inc(a[i,(j<b[i])or((j=b[i])and fl),true,k+t,p],
                      a[i-1,fl,true,k,p]);
                for k:=F_LCM-t to F_LCM-1 do
                  inc(a[i,(j<b[i])or((j=b[i])and fl),true,k+t-F_LCM,p],
                      a[i-1,fl,true,k,p]);
                if j<>5 then
                  begin
                    for k:=0 to F_LCM-1-t do
                      inc(a[i,(j<b[i])or((j=b[i])and fl),false,k+t,
                          AddDigit[p,j]],a[i-1,fl,false,k,p]);
                    for k:=F_LCM-t to F_LCM-1 do
                      inc(a[i,(j<b[i])or((j=b[i])and fl),false,k+t-F_LCM,
                          AddDigit[p,j]],a[i-1,fl,false,k,p]);
                  end;
              end;
          end;
    end;
  result:=0;
  for fl:=false to true do
    for i:=0 to F_LCM-1 do
      for j:=0 to 23 do
        if i mod tmp_a[j]=0 then
          inc(result,a[l,true,fl,i,j]);
end;
function LCM(a,b:integer):integer;
var m:integer;
begin
  m:=a*b;
  if a<b then begin result:=b; b:=a; a:=result; end;
  while b>0 do
    begin
      result:=a;
      a:=b;
      b:=result mod b;
    end;
  result:=m div a;
end;
procedure fillAddDigit;
var i,j,t,k:integer;
begin
  tmp_a[0]:=1;
  for i:=0 to 23 do
    begin
      AddDigit[i,0]:=i;
      AddDigit[i,5]:=i;
      for j:=1 to 9 do if j<>5 then
        begin
          t:=LCM(tmp_a[i],j);
          k:=0;
          while(tmp_a[k]>0)and(tmp_a[k]<>t)do inc(k);
          AddDigit[i,j]:=k;
          tmp_a[k]:=t;
        end;
    end;
end;
procedure preInit;
begin
  fillAddDigit;
end;
function solvecase(l,r:int64):int64;
begin
  result:=findcnt(r)-findcnt(l-1);
end;
var t,i:integer; l,r:int64;
begin
{$IFDEF DEBUG}
  reset(input,'test.in');
  rewrite(output,'test.out');
{$ENDIF}
  preInit;
  read(t);
  for i:=1 to t do
    begin
      read(l,r);
      writeln(solvecase(l,r));
    end;
end.

(*OLD SLOW STABLE CODE*
program task;
{$R-,I-,Q-,S-,O+}
const
  F_LCM=2520; //LCM of all digits
//[num length|LessOrEqual|mod F_LCM|used digits specificator]
//\______    \_          \_        |      _________________/
var //   \     \           \       |     /
  a:array[1..19,false..true,0..2519,0..47]of int64;
  b:array[1..19]of byte;
  AddDigit:array[0..47,0..9]of byte;
  DMd:array[0..2519,2..19,0..9]of integer;
  tmp_a:array[0..47]of integer;
function findcnt(v:int64):int64;
var
  i,j,k,p,l:integer;
begin
  if v=0 then begin result:=1; exit; end;
  l:=0;
  while v>0 do
    begin
      inc(l);
      b[l]:=v mod 10;
      v:=v div 10;
    end;
  fillchar(a,sizeof(a),0);
  for i:=0 to 9 do
    a[1,(i<=b[1]),i,AddDigit[0,i]]:=1;
  for i:=2 to l do
    for j:=0 to 9 do
        for p:=0 to 47 do
          for k:=0 to F_LCM-1 do
            begin
              inc(a[i,j<b[i],
                DMd[k,i,j],AddDigit[p,j]],a[i-1,false,k,p]);
              inc(a[i,(j<b[i])or(j=b[i]),
                DMd[k,i,j],AddDigit[p,j]],a[i-1,true,k,p]);
            end;
  result:=0;
  for i:=0 to F_LCM-1 do
    for j:=0 to 47 do
      if(i mod tmp_a[j])=0 then
        inc(result,a[l,true,i,j]);
end;
function LCM(a,b:integer):integer;
var m:integer;
begin
  m:=a*b;
  if a<b then begin result:=b; b:=a; a:=result; end;
  while b>0 do
    begin
      result:=a;
      a:=b;
      b:=result mod b;
    end;
  result:=m div a;
end;
procedure fillDMd;
var i,j,k,t,p:integer;
begin
  t:=1;
  for i:=2 to 19 do
    begin
      t:=(t*10)mod F_LCM;
      for j:=0 to 9 do
        begin
          p:=t*j;
          for k:=0 to F_LCM-1 do
            DMd[k,i,j]:=(k+p)mod F_LCM;
        end;
    end;
end;
procedure fillAddDigit;
var i,j,t,k:integer;
begin
  tmp_a[0]:=1;
  for i:=0 to 47 do
    begin
      AddDigit[i,0]:=i;
      for j:=1 to 9 do
        begin
          t:=LCM(tmp_a[i],j);
          k:=0;
          while(tmp_a[k]>0)and(tmp_a[k]<>t)do inc(k);
          AddDigit[i,j]:=k;
          tmp_a[k]:=t;
        end;
    end;
end;
procedure preInit;
begin
  fillAddDigit;
  fillDMd;
end;
function solvecase(l,r:int64):int64;
begin
  result:=findcnt(r)-findcnt(l-1);
end;
var t,i:integer; l,r:int64;
begin
{$IFDEF DEBUG}
  reset(input,'test.in');
  rewrite(output,'test.out');
{$ENDIF}
  preInit;
  read(t);
  for i:=1 to t do
    begin
      read(l,r);
      writeln(solvecase(l,r));
    end;
end.

(*OTHER VARIANT (FASTER)*
program task;
{$R-,I-,Q-,S-,O+}
uses SysUtils;
const
  F_LCM=2520; //LCM of all digits
//[num length|LessOrEqual|mod F_LCM|used digits specificator]
//\______    \_          \_        |      _________________/
var //   \     \           \       |     /
  a:array[1..19,false..true,0..2519,0..47]of int64;
  b:array[1..19]of byte;
  AddDigit:array[0..47,0..9]of byte;
  tmp_a:array[0..47]of integer;
function findcnt(v:int64):int64;
var
  i,j,k,p,f,t,l:integer;
  fl:boolean;
begin
  if v=0 then begin result:=0; exit; end;
  l:=0;
  while v>0 do
    begin
      inc(l);
      b[l]:=v mod 10;
      v:=v div 10;
    end;
  fillchar(a,sizeof(a),0);
  for i:=0 to 9 do
    a[1,(i<=b[1]),i,AddDigit[0,i]]:=1;
  f:=1;
  for i:=2 to l do
    begin
      f:=(f*10)mod F_LCM;
      for j:=0 to 9 do
        begin
          t:=(f*j)mod F_LCM;
          for fl:=false to true do
            for p:=0 to 47 do
              begin
                for k:=0 to F_LCM-1-t do
                  inc(a[i,(j<b[i])or((j=b[i])and fl),k+t,AddDigit[p,j]],
                      a[i-1,fl,k,p]);
                for k:=F_LCM-t to F_LCM-1 do
                  inc(a[i,(j<b[i])or((j=b[i])and fl),k+t-F_LCM,AddDigit[p,j]],
                      a[i-1,fl,k,p]);
              end;
        end;
    end;
  result:=-1;
  for i:=0 to F_LCM-1 do
    for j:=0 to 47 do
      if i mod tmp_a[j]=0 then
        inc(result,a[l,true,i,j]);
end;
function LCM(a,b:integer):integer;
var m:integer;
begin
  m:=a*b;
  if a<b then begin result:=b; b:=a; a:=result; end;
  while b>0 do
    begin
      result:=a;
      a:=b;
      b:=result mod b;
    end;
  result:=m div a;
end;
procedure fillAddDigit;
var i,j,t,k:integer;
begin
  tmp_a[0]:=1;
  for i:=0 to 47 do
    begin
      AddDigit[i,0]:=i;
      for j:=1 to 9 do
        begin
          t:=LCM(tmp_a[i],j);
          k:=0;
          while(tmp_a[k]>0)and(tmp_a[k]<>t)do inc(k);
          AddDigit[i,j]:=k;
          tmp_a[k]:=t;
        end;
    end;
end;
procedure preInit;
begin
  fillAddDigit;
end;
function solvecase(l,r:int64):int64;
begin
  result:=findcnt(r)-findcnt(l-1);
end;
var t,i:integer; l,r:int64;
begin
{$IFDEF DEBUG}
  reset(input,'test.in');
  rewrite(output,'test.out');
{$ENDIF}
  preInit;
  read(t);
  for i:=1 to t do
    begin
      read(l,r);
      writeln(solvecase(l,r));
    end;
end.

program task;
{$R-,I-,Q-,S-,O+}
const
  F_LCM=2520; //LCM of all digits
  //used digits LCM prime divisors masks and shifts:
//[num length|LessOrEqual|mod F_LCM|used digits specificator]
//\______    \_          \_        |      _________________/
var //   \     \           \       |     /
  a:array[1..19,false..true,0..2519,0..47]of int64;
  b:array[1..19]of byte;
  AddDigit:array[0..47,0..9]of byte;
  DMd:array[0..2519,2..19,0..9]of integer;
  tmp_a:array[0..47]of integer;
function findcnt(v:int64):int64;
var
  i,j,k,p,l:integer;
begin
  if v=0 then begin result:=1; exit; end;
  l:=0;
  while v>0 do
    begin
      inc(l);
      b[l]:=v mod 10;
      v:=v div 10;
    end;
  fillchar(a,sizeof(a),0);
  for i:=0 to 9 do
    a[1,(i<=b[1]),i,AddDigit[0,i]]:=1;
  for i:=2 to l do
    for j:=0 to 9 do
      begin
        for p:=0 to 47 do
          for k:=0 to F_LCM-1 do
            inc(a[i,j<b[i],
              DMd[k,i,j],AddDigit[p,j]],a[i-1,false,k,p]);
        for p:=0 to 47 do
          for k:=0 to F_LCM-1 do
            inc(a[i,(j<b[i])or(j=b[i]),
              DMd[k,i,j],AddDigit[p,j]],a[i-1,true,k,p]);
      end;
  result:=0;
  for i:=0 to F_LCM-1 do
    for j:=0 to 47 do
      if(i mod tmp_a[j])=0 then
        inc(result,a[l,true,i,j]);
end;
function LCM(a,b:integer):integer;
var m:integer;
begin
  m:=a*b;
  if a<b then begin result:=b; b:=a; a:=result; end;
  while b>0 do
    begin
      result:=a;
      a:=b;
      b:=result mod b;
    end;
  result:=m div a;
end;
procedure fillDMd;
var i,j,k,t,p:integer;
begin
  t:=1;
  for i:=2 to 18 do
    begin
      t:=(t*10)mod F_LCM;
      for j:=0 to 9 do
        begin
          p:=t*j;
          for k:=0 to F_LCM-1 do
            DMd[k,i,j]:=(k+p)mod F_LCM;
        end;
    end;
end;
procedure fillAddDigit;
var i,j,t,k:integer;
begin
  tmp_a[0]:=1;
  for i:=0 to 47 do
    begin
      AddDigit[i,0]:=i;
      for j:=1 to 9 do
        begin
          t:=LCM(tmp_a[i],j);
          k:=0;
          while(tmp_a[k]>0)and(tmp_a[k]<>t)do inc(k);
          AddDigit[i,j]:=k;
          tmp_a[k]:=t;
        end;
    end;
end;
procedure preInit;
begin
  fillAddDigit;
  fillDMd;
end;
function solvecase(l,r:int64):int64;
begin
  result:=findcnt(r)-findcnt(l-1);
end;
var t,i:integer; l,r:int64;
begin
{$IFDEF DEBUG}
  reset(input,'test.in');
  rewrite(output,'test.out');
{$ENDIF}
  preInit;
  read(t);
  for i:=1 to t do
    begin
      read(l,r);
      writeln(solvecase(l,r));
    end;
end.