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

package year_2012.qualification;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author paul
 * @date 14 Apr 2012
 */
public class QuestionC {

    private static Set<Integer> getCycle(int x) {

        String s = Integer.toString(x);
        Set<Integer> set = new HashSet<Integer>();
        set.add(x);
        for (int c = 1; c < s.length(); c++) {
            String t = s.substring(c).concat(s.substring(0, c));
            set.add(Integer.parseInt(t));
        }
        return set;
        
    }

    private static Set<Integer> mask(Set<Integer> set, int a, int b) {
        Set<Integer> result = new HashSet<Integer>();

        for (int x : set) {
            if (x >= a && x <= b) {
                result.add(x);
            }
        }
        
        return result;

    }


    public static void main(String[] args) throws FileNotFoundException, IOException {

        String question = "C";

//        String name = "large";
        String name = "small-attempt0";
//        String name = "test";

        String filename = String.format("%s-%s", question, name);

        BufferedReader input = new BufferedReader(
                new FileReader(String.format("/home/paul/Documents/code-jam/2012/qualification/%s.in",
                filename)));

        String firstLine = input.readLine();

        PrintWriter pw = new PrintWriter(new File(
                String.format("/home/paul/Documents/code-jam/2012/qualification/%s.out", filename)));


        int T = Integer.parseInt(firstLine);

        // Loop through test cases.
        for (int i = 0; i < T; i++) {

//             Read data.
            String[] tokens = input.readLine().split(" ");

//            int N = Integer.parseInt(input.readLine());

            int A = Integer.parseInt(tokens[0]);
            int B = Integer.parseInt(tokens[1]);

//            System.out.format("%d, %d\n", A, B);

            boolean[] used = new boolean[B+1];
            int total = 0;
            for (int n = A; n <= B; n++) {
                if (!used[n]) {
                    Set<Integer> set = mask(getCycle(n), A, B);
                    int k = set.size();
                    total += (k * (k-1))/2;
                    for (int m : set) {
                        used[m] = true;
                    }
                }
            }

            pw.format("Case #%d: %d\n", i + 1, total);
        }

        pw.close();

    }

}

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

import dancing.gogglers.Googlers;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

/**
 *
 * @author Raveesh
 */
public class Recycler {
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        String filePath = "C:\\Users\\Raveesh\\Downloads\\C-small-attempt0.in";
        String filePathOutput = filePath + "_out";
        BufferedReader stdin = new BufferedReader(new FileReader(new File(filePath)));
        FileWriter fileWriter = new FileWriter(filePathOutput);
        ArrayList stringList = Recycler.getInputList(stdin);
        int count = 1;
        Iterator iter = stringList.iterator();
        while (iter.hasNext()) {
            String inputStr = (String) iter.next();
            String[] inputStrArr = inputStr.split("\\ ");
            int startNum  = Integer.valueOf(inputStrArr[0]);
            int endNum  = Integer.valueOf(inputStrArr[1]);
            int output = Recycler.performPerm(startNum,endNum);
            String out = "Case #" + count + ": " + output;
            fileWriter.write(out);
            fileWriter.write("\n");
            count++;
        }       
        fileWriter.close();

    }
    
    private static int performPerm(int start,int end ) {
        finalSet = new ArrayList();
        inputSet = new HashSet();
        for (int i=start;i<=end;i++) {
//            System.out.println("Running" + i);
            StringBuffer val = new StringBuffer(String.valueOf(i));
//            System.out.println("Running" + val);
            checkVal(start, end, val);
        }
        
//        Iterator iter = finalSet.iterator();
//        while (iter.hasNext()) {
//            System.out.println("Val " + iter.ne);
//        } 
//        System.out.println("=========================COUNT " + finalSet.size()/2);
        return finalSet.size()/2;
        
        
    }
    
    private static ArrayList finalSet = new ArrayList();
    private static HashSet inputSet = new HashSet();
    private static boolean checkVal (int start,int end, StringBuffer val) {
        
        
        for (int i =0;i< val.length() ; i++) {
            String temp = val.substring(0,i+1);
            String rem = val.substring(i+1,val.length());
            String val1 = rem + temp;
//            System.out.println("temp" + temp + " rem : " + rem);
//            for (int j = 0;j <= rem.length() ; j++) {
//                StringBuffer val1 = new StringBuffer(rem);
                
                
//                if (j==i) {
//                    continue;
//                    
//                }
//                val1.insert(j, temp);
//                System.out.println("iNPUT :; " + val + " :: Output : " + val1);
//                System.out.println("iNPUT :; " + val + " :: Output : " + val1);
                int t = Integer.valueOf(val1.toString());
                if (t >= start && t <= end && !val.toString().equals(val1.toString())) {
//                    System.out.println("iNPUT :; " + val + " :: Output : " + val1 + " t :: " + t );
                    if (inputSet.add(val + "," + val1)) {
                        finalSet.add(t);
                    }
////                    System.out.println("dding :: " + t);
//                    String key = val.toString() + ","  + val1.toString();
//                    String key1 = val1.toString() + "," + val.toString();
//                    if(!(finalSet.containsKey(key)|| finalSet.containsKey(key1))) {
                        
//                        finalSet.put(key, t);
//                    }
////                    finalSet.put(Integer.valueOf(val1.toString()));
//                }
//                System.out.println("num :: " + val1);
            }
        }
        return true;
    }
    
    
    private static ArrayList getInputList(BufferedReader stdin) throws IOException {
        ArrayList stringList = new ArrayList<String>();
        String line;
        int inputLineCount = 0;
        Integer testCaseCount = 0;
        while ((line = stdin.readLine()) != null && line.length() != 0 && inputLineCount <= testCaseCount.intValue()) {
//            System.out.println("line ::  " + line);
            if (inputLineCount == 0) {
                try {
                    testCaseCount = Integer.valueOf(line);
                } catch (NumberFormatException pe) {
                    System.out.println("Invalid input for # of test cases");
                    System.exit(0);
                }
                inputLineCount++;
                continue;
            }
            stringList.add(line);
            inputLineCount++;

            if (inputLineCount == testCaseCount + 1) {
                break;
            }

        }
        return stringList;
    }
    
}
