package jp.funnything.competition.util;

import java.math.BigInteger;

/**
 * Utility for BigInteger
 */
public class BI {
    public static BigInteger ZERO = BigInteger.ZERO;
    public static BigInteger ONE = BigInteger.ONE;

    public static BigInteger add( final BigInteger x , final BigInteger y ) {
        return x.add( y );
    }

    public static BigInteger add( final BigInteger x , final long y ) {
        return add( x , v( y ) );
    }

    public static BigInteger add( final long x , final BigInteger y ) {
        return add( v( x ) , y );
    }

    public static int cmp( final BigInteger x , final BigInteger y ) {
        return x.compareTo( y );
    }

    public static int cmp( final BigInteger x , final long y ) {
        return cmp( x , v( y ) );
    }

    public static int cmp( final long x , final BigInteger y ) {
        return cmp( v( x ) , y );
    }

    public static BigInteger div( final BigInteger x , final BigInteger y ) {
        return x.divide( y );
    }

    public static BigInteger div( final BigInteger x , final long y ) {
        return div( x , v( y ) );
    }

    public static BigInteger div( final long x , final BigInteger y ) {
        return div( v( x ) , y );
    }

    public static BigInteger mod( final BigInteger x , final BigInteger y ) {
        return x.mod( y );
    }

    public static BigInteger mod( final BigInteger x , final long y ) {
        return mod( x , v( y ) );
    }

    public static BigInteger mod( final long x , final BigInteger y ) {
        return mod( v( x ) , y );
    }

    public static BigInteger mul( final BigInteger x , final BigInteger y ) {
        return x.multiply( y );
    }

    public static BigInteger mul( final BigInteger x , final long y ) {
        return mul( x , v( y ) );
    }

    public static BigInteger mul( final long x , final BigInteger y ) {
        return mul( v( x ) , y );
    }

    public static BigInteger sub( final BigInteger x , final BigInteger y ) {
        return x.subtract( y );
    }

    public static BigInteger sub( final BigInteger x , final long y ) {
        return sub( x , v( y ) );
    }

    public static BigInteger sub( final long x , final BigInteger y ) {
        return sub( v( x ) , y );
    }

    public static BigInteger v( final long value ) {
        return BigInteger.valueOf( value );
    }
}

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


public class RecycledNumbers {


	public static void main(String[] args) throws Exception {
		RecycledNumbers recycledNumbers = new RecycledNumbers();
		// file name
		String fileName = "recycled_numbers";
		
		// file variable
		File inputFile = new File(fileName + ".in");
		File outputFile = new File(fileName + ".out");
		Scanner scanner = new Scanner(inputFile);
		FileWriter writer = new FileWriter(outputFile);


		// problem variable
		int totalCase;
	
		// get total case
		totalCase = Integer.parseInt(scanner.nextLine());
		System.out.println("Total case:"+totalCase);
		
		for (int caseIndex = 1; caseIndex <= totalCase; caseIndex++) {

			String lineDetail = scanner.nextLine();
			String[] inputArray = lineDetail.split(" ");
			String min =  inputArray[0];
			String max =  inputArray[1];
			
			int  possibleCases = 0;
			
			possibleCases = recycledNumbers.findPossibleCases(min, max);
			
			String output = "Case #" + caseIndex + ": " + possibleCases;		
			System.out.println(output);
			writer.write(output + "\n");
		}
		scanner.close();
		writer.close();
	}
	
	private int findPossibleCases(String min, String max){
		int possibleCases = 0;
		int startVal = Integer.parseInt(min);
		int endVal = Integer.parseInt(max);
		int n,m = 0;
		HashMap<String, String> usedPattern = new HashMap<String, String>();
		
		for(int i=startVal; i<endVal; i++){
			n = i;
			String inputStr = ""+i;
			String shiftStr = "";
			char[] charArr = inputStr.toCharArray();
		
			usedPattern = new HashMap<String, String>();
			
			//startAt
			for(int j= 0; j<charArr.length; j++){
				shiftStr = "";
				// loop to get shift data
				for(int k= j; k<charArr.length+j; k++){
					shiftStr += charArr[k%charArr.length];
				}
				m = Integer.parseInt(shiftStr);
				if( (m > n) && (m <=endVal)){
					if(!usedPattern.containsKey(shiftStr) ){
						possibleCases++;
					}
				}
				
				// keep the temp of shifting data
				usedPattern.put(shiftStr, shiftStr);
			}
			m = 0;

		}
		return possibleCases;
	}
}
