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

}

import java.util.*;
import java.io.*;
public class C
{
	List<String> arrList ;
	public void readFile(String srcFileName, String destFileName)
	{
		try
		{
			File srcFile = new File(srcFileName);
			File destFile = new File(destFileName);
			BufferedReader br = new BufferedReader(new FileReader(srcFile));
			BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
			int cases = Integer.parseInt(br.readLine());
			//cases =1;
			for(int i=0;i<cases;i++)
			{
				int counter =0;
				String lineInfo[] = br.readLine().split(" ");
				int firstNum = Integer.parseInt(lineInfo[0]);
				int secondNum = Integer.parseInt(lineInfo[1]);
				arrList= new ArrayList<String>();
				for(int k=firstNum;k<=secondNum;k++)
				{
					char[] ch = (k+"").toCharArray();
					for(int j =0; j<ch.length;j++)
					{
						ch = rotate(ch);
						int check = arrToString(ch);
					//	System.out.println(check);
						if(check != k && firstNum!=check && check!=secondNum)
						{
							if(firstNum<check && check<secondNum)
							{
								String sa = k+"-"+check;
								String sb = check+"-"+k;
								if(!arrList.contains(sa)&& !arrList.contains(sb))
								{
									arrList.add(sa);
									arrList.add(sb);
									counter++;
								}
							}
						}
					}
				}
				String str = "Case #"+(i+1)+": "+counter;
				bw.write(str);
				bw.newLine();
			}
			br.close();
			bw.close();
			
		}
		catch(Exception e)
		{
			System.out.println("Exception");
		}
	}
	
	public char[] rotate(char[] ch)
	{
		char temp = ch[0];
		for(int j=1;j<ch.length;j++)
		{
			ch[j-1]=ch[j];
		}
		ch[ch.length-1] = temp;
		return ch;
	}
	
	public int arrToString(char[] ch)
	{
		String str = "";
		for(int i=0;i<ch.length;i++)
		{
			str= str+ch[i];
		}
		return Integer.parseInt(str);
	}
	
	public static void main(String args[])
	{
		C c = new C();
		c.readFile(args[0],args[1]);
	}
}