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

import java.util.HashSet;

/**
 *
 * @author vandit
 */
public class RecycleNumbers {

    private HashSet<String> numbers = new HashSet<String>();
    private HashSet<String> initialTempNumbers = new HashSet<String>();
    private HashSet<String> finalTempNumbers = new HashSet<String>();

    HashSet<Pair> numberPairs = new HashSet<Pair>();


    private void findRecycledNumbers(int start, int end, int places) {


        for (int i = start; i <= end; i++) {
            String initialNumber = Integer.toString(i);
            //if (!(numbers.contains(initialNumber) || finalTempNumbers.contains(initialNumber))) {

                StringBuffer tempNumber = new StringBuffer(initialNumber);
                int len = tempNumber.length();
                int startIndexToMove = len - places;
                String tempString = tempNumber.substring(startIndexToMove);
                if ((places == 1 && tempString.equals("0")) || tempString.charAt(0) == '0') {
                    continue;
                }
                tempNumber.delete(startIndexToMove, len);
                String finalTempNumber = tempString + tempNumber.toString();

                if (! (finalTempNumber.equals(initialNumber) || Integer.parseInt(finalTempNumber) > end || Integer.parseInt(finalTempNumber) < Integer.parseInt(initialNumber))) {
                    numbers.add(initialNumber);
                    finalTempNumbers.add(finalTempNumber);
                    Pair pair = new Pair(finalTempNumber,initialNumber);
                    numberPairs.add(pair);
                }
            }

        

    }

    public HashSet<Pair> findAllRecycledNumbers(int start, int end) {
        int length = Integer.toString(start).length();
        for (int i = 1; i < length; i++) {
            findRecycledNumbers(start, end, i);
        }
        return numberPairs;
    }
}

import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.PrintStream;

public class RecycledNumbers {
	public boolean testRecycled(int a, int b) {
		String sa = String.valueOf(a);
		String sb = String.valueOf(b);
//		if(sa.indexOf('0')!=-1 || sb.indexOf('0')!=-1)
//			return false;
		int n = sa.length();
		for(int i=0; i<n; i++) {
			StringBuffer buf = new StringBuffer();
			buf.append(sa.substring(n-1));
			buf.append(sa.substring(0,n-1));
			sa = buf.toString();
			if(sb.equals(sa))
				return true;
		}
		return false;
	}
	public static void main(String[] args) throws Exception {
		RecycledNumbers rn = new RecycledNumbers();
		LineNumberReader in = new LineNumberReader(new FileReader(args[0]+".in"));
		String line = null;
		line = in.readLine();
		if(line==null||line.length()==0)
			return;
		int T = 0;
		T = Integer.parseInt(line);
		PrintStream out = new PrintStream(args[0]+".out");
		System.out.println(new java.util.Date());
		for(int k=1; k<=T; k++){
			line = in.readLine();
			String[] data = line.split(" ");
			// brute force
			int n = 0;
			int A = Integer.parseInt(data[0]); 
			int B = Integer.parseInt(data[1]); 
			for(int i = A; i<B; i++) {
				for(int j = i+1; j<=B; j++) {
					if(rn.testRecycled(i, j))
						n++;
				}
			}
			StringBuffer buf = new StringBuffer();
			buf.append("Case #");
			buf.append(k);
			buf.append(": ");
			buf.append(n);
			out.println(buf.toString());
			System.out.println(buf.toString());
		}
		System.out.println(new java.util.Date());
		in.close();
		out.close();
	}

}
