
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.HashMap;
import java.util.Scanner;


public class RecycledNums {
	public static String rootDir = System.getProperty("user.dir") + File.separator;
	public static String filesDir = rootDir + "files" + File.separator;
	private FileWriter fw;
	
	private int tests;
	private int[] a;
	private int[] b;
	private int[] pow10 = {1,10,100,1000,10000,100000,1000000,10000000};
	
	
	public static void main(String[] args) {
		RecycledNums r = new RecycledNums();
		r.go();
	}
	
	public RecycledNums() {
		try {
			Scanner input = new Scanner(new File(filesDir + "recycled.txt"));
			Scanner line;
			tests = Integer.parseInt(input.nextLine());
			a = new int[tests];
			b = new int[tests];			
			
			for (int c  = 0; c < tests; c++) {
				line = new Scanner(input.nextLine());
				
				a[c] = line.nextInt();
				b[c] = line.nextInt();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
//		for (int i = 0; i < a.length; i++) {
//			System.out.println(a[i] + " " + b[i]);
//		}
	}
	
	public void go() {
		
		open();
		int[] r;
		int count;
		HashMap<Integer,Integer> h;
		for (int c = 0; c < tests; c++) {
			count = 0;
			h = new HashMap<Integer,Integer>();
			
			for (int n = a[c]; n <= b[c]; n++) {
				if (digits(n) < 2)
					continue;
				
				if (h.containsKey(n))
					continue;
				r = getRecycledNums(n);
				
				for (int i = 0; i < r.length; i++) {
					for (int j = i + 1; j < r.length; j++) {
						if (r[i] < a[c] || r[i] > b[c] || r[j] < a[c] || r[j] > b[c])
							continue;
						if (digits(r[i]) != digits(r[j]))
							continue;
						if (r[i] == r[j])
							continue;
						
						count++;
					}
				}
				add(r,h);
			}
			
			write("Case #" + (c+1) + ": " + count);
			if (c != tests-1)
				write("\n");
			System.out.println("Case #" + (c+1) + ": " + count);
		}
		close();
	}
	
	private void add(int[] r,HashMap<Integer,Integer> h) {
		for (int n : r)
			if (!h.containsKey(n))
				h.put(n, n);
	}
	
	private int digits(int n) {
		if (n == 0)
			return 0;
		return 1 + digits(n/10);
	}
	
	private int[] getRecycledNums(int n) {
		int[] digits = new int[digits(n)];
		int[] nums = new int[digits.length];
		int start;
		int num;
		
		for (int i = 0; i < digits.length; i++)
			digits[i] = (n/pow10[i])%10;
		
		for (int i = 0; i < digits.length; i++) {
			start = i;
			num = 0;
			
			for (int j = 0; j < digits.length; j++) {
				num += digits[start]*pow10[j];
				
				start = (start+1)%digits.length;
			}
			
			nums[i] = num;
		}
		return check(nums);
		
	}
	
	private int[] check(int[] n) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for (int i : n)
			if (!list.contains(i))
				list.add(i);
		
		return toIntArray(list);
	}
	
	private int[] toIntArray(ArrayList<Integer> list) {
		int[] temp = new int[list.size()];
		
		for (int i = 0; i < list.size(); i++)
			temp[i] = list.get(i).intValue();
		return temp;
	}
	private void open() {
		try {
			fw = new FileWriter(new File(filesDir + "recycledAns.txt"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void close() {
		try {
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void write(String s) {
		try {
			fw.write(s);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
