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

package year_2012.qualification;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author paul
 * @date 14 Apr 2012
 */
public class QuestionC {

    private static Set<Integer> getCycle(int x) {

        String s = Integer.toString(x);
        Set<Integer> set = new HashSet<Integer>();
        set.add(x);
        for (int c = 1; c < s.length(); c++) {
            String t = s.substring(c).concat(s.substring(0, c));
            set.add(Integer.parseInt(t));
        }
        return set;
        
    }

    private static Set<Integer> mask(Set<Integer> set, int a, int b) {
        Set<Integer> result = new HashSet<Integer>();

        for (int x : set) {
            if (x >= a && x <= b) {
                result.add(x);
            }
        }
        
        return result;

    }


    public static void main(String[] args) throws FileNotFoundException, IOException {

        String question = "C";

//        String name = "large";
        String name = "small-attempt0";
//        String name = "test";

        String filename = String.format("%s-%s", question, name);

        BufferedReader input = new BufferedReader(
                new FileReader(String.format("/home/paul/Documents/code-jam/2012/qualification/%s.in",
                filename)));

        String firstLine = input.readLine();

        PrintWriter pw = new PrintWriter(new File(
                String.format("/home/paul/Documents/code-jam/2012/qualification/%s.out", filename)));


        int T = Integer.parseInt(firstLine);

        // Loop through test cases.
        for (int i = 0; i < T; i++) {

//             Read data.
            String[] tokens = input.readLine().split(" ");

//            int N = Integer.parseInt(input.readLine());

            int A = Integer.parseInt(tokens[0]);
            int B = Integer.parseInt(tokens[1]);

//            System.out.format("%d, %d\n", A, B);

            boolean[] used = new boolean[B+1];
            int total = 0;
            for (int n = A; n <= B; n++) {
                if (!used[n]) {
                    Set<Integer> set = mask(getCycle(n), A, B);
                    int k = set.size();
                    total += (k * (k-1))/2;
                    for (int m : set) {
                        used[m] = true;
                    }
                }
            }

            pw.format("Case #%d: %d\n", i + 1, total);
        }

        pw.close();

    }

}

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;


public class Recycled {

	
	public static void main(String args[]) {
		try {
		BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("c:\\gcj\\C\\C-small-attempt0.in")));
		String line=br.readLine();
		PrintStream ps=new PrintStream("c:\\gcj\\C\\C-Small.out");
		
		int n=Integer.parseInt(line);
			for(int i=1;i<=n;i++) {
				
				line=br.readLine();
				String[] nums=line.split(" ");
				long A=Long.parseLong(nums[0]);
				long B=Long.parseLong(nums[1]);
				ps.println("Case #"+i+": "+getCount(A,B));
										
			}
		} 
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static long getCount(long A, long B) {
		//long A=1111,B=2222;
		long count=0;
		//HashMap hm=new HashMap<Integer,Integer>();
		for(long i=A;i<=B;i++)
		{
			for(long j=i+1;j<=B;j++) {
				if(j>i && isRecycled(i,j))
				{
					//hm.put(i, j);
					count++;
				}
			}	
		}
		//count=hm.size();
		//System.out.print("count : "+count);
		return count;
	}

	
	static boolean isRecycled(long n, long m) {
		long temp=n;
		int[] countN=new int[10];
		int[] countM=new int[10];
		
		int numDigits=0;
		//int[] orderN=new int[10];
		//int[] orderM=new int[10];
		for(int i=0;i<10;i++) {
			countN[i]=0;
			countM[i]=0;
			
			//orderN[i]=-1;
			//orderM[i]=-1;
		}
		//int firstDigit=0;
		while(temp!=0)
		{
			int digit=(int)temp%10;
			countN[digit]++;
			temp=temp/10;
			numDigits++;
	/*		if(temp==0)
				firstDigit=digit;
		*/		
		}

		temp=m;
		while(temp!=0)
		{
			int digit=(int)temp%10;
			countM[digit]++;
			temp=temp/10;
		}
		
		for(int i=0;i<10;i++){

			if(countN[i]!=countM[i])
				return false;
		}
		
	
		
	long denom=(long)Math.pow(10, numDigits-1);
	
	temp=m;
//	int digit=firstDigit;
	long sub=0;
	do {
	
		int firstDigit=(int)(temp/denom);
		sub=denom*firstDigit;
		//int digit=(int)temp%10;
		temp=temp-sub;
		temp=temp*10+firstDigit;
		if(temp==n)
			return true;
		
		
	}
	while(temp!=m);
	
	
		
	/*
		
		String strN=String.valueOf(n);
		StringBuilder strM=new StringBuilder(String.valueOf(m));

for(int i=0;i<strM.length();i++)
{
	char ch=strM.charAt(0);
	strM.deleteCharAt(0);
	strM.append(ch);
	if(strM.toString().equals(strN))
		return true;
	
}*/
		return false;
		
	}
}
