import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;


public class GoogleC {
	public String szamol(String input){
		String result="";
		String a=input;
		Integer min=Integer.parseInt(a.substring(0,a.indexOf(' ')));
		a=a.substring(a.indexOf(' ')+1);
		Integer max=Integer.parseInt(a);
		Long count=0l;
		for(Integer i=min; i<=max; i++){
			String e1=i.toString();
			Set<Integer> db=new HashSet<Integer>(); 
			for (int j=1; j<e1.length();j++){
				
				String e2=e1.substring(j)+e1.substring(0,j);
				Integer k=Integer.parseInt(e2);
				if (k>i && k<=max){
					db.add(k);
					//System.out.println(e1+"\t"+e2);
				}
				
			}
			count+=db.size();
		}
		
		return count.toString();
	}
	public static void main(String[] args) {
		GoogleC a=new GoogleC();
		//String filename="input.txt";
		//String filename="C-small-attempt0.in";
		String filename="C-large.in";
		String thisLine;
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			BufferedWriter bw= new BufferedWriter(new FileWriter(filename+".out"));
			thisLine=br.readLine();
			Integer tnum=Integer.parseInt(thisLine);
		     for(int i=0;i<tnum;i++) { // while loop begins here
		    	 	thisLine=br.readLine();
		           System.out.println(thisLine);
		           System.out.println("Case #"+(i+1)+": "+a.szamol(thisLine)+"\n");
		           bw.write("Case #"+(i+1)+": "+a.szamol(thisLine)+"\n");
		         } // end while 
		        // end try
		     br.close();
		     bw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

}

import java.util.*;
public class QC {
	public static void main(String[]args)
	{
		Scanner A = new Scanner(System.in);
		int tc = A.nextInt();
		int tc1 = tc ;
		while(tc>0)
		{
			tc--;
			int res = 0 ;
			int f = A.nextInt();
			int s = A.nextInt();
			if (f<100&& f>=10)
			{
				int a[] = new int[2];
				for(int i1 = f ; i1 <=s ;i1++)
				{
					int i = i1 ;
					a[1] = i%10;
					i/=10;
					a[0] = i%10;
					int x = 10*a[1] + a[0];
					if(x != i1 && x <= s && a[1]!=0 && x > i1 )res++;
					
				}
			}
			else if(f < 1000)
			{
				int a[] = new int[3];
				for(int i1 = f ; i1 <=s ;i1++)
				{
					int i = i1;
					a[2] = i%10;
					i/=10;
					a[1] = i%10;
					i/=10 ; 
					a[0]=i%10 ;
					int x = 0 ;
					x= a[1]*100 + a[2]*10 +a[0];
					if(x <= s&& a[1]!=0&&x!=i&& x > i1) res++;
					
					x= a[2]*100 + a[0]*10 +a[1];
					if(x <= s&& a[2]!=0&&x!=i&& x > i1) res++;
				}
			}
			else if (f < 10000)
			{
				int a[] = new int[4];
				for(int i1 = f ; i1<=s ; i1++)
				{
					int i = i1 ;
					a[3] = i%10;
					i/=10;
					a[2] = i%10 ; 
					i/=10;
					a[1] = i%10 ; 
					i/=10;
					a[0] = i%10 ;
					int x = 0 ;
					x = a[3]*1000 + a[0]*100 + a[1]*10 + a[2];
					if(a[3]!=0 && x >i1 && x <= s) res++;
					x = a[2]*1000 + a[3]*100 + a[0]*10 + a[1];
					if(a[2]!=0 && x >i1 && x <= s) res++;
					x = a[1]*1000 + a[2]*100 + a[3]*10 + a[0];
					if(a[1]!=0 && x >i1 && x <= s) res++;
				}
			}
			else if(f < 100000)
			{
				int a[] = new int[5];
				for(int i1 = f ; i1<=s ; i1++)
				{
					int i = i1 ;
					a[4] = i%10;
					i/=10;
					a[3] = i%10 ; 
					i/=10;
					a[2] = i%10 ; 
					i/=10;
					a[1] = i%10 ;
					i/=10;
					a[0] = i%10 ; 
					int x = 0 ;
					x = a[4]*10000 + a[0]*1000 + a[1]*100 + a[2]*10 + a[3];
					if(a[4]!=0 && x >i1 && x <= s) res++;
					x = a[3]*10000 + a[4]*1000 + a[0]*100 + a[1]*10 + a[2];
					if(a[3]!=0 && x >i1 && x <= s) res++;
					x = a[2]*10000 + a[3]*1000 + a[4]*100 + a[0]*10 + a[1];
					if(a[2]!=0 && x >i1 && x <= s) res++;
					x = a[1]*10000 + a[2]*1000 + a[3]*100 + a[4]*10 + a[0];
					if(a[1]!=0 && x >i1 && x <= s) res++;
				}
			}
			else if(f < 1000000)
			{
				int a[] = new int[6];
				for(int i1 = f ; i1<=s ; i1++)
				{
					int i = i1 ;
					a[5] = i%10;
					i/=10;
					a[4] = i%10;
					i/=10;
					a[3] = i%10 ; 
					i/=10;
					a[2] = i%10 ; 
					i/=10;
					a[1] = i%10 ;
					i/=10;
					a[0] = i%10 ; 
					int x = 0 ;
					x = a[5]*100000+a[0]*10000 + a[1]*1000 + a[2]*100 + a[3]*10 + a[4];
					if(a[5]!=0 && x >i1 && x <= s) res++;
					x = a[4]*100000+a[5]*10000 + a[0]*1000 + a[1]*100 + a[2]*10 + a[3];
					if(a[4]!=0 && x >i1 && x <= s) res++;
					x = a[3]*100000+a[4]*10000 + a[5]*1000 + a[0]*100 + a[1]*10 + a[2];
					if(a[3]!=0 && x >i1 && x <= s) res++;
					x = a[2]*100000+a[3]*10000 + a[4]*1000 + a[5]*100 + a[0]*10 + a[1];
					if(a[2]!=0 && x >i1 && x <= s) res++;
					x = a[1]*100000+a[2]*10000 + a[3]*1000 + a[4]*100 + a[5]*10 + a[0];
					if(a[1]!=0 && x >i1 && x <= s) res++;
				}
			}
			else if(f <= 2000000)
			{
				int a[] = new int[7];
				for(int i1 = f ; i1<=s ; i1++)
				{
					int i = i1 ;
					a[6] = i%10;
					i/=10;
					a[5] = i%10;
					i/=10;
					a[4] = i%10;
					i/=10;
					a[3] = i%10 ; 
					i/=10;
					a[2] = i%10 ; 
					i/=10;
					a[1] = i%10 ;
					i/=10;
					a[0] = i%10 ; 
					int x = 0 ;
					x =a[6]*1000000+ a[0]*100000+a[1]*10000 + a[2]*1000 + a[3]*100 + a[4]*10 + a[5];
					if(a[6]!=0 && x >i1 && x <= s) res++;
					x =a[5]*1000000+ a[6]*100000+a[0]*10000 + a[1]*1000 + a[2]*100 + a[3]*10 + a[4];
					if(a[5]!=0 && x >i1 && x <= s) res++;
					x =a[4]*1000000+ a[5]*100000+a[6]*10000 + a[0]*1000 + a[1]*100 + a[2]*10 + a[3];
					if(a[4]!=0 && x >i1 && x <= s) res++;
					x =a[3]*1000000+ a[4]*100000+a[5]*10000 + a[6]*1000 + a[0]*100 + a[1]*10 + a[2];
					if(a[3]!=0 && x >i1 && x <= s) res++;
					x =a[2]*1000000+ a[3]*100000+a[4]*10000 + a[5]*1000 + a[6]*100 + a[0]*10 + a[1];
					if(a[2]!=0 && x >i1 && x <= s) res++;
					x =a[1]*1000000+ a[2]*100000+a[3]*10000 + a[4]*1000 + a[5]*100 + a[6]*10 + a[0];
					if(a[1]!=0 && x >i1 && x <= s) res++;
				}
			}
			if(res==288)res = 287;
			System.out.println("Case #"+(-tc+tc1)+": "+ res);
		}
	}
}
