
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;

public class Recycle
{
	public static void main(String[] args)
	{
/*		Set<Integer> perms = getPermutations(123456);
		
		for(Integer i: perms)
			System.out.println(i);*/
	
		try
		{
			Scanner s = new Scanner(new File("recycle.in"));
			BufferedWriter w = new BufferedWriter(new FileWriter(new File("recycle.out")));
			int numCases = s.nextInt();
			s.nextLine();
			
			for(int n = 1;n <= numCases; n++)
			{
				int A = s.nextInt();
				int B = s.nextInt();
				int count = 0;
				for(int k = A; k <= B; k++)
				{
					count += getNumPairs(k, B);
				}
				
				w.write("Case #" + n + ": " + count + "\n");
			}
			
			w.flush();
			w.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static int getNumPairs(int n, int B)
	{
		Set<Integer> possibles = getPermutations(n);
		int count = 0;
		for(Integer i : possibles)
			if(i > n && i <= B)
				count++;
		return count;
	}
	
	public static Set<Integer> getPermutations(int n)
	{
		Set<Integer> perms = new TreeSet<Integer>();
		char[] digits = String.valueOf(n).toCharArray();
		
		for(int firstPos = 1; firstPos < digits.length; firstPos++)
		{
			String toBuild = "";
			for(int k = 0; k < digits.length; k++)
				toBuild += digits[(firstPos + k) % digits.length];
			perms.add(Integer.parseInt(toBuild));
		}
		return perms;
	}
	

}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;


public class RecycledNumbers {

	public static void main(String[] args) {
		Scanner input;
		try {
			File in = new File(args[0]);
			input = new Scanner(in);
			int testcaseSize = Integer.parseInt(input.nextLine());
			int testcaseNo = 1;
			
			String output = "";
			while ((input.hasNext()) && testcaseNo <= testcaseSize) {
				//read input
				String s = input.nextLine();			
				//solve problem
				String result = solve(s);
				//record formatted result 
				String outputLine = String.format("Case #%d: %s\n", testcaseNo, result);
				//append output
				output += outputLine;
				
				testcaseNo++;
			}
			input.close();
			//print output
			System.out.print(output);
			// write output file
			FileWriter writer = new FileWriter(in.getName() +".out");
			writer.write(output);
			writer.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String solve (String text){
		String result = "";
		int A = Integer.parseInt(text.substring(0,text.indexOf(" ")));
		int B = Integer.parseInt(text.substring(text.indexOf(" ")+1, text.length()));
		HashSet<Integer> set = new HashSet<Integer>();
		int digits = text.indexOf(" ");

		ArrayList<Integer> list1 = new ArrayList<Integer>();
		ArrayList<Integer> list2 = new ArrayList<Integer>();
		for (int n=A; n<=B; n++){
			if (!set.contains(n)) {
				String nString = Integer.toString(n);
				HashSet<Integer> group = new HashSet<Integer>();
				group.add(n);
				
				for (int j=1; j<digits; j++){
					String part1 = nString.substring(0,j);
					String part2 = nString.substring(j,digits);
					int m = Integer.parseInt(part2+part1);
					if (m>=A && m<=B && m!=n) {
						group.add(m);
					}
				}
				
				for (int i : group) set.add(i);
				Object[] groupArray = group.toArray();
				for (int i=0; i<group.size()-1; i++){
					for (int j=i+1; j<group.size(); j++){
						list1.add((Integer)groupArray[i]);
						list2.add((Integer)groupArray[j]);
					}
				}
			}
		}
		
//		for (int i=0;i<list1.size();i++){
//			System.out.println(String.format("(%d,%d);", list1.get(i), list2.get(i)));
//		}
//		System.out.println();
		result = String.valueOf(list1.size());
		return result;
	}
}
