/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gcj;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author daniele
 */
public class GCJ_C {
    
    public static void main(String[] args) throws Exception{
        
        
        
        Scanner in = new Scanner(new File(args[0]));
        FileWriter out = new FileWriter("/home/daniele/Scrivania/Output");
        

        int prove=in.nextInt();
        int min,max;
        int cifre;
        int cifreaus;
        String temp;
        int aus;
        int count=0;
        ArrayList<Integer> vals = new ArrayList<Integer>();
        int jcount=0;
        ArrayList<Integer> perm = new ArrayList<Integer>();
        
        out.write("");
        out.flush();
        
        for(int i=1;i<=prove;i++){
            
            out.append("Case #" +i+": ");out.flush();
            
            min=in.nextInt();
            max=in.nextInt();
            
            for(int j=min;j<=max;j++){
               if(!vals.contains(j)){
                    temp=""+j;
                    cifre=temp.length();
                    aus=j;
                    perm.add(j);
                    for(int k=1;k<cifre;k++){
                        aus=rotateOnce(aus);
                        temp=""+aus;
                        cifreaus=temp.length();//elusione zeri iniziali
                        if(aus>=min && aus<=max && aus!=j && cifreaus==cifre && nobody(vals,perm)){ 
                            perm.add(aus);
                            jcount ++;
                            
                        }
                        
                    }
                    while(jcount>0){count+=jcount; jcount--;}
                    vals.addAll(perm);
                    perm= new ArrayList<Integer>();
                    
                }
            }
            out.append(count+"\n");out.flush();
            count=0;
            vals= new ArrayList<Integer>();
        }
        
        
        
    }
        static public int rotateOnce(int n){
            String s=""+n;
            if(s.length()>1){
                s=s.charAt(s.length()-1) + s.substring(0,s.length()-1);
                n=Integer.parseInt(s);
            }
            return n;
        }    
        
        static public boolean nobody(ArrayList<Integer> v,ArrayList<Integer> a){;
            for(int i=0;i<a.size();i++)
                if(v.contains(a.get(i))) return false;
            return true;
        } 
    
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.StringTokenizer;

public class TestJAM
{
	public static void main (String[] args)
	{
/*		String nStr1 = "12";
		int numswindex1 = ;
		String nsw1 = nStr1.substring (numswindex1) + nStr1.substring (0, numswindex1);		
		System.out.println (nStr1.substring (numswindex1));
		System.out.println (nStr1.substring (0, numswindex1));
		System.out.println (nsw1);
		if (1==1) return;
*/		
		BufferedReader br = null;
		PrintStream ps = null;
		
		try
		{
			int a;
			int b; 
			int t;
			int count;
			String nStr;
			String mStr;
			String nsw;
			String line;
			StringTokenizer st;
			
			br = new BufferedReader (new FileReader ("C-small-attempt0.in"));
			ps = new PrintStream (new FileOutputStream ("C-small-attempt0.out"));
						
			line = br.readLine ();
			t = Integer.parseInt (line);
			
			for (int testCase = 0; testCase < t; testCase++)
			{
				count = 0;
				line = br.readLine ();
				st = new StringTokenizer (line);
				a = Integer.parseInt (st.nextToken ());
				b = Integer.parseInt (st.nextToken ());
				
				for (int n = a; n <= b; n++)
				{
					for (int m = n + 1; m <= b; m++)
					{
						nStr = String.valueOf (n);
						mStr = String.valueOf (m);
						
						int nlen = nStr.length ();
						int mlen = mStr.length ();

						if (nlen == mlen)
						{
							for (int numswindex = 0; numswindex < nlen; numswindex++)
							{
								nsw = nStr.substring (numswindex) + nStr.substring (0, numswindex);
								if (nsw.equalsIgnoreCase (mStr))
								{
									count++;
									break;
								}
							}
						}
						else if (nlen > mlen)
						{
							break;
						}
					}
				}
				
				ps.println ("Case #" + (testCase + 1) + ": " + count);

//				System.out.println (count);
			}
						

//			ps.println ("test1 test1 test1");
			
//			System.out.println ("Hola Mundo!!!");			
		}
		catch (IOException ioe)
		{
			ioe.printStackTrace ();
		}
		catch (NumberFormatException nfe)
		{
			nfe.printStackTrace ();
		}
		finally
		{
			if (ps != null)
			{
				ps.close ();
			}
			if (br != null)
			{
				try
				{
					br.close ();					
				}
				catch (IOException ioe)
				{
					ioe.printStackTrace ();
				}
			}
		}
	}	
}




