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


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;

/**
 *
 * @author dafferianto
 */
public class RecycledNumbers {
    private static Scanner inputStream;
    private static PrintWriter outputStream;
    private static int T, lower, upper, counter, tempNumber, result;
    private static String numberString, tempNumberString;
    private static HashMap<Integer, Integer> aHashMap;
    public static void main(String[] args) throws FileNotFoundException, IOException {
        try {
            inputStream = new Scanner(new FileReader(args[0]));
            outputStream = new PrintWriter(new FileWriter("RecycledNumbersOutput.txt"));
            
            T = inputStream.nextInt();
            //T = Integer.parseInt(inputStream.nextLine());
            
            for (int i = 0; i < T; i++) { // For each test case
                lower = inputStream.nextInt();
                upper = inputStream.nextInt();
                aHashMap = new HashMap<Integer, Integer>();
                //result = solve(lower, upper);
                //outputStream.println("Case #" + (i+1) + ": " + result);
                outputStream.println("Case #" + (i+1) + ": " + solve(lower, upper));
                //System.out.println("Case #" + (i+1) + ": " + result);
            }
            
        } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
        }
    }

    private static int solve(int lower, int upper) {
        counter = 0;
        for (int currentNumber = lower; currentNumber <= upper; currentNumber++) {
            if (aHashMap.containsKey(currentNumber)) {
                continue;
            }
            counter += process(currentNumber);
        }
        return counter;
    }
    
    private static int process(int aNumber) {
        int localCounter = 0;
        numberString = Integer.toString(aNumber);
        for (int i = numberString.length()-1; i > 0; i--) {
            if (Character.getNumericValue(numberString.charAt(i)) == 0) {
                continue;
            }
            tempNumberString = numberString.substring(i) + numberString.substring(0, i);
            tempNumber = Integer.parseInt(tempNumberString);
            if (tempNumber == aNumber) {
                continue;
            }
            if (aHashMap.containsKey(tempNumber)) {
                continue;
            }
            if ((tempNumber >= lower) && (tempNumber <= upper)) {
                localCounter++;
                aHashMap.put(tempNumber, tempNumber);
                //System.out.println(tempNumber);
            }
        }
        if (localCounter != 0) {
            aHashMap.put(aNumber, aNumber);
            //System.out.println(aNumber);
        }
        if (localCounter > 1) {
            return sumOfSequence(localCounter);
        }
        return localCounter;
    }

    private static int sumOfSequence(int aCounter) {
        int tempSum = 0;
        for (int i = 1; i <= aCounter; i++) {
            tempSum += i;
        }
        return tempSum;
    }
}
