/*
 * 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;
    }
}

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

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/**
 *
 * @author agraa1
 */
public class Recycle {
    public static void main(String[] args) throws Exception {
        Recycle puzzle = new Recycle();
        puzzle.solve();
    }
    
    private void solve() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("E:\\C-small-attempt0.in"));
        String line = br.readLine();
        for (int count=0; count<Integer.valueOf(line); count++) {
            showResult(count+1, br.readLine());
        }
        br.close();
    }
    
    private void showResult(int caseNum, String input) {
        List<Integer> recNumbers = new ArrayList<Integer>();
        StringTokenizer tokenizer = new StringTokenizer(input, " ");
        String A = tokenizer.nextToken();
        String B = tokenizer.nextToken();
        if (Integer.valueOf(B) > 10) {
            findSolution(A, B, recNumbers);
        }
        StringBuilder builder = new StringBuilder("Case #");
        builder.append(caseNum);
        builder.append(": ");
        builder.append(recNumbers.size());
        System.out.println(builder.toString());
    }
    
    private void findSolution(String A, String B, List recNumbers) {
        int numA = Integer.valueOf(A);
        int numB = Integer.valueOf(B);
        for (int num = numA; num < numB; num++) {
            getRecycles(num, numA, numB, recNumbers);
        }
    }
    
    private void getRecycles(int num, int numA, int numB, List recNumbers) {
        String toBeRec = ""+num;
        //System.out.println("Recycles for " + num);
        Set<Integer> recyclesForNum = new HashSet<Integer>();
        for (int count=1; count<toBeRec.length(); count++) {
            int recycledNum = Integer.valueOf(toBeRec.substring(toBeRec.length() - count, toBeRec.length()) + toBeRec.substring(0, toBeRec.length() - count));
            //System.out.println("" + count + ":" + recycledNum);
            if ( recycledNum <= numB && recycledNum >= numA && recycledNum > num) {
                //System.out.println("#" + (recNumbers.size()+1) + " ----------------" + num + "--" + recycledNum);
                recyclesForNum.add(recycledNum);
            }
        }
        recNumbers.addAll(recyclesForNum);
    }
}
