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.io.* ; 
import java.util.* ; 

public class FileReaderWriter {
	
	public static final int WRITE_MODE = 1 ; 
	public static final int READ_MODE = 2 ;
	
	private File file ; 
	
	private BufferedReader br ;
	private PrintWriter pw ; 
	private int mode ; 
	
	FileReaderWriter(String fileName , int mode) throws Exception
	{
		this.file = new File(fileName) ;
		this.mode = mode ;
		
		if(mode == FileReaderWriter.READ_MODE) 
		{
			if(!(file.isFile() && file.exists()))
			{
				throw new Exception(" File "+file.getPath()+" is not available for reading !") ;
			}
			else
			{
				br = new BufferedReader(new FileReader(this.file)) ;
			}
		}
		else if(mode == FileReaderWriter.WRITE_MODE)
		{
		  	pw = new PrintWriter(new FileWriter(this.file)) ; 
		}
		else 
		{
			throw new Exception("Unsupported File mode") ;
		}
	}
	
	public String readLine() throws Exception
	{
		return br.readLine() ; 
	}
	
	public Integer readInt() throws Exception
	{
		return Integer.parseInt(br.readLine()) ; 
	}
	
	public ArrayList<String> readStrList() throws Exception
	{
		String readStr = readLine() ;
		
		StringTokenizer stTokenizer = new StringTokenizer(readStr) ;
		
		ArrayList<String> readStrList = new ArrayList<String>(stTokenizer.countTokens()) ; 
		
		while(stTokenizer.hasMoreTokens())
		{
			readStrList.add(stTokenizer.nextToken()) ;
		}
		
		return readStrList ; 
		
	}
	
	public ArrayList<Integer> readIntList() throws Exception
	{
		String readStr = readLine() ;
		
		StringTokenizer stTokenizer = new StringTokenizer(readStr) ;
		
		ArrayList<Integer> readIntList = new ArrayList<Integer>(stTokenizer.countTokens()) ; 
		
		while(stTokenizer.hasMoreTokens())
		{
			readIntList.add(Integer.parseInt(stTokenizer.nextToken())) ;
		}
		
		return readIntList ; 
		
	}
	
	public ArrayList<Long> readLongList() throws Exception
	{
		String readStr = readLine() ;
		
		StringTokenizer stTokenizer = new StringTokenizer(readStr) ;
		
		ArrayList<Long> readLongList = new ArrayList<Long>(stTokenizer.countTokens()) ; 
		
		while(stTokenizer.hasMoreTokens())
		{
			readLongList.add(Long.parseLong(stTokenizer.nextToken())) ;
		}
		
		return readLongList ; 
		
	}
	
	public void write(String str) throws Exception
	{
		pw.print(str) ;
		pw.flush() ;
		
	}
	
	public void writeLine(String str) throws Exception
	{
		pw.println(str) ; 
		pw.flush() ;
		
	}
	
	public void writeListLine(ArrayList list, String seperator) throws Exception
	{
		ListIterator iterator = list.listIterator() ;
		
		while(iterator.hasNext())
		{
			pw.print(iterator.next()) ;
			if(iterator.hasNext()) pw.print(seperator) ;
			
		}
		
		pw.print("\n"); 
		pw.flush();
		
	}
	
	
	public void close() throws Exception
	{
		if(mode == FileReaderWriter.READ_MODE)
		{
			br.close() ; 
		}
		else if(mode == FileReaderWriter.WRITE_MODE) 
		{
			pw.flush() ;
			pw.close() ;
		}
	}
	
	
}
