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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Vector;


public class RecycledNumbers {


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String strNumberOfSentences = "";
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
		    
		Integer numberOfSentences = -1;
		MyPair[] sentences = null;
		try {
			strNumberOfSentences = in.readLine();
		
			numberOfSentences = Integer.parseInt(strNumberOfSentences);
			sentences = new MyPair[numberOfSentences];
			
			for ( int i = 0; i < numberOfSentences; ++i) {
				sentences[i] = new MyPair(in.readLine());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if ( numberOfSentences == -1) {
			return;
		}
		
		Vector<MyPair> usedNumbers = new Vector<MyPair>();
		
		for ( int i = 0; i < numberOfSentences; ++i) {
			
			int curr = sentences[i].lowerLimit;
			while (curr < sentences[i].higherLimit) {
				StringBuilder sss = new StringBuilder("");
				
				sss.append(String.valueOf(curr));
				StringBuilder sss2 = new StringBuilder(String.valueOf(curr));
				for (int ii = 0; ii < sss.length()-1; ++ii) {
					
					char temp = sss2.charAt(0);
					sss2.replace(0, 0, String.valueOf(sss2.charAt(sss2.length()-1)));
					sss2.delete(sss2.length()-1, sss2.length());
					
					if (Integer.parseInt(sss2.toString()) <= sentences[i].higherLimit && 
						Integer.parseInt(sss2.toString()) >= sentences[i].lowerLimit &&
						
						Integer.parseInt(sss.toString()) < Integer.parseInt(sss2.toString()) ) {
						MyPair x = new MyPair(Integer.parseInt(sss.toString()),Integer.parseInt(sss2.toString()));
						boolean isFound = false;
						for (int kkk =0; kkk < usedNumbers.size(); ++kkk ){
							if ( usedNumbers.get(kkk).sameAs(x) ) {
								isFound = true;
							}
						}
						if ( !isFound ) {
							usedNumbers.add(x);
							//System.out.println("" + curr + ": " + x.lowerLimit + "," + x.higherLimit );
						}
					}
				}
				curr++;
			}
			
			System.out.println("Case #" + new Integer(i+1).toString() + ": " + usedNumbers.size() );
			SortedSet<Integer> usedNumbers2 = new TreeSet<Integer>();
			for (int kkk =0; kkk < usedNumbers.size(); ++kkk ){
				if ( !usedNumbers2.contains(usedNumbers.get(kkk).lowerLimit) ) {
					usedNumbers2.add(usedNumbers.get(kkk).lowerLimit);	
				}
				if ( !usedNumbers2.contains(usedNumbers.get(kkk).higherLimit) ) {
					usedNumbers2.add(usedNumbers.get(kkk).higherLimit);	
				}
			}
			for (Integer item: usedNumbers2 ){
				//System.out.println("" + item.toString() );	
			}
			
			usedNumbers.clear();
			
			
		}
		
		

	}

}



/*			
			int length = sentences[i].lowerLimit.length();
			
			if ( length == 0 || length == 1) {
				numberOfShits = 0;
			}
			else if ( length == 2) {
				int iii = 
						Character.digit(sentences[i].higherLimit.charAt(0),10) - 
						Character.digit(sentences[i].lowerLimit.charAt(0),10);
				
				numberOfShits *= iii * 2;
			}
			else if ( length > 2) {
				numberOfShits = (int) Math.pow(10, length-2);
				int iii = 
						Character.digit(sentences[i].higherLimit.charAt(0),10) - 
						Character.digit(sentences[i].lowerLimit.charAt(0),10);
				
				numberOfShits *= iii * iii;
				numberOfShits -= iii;
			}
			
			
			*/