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

package hasnaer.puzzles.codejam;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 *
 * @author hasnae rehioui
 */
public class RecycledNumbers {

    public static Map<Integer, List<Integer>> _map  = new HashMap<Integer, List<Integer>>();
    
    
    static{    
        for(int i = 0; i < 3000; i++){
           if(i <= 10){
               _map.put(i, new ArrayList<Integer>());
           } 
           else{
               String _str = Integer.toString(i);
               List<Integer> _list = new ArrayList<Integer>();
               for(int j = 1; j < _str.length(); j++){
                   _str = _str.substring(1).concat(_str.substring(0, 1));
                   int _val = Integer.parseInt(_str);
                   if(_val > i){
                       _list.add(_val);
                   }
               }
               _map.put(i, _list);
           }
        }
    }
    
    
    public static void main(String[] args) throws Exception {
        
//        Scanner _input = new Scanner(System.in);
        Scanner _input = new Scanner(new FileInputStream("C-small-attempt0.in"));
//        BufferedWriter _writer = new BufferedWriter(new OutputStreamWriter(System.err));
        BufferedWriter _writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C-small-attempt0.out")));
                        
        int _t = _input.nextInt();
        for(int i = 0; i < _t; i++){
            int _a = _input.nextInt();
            int _b = _input.nextInt();
            
//            System.out.println(String.format("_a:%s;_b:%s", _a, _b));
            
            int _cnt = 0;
            for(int j = _a; j <= _b; j++){
                List<Integer> _list = _map.get(j);
                for(Integer _num : _list){
                    if(_num <= _b){
                        _cnt++;
//                        System.out.println(String.format("[%s,%s]", j, _num));
                    }
                }
            }
            
            _writer.write(String.format("Case #%s: %s\n", i + 1, _cnt));
        }
        
        _writer.close();
    }
    
}