import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Recycle {

	private static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
	private static HashSet<Integer> toSkip = new HashSet<Integer>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int T = reader.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int a = reader.nextInt();
			int b = reader.nextInt();
			long before = System.currentTimeMillis();
			int res = doit(a, b);
			System.out.printf("Case #%d: %d\n", tc, res);
			long after = System.currentTimeMillis();
//			System.out.println("time taken: " + (after - before));
		}
	}

	private static int doit(int a, int b) {
		int count = 0;
		HashSet<Integer> skip = new HashSet<Integer>(toSkip);
		for (int i = a; i <= b; i++) {
			if (skip.contains(i))
				continue;
			HashSet<Integer> arr = generate(i);
//			if(arr.size() > 1) {
//				map.put(i, arr);
//			}
			// System.out.println(i + " --> " +
			// Arrays.deepToString(arr.toArray()));
			int temp = 0;
			if (arr.size() > 1) {
				for (int x : arr) {
					if (x >= a && x <= b) {
						temp++;
						skip.add(x);
					}
				}
				count += temp * (temp - 1) / 2;
			}
//			System.out.println("not skipping " + i + " gen: " + arr.size()+ " " + (temp * (temp - 1) / 2));
		}
		return count;
	}

	private static HashSet<Integer> generate(int num) {
		if(map.containsKey(num)) {
			HashSet<Integer> list = map.get(num);
			return list;
		}
		HashSet<Integer> list = new HashSet<Integer>();
		String s = "" + num;
		list.add(num);
		int len = s.length();
		for (int i = 1; i < len; i++) {
			String temp = s.substring(i) + s.substring(0, i);
			// System.out.println("temp: " + temp);
			if (temp.charAt(0) == '0')
				continue;
			int x = Integer.parseInt(temp);
			if( x <= 2000000) {
				list.add(x);
			}
		}
		if(list.size() > 1) {
			map.put(num, list);
		} else {
			toSkip.add(num);
		}
		return list;
	}

}

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

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 *
 * @author Vale
 */
public class RecycledNumbers {
    
    public static void main(String[] args){
        String inputFile="/Users/Vale/Desktop/CodeJam/C-small-attempt1.in";
        String outputFile="/Users/Vale/Desktop/CodeJam/C-small-output1.txt";
        
        String[] lines=readAllLinesOfFile(inputFile);
        int numberOfCases=Integer.parseInt(lines[0]);
        String[] result=new String[numberOfCases];
        
        for (int i = 1; i <= numberOfCases; i++) {
            String currentLine=lines[i];
            int lowerValue=Integer.parseInt(currentLine.substring(0, currentLine.indexOf(" ")));
            int start=currentLine.indexOf(" ")+1;
            int upperValue=Integer.parseInt(currentLine.substring(start));
            int resultValue=0;
            for (int j = lowerValue; j < upperValue; j++) {
                for (int k = j+1; k < upperValue+1; k++) {
                    if(isRecycledPair(j, k))
                        resultValue++;
                }
            }
            result[i-1]="Case #"+i+": "+resultValue;
        }
        writeLinesToFile(outputFile, result);
    }
    
    public static int powerOf(int n, int i){
        if(i==1)
            return n;
        else
            return n*powerOf(n, i-1);
    }
    
    public static int digitsOf(int n){
        int result=0;
        while(n>0){
            result++;
            n/=10;
        }
        return result;
    }
    
    public static boolean isRecycledPair(int n, int m){
        int numberOfDigits=digitsOf(n);
        for (int digits = 1; digits < numberOfDigits; digits++) {
            int recycledPart=(n%powerOf(10, digits));
            int recycleValue=recycledPart*(powerOf(10, numberOfDigits-digits));
            int recycledNumber=(n/powerOf(10, digits))+recycleValue;
            if (recycledNumber==m)
                return true;
        }
        return false;
    }
    
    public static String[] readAllLinesOfFile(String file){
        try{
          // Open the file that is the first 
          // command line parameter
          FileInputStream fstream = new FileInputStream(file);
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          //Read File Line By Line
          String[] result=new String[1000000];
          int index=0;
          while ((strLine = br.readLine()) != null)   {
              result[index++]=strLine;
          }
          //Close the input stream
          in.close();
          String[] newResult=java.util.Arrays.copyOf(result, index);
          return newResult;
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
          return new String[] {};
        }
    }
    
    public static void writeLinesToFile(String filename, String[] linesToWrite) {
        try {
            PrintWriter pw = new PrintWriter(new FileWriter(filename));
            for (int i = 0; i < linesToWrite.length; i++) {
                pw.println(linesToWrite[i]);
            }
            pw.flush();
            pw.close();
        } catch (Exception e) {
            throw new RuntimeException("Writing File \"" + filename + "\" Failed!");
        }
    }
}
