import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t=sc.nextInt();
		int casos=1, a, b, n, m, cont;
		while(t!=0){
			a=sc.nextInt();
			b=sc.nextInt();
			if(a>b){
				int aux=a;
				a=b;
				b=aux;
			}
			System.out.printf("Case #%d: ",casos++);
			if(a==b){
				System.out.printf("%d\n",0);
			}else{
				cont=0;
				for(n=a;n<b;n++){					
					for(m=n+1;m<=b;m++){					
						if(isRecycled(n,m)) cont++;
					}
				}
				System.out.printf("%d\n",cont);
			}
			t--;
		}		
	}
	public static boolean isRecycled(int n1, int n2){
		String s1, s2, aux;
		s1 =  String.valueOf( n1 );
		s2 =  String.valueOf( n2 );		
		boolean r = false;		
		for(int i=0;i<s1.length();i++){
			aux="";
			aux=s1.substring(i,s1.length())+s1.substring(0,i);
//			System.out.println(aux);
			if(aux.equals(s2)){
				r=true;
				break;
			}
		}		
		return r;
	}
}

package RecycledNumbers;

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

/**
 *
 * @author Nikhil
 */
public class c {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        File fInput = new File(args[0]);
        String tmp;
        BufferedReader br = new BufferedReader(new FileReader(fInput));
        int iTestCases = Integer.parseInt(br.readLine());
        int A, B, iCase = 1;
        while (null != (tmp = br.readLine())) {
            A = Integer.parseInt(tmp.split(" ")[0]);
            B = Integer.parseInt(tmp.split(" ")[1]);
            System.out.println("Case #" + iCase + ": " + findRecycled(A, B));
            iCase++;
        }
        br.close();
    }

    private static int findRecycled(final int A, final int B) {
        int iRecycled = 0;
        int iRemainder, m, iTenPowMove;
        int iRemainderB;
        String sUnqNM;
        HashSet<String> hsRecycled = new HashSet<String>();
        final int B_LEN = String.valueOf(B).length() - 1;
        if (B < 10) {
            return 0;
        }
        //System.out.println("A=" + A + "\tB=" + B);
        //dblRemainderB = B / Math.pow(10d, (double) (String.valueOf(B).length() - 1));
        for (int iMoveDigitCount = 1; iMoveDigitCount <= B_LEN; iMoveDigitCount++) {
            // Increament 10^x to compute last digit from A
            iTenPowMove = (int) Math.pow(10d, (double) iMoveDigitCount);
            // Get the MSB digits from B to optimize algo
            iRemainderB = Integer.parseInt(String.valueOf(B).substring(0, iMoveDigitCount));
            for (int n = A; n <= B; n++) {
                iRemainder = n % iTenPowMove;
                if (0 == iRemainder || n < iTenPowMove) {
                    continue;
                } else if (iRemainder > iRemainderB) {
                    //iMoveDigitCount++;
                    n += iTenPowMove - iRemainder;
                    continue;
                } else {
                    String strM = null;
                    StringBuilder sb = new StringBuilder(String.valueOf(n));
                    int iPre = sb.length() - iMoveDigitCount;
                    try {
                     strM = sb.substring(iPre) + sb.substring(0, iPre);
                    } catch (StringIndexOutOfBoundsException ex) {
                        System.out.println("sb=" + iRemainder + "\niPre=" + iPre);
                        throw ex;
                    }
                    m = Integer.parseInt(strM);
                    if (m <= n) {
                        continue;
                    } else if (m <= B) {
                        sUnqNM = String.valueOf(n) + String.valueOf(m);
                        if (!hsRecycled.contains(sUnqNM)) {
                            hsRecycled.add(sUnqNM);
                            iRecycled++;
                       }
                    }
                }
            }
        }
        return iRecycled;
    }

}
