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

public class C {
	HashMap map = new HashMap<Character, Character>();

	public static void main(String args[]) {
		try {
			InputStreamReader isr = null;
			try {
				isr = new InputStreamReader(System.in, "UTF-8");
				BufferedReader br = null;
				try {
					br = new BufferedReader(isr);
					new C().solve(br);
				} finally {
					if (br != null) br.close();
				}
			} finally {
				if (isr != null) isr.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			System.exit(1);
		}
	}

	public void solve(BufferedReader br) throws IOException {
		String line = null;
		int T = Integer.parseInt(br.readLine());
		for (int count = 1; count <= T; count++) {
			doOne(count, br.readLine());
		}
	}

	public void doOne(int count, String line) {
		TreeMap<String, List<String>> map =
			new TreeMap<String, List<String>>();
		List<String> list = null;
		List<String> keys = null;
		String key = null;
		String nstr = null;
		String sstr = null;
		String lines[] = line.split(" ");
		int startNum = Integer.parseInt(lines[0]);
		int endNum = Integer.parseInt(lines[1]);
		for (int n = startNum; n <= endNum; n++) {
			nstr = Integer.toString(n);
			keys = makeKeys(nstr);
			key = null;;
			for (int i = 0; i < keys.size(); i++) {
				key = keys.get(i);
				list = map.get(key);
				if (list != null)
					break;
			}
			if (list == null) {
				key = nstr;
				list = new ArrayList<String>();
			}
			list.add(nstr);
			map.put(key, list);
		}

		Set <String>keySet = map.keySet();  
		Iterator <String>keyIte = keySet.iterator();  
		int total = 0;
		while(keyIte.hasNext()) {
			String mapKey = keyIte.next();  
			List<String> data = map.get(mapKey);  
			// System.out.println(mapKey + ":" + data);

			if (data.size() > 1)
				total += data.size() * (data.size() - 1) / 2;
		}

		System.out.println("Case #" + count + ": " + total);
	}

	public List<String> makeKeys(String nstr) {
		List<String>list = new ArrayList<String>();
		char nchar[] = new char[nstr.length()];
		for (int i = 0; i < nchar.length; i++)
			nchar[i] = nstr.charAt(i);
		char kchar[] = new char[nstr.length()];
		for (int startc = 0; startc < nchar.length; startc++) {
			for (int c = 0; c < nchar.length; c++)
				kchar[c] = nchar[(startc + c) % nchar.length];
			list.add(new String(kchar));
		}
		return list;
	}
}
