package codejam;

import java.util.*;
import java.io.*;

public class RecycledNumbers {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader("in.txt"));
		PrintWriter out = new PrintWriter(new File("out.txt"));
		
		int T = Integer.parseInt(in.readLine());
		for (int t = 1; t <= T; ++t) {
			String[] parts = in.readLine().split("[ ]+");
			int A = Integer.parseInt(parts[0]);
			int B = Integer.parseInt(parts[1]);
			
			int cnt = 0;
			for (int i = A; i < B; ++i) {
				String str = String.valueOf(i);
				int n = str.length();
				String res = "";
				Set<Integer> seen = new HashSet<Integer>();
				for (int j = n - 1; j > 0; --j) {
					res = str.substring(j) + str.substring(0, j);
					int k = Integer.parseInt(res);
					if (k > i && k <= B) {
						//System.out.println("(" + i + ", " + k + ")");
						if (!seen.contains(k)) {
							++cnt;
							seen.add(k);
						}
					}
				}
			}

			out.println("Case #" + t + ": " + cnt);
		}
		
		in.close();
		out.close();
		System.exit(0);
	}
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;


public class RecycledNumbers {

	/**
	 * @param args
	 * @throws Exception 
	 */
	
	public static void main(String[] args) throws Exception {
		FileReader fr = new FileReader(new File(args[0]));
		BufferedReader br = new BufferedReader(fr);
		FileWriter fw = new FileWriter(new File("C:\\Users\\subhar\\Desktop\\DropBox\\Output.txt"));
		BufferedWriter bw = new BufferedWriter(fw);
		int testCases = Integer.parseInt(br.readLine());
		for(int i =1;i<testCases+1;i++){
			bw.write("Case #"+i+": ");
			String str = br.readLine();
			bw.write(String.valueOf(getCyclicCount(str)));
			if(i<testCases)bw.write("\r\n");

		}
		bw.flush();
		bw.close();

	}

	private static int getCyclicCount(String str) {
		int cyclicCount = 0;
		String strArr[] = str.split(" ");
		int firstNum = Integer.parseInt(strArr[0]);
		int secNum = Integer.parseInt(strArr[1]);
		int runningNum = firstNum;
		
		ArrayList<String> numList = new ArrayList<String>();
		while(runningNum<secNum+1){
				String runStr = String.valueOf(runningNum);
				int strLen = runStr.length();
				for(int i=1;i<strLen;i++){
					String newNumStr = runStr.substring(i, strLen)+runStr.substring(0, i);
					int newNum = Integer.parseInt(newNumStr);
					System.out.print(runningNum + "::"+newNum);
					if(newNum>=firstNum && newNum <=secNum && newNum!=runningNum){	
						String strFComb = runningNum<newNum?runningNum+"::"+newNum:newNum+"::"+runningNum;
						if(!numList.contains(strFComb)){
							cyclicCount++;
							numList.add(strFComb);
						}
					}
				
			}			
			runningNum++;			
		}		
		return cyclicCount;
	}
}
