import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Recycled {
	public static void main(String[] args) throws Exception{
		String inputFile = "C-small-attempt0.in";
		
		BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output")));
		long num_cases = Long.parseLong(input.readLine());
		int current_case = 0;
		
		while (current_case < num_cases){
			current_case++;
			
			String[] fields = input.readLine().split(" ");
			int A = Integer.parseInt(fields[0]);
			int B = Integer.parseInt(fields[1]);
			int total = 0;
			
			for (int n = A; n < B; n++){
				for (int m = n+1; m <= B; m++){
					if (isRecycled(n,m)) total++;
				}
			}
			
			System.out.println("Case #" + current_case + ": " + total);
			output.write("Case #" + current_case + ": " + total + "\n");
		}
		output.close();
	}

	private static boolean isRecycled(int n, int m) {
		String sn = ""+n;
		String sm = ""+m;
		if (sn.length() != sm.length()) return false;
		int totaln = 0, totalm = 0;
		for (int i = 0; i < sn.length(); i++){
			totaln += sn.charAt(i);
			totalm += sm.charAt(i);
		}
		if (totaln != totalm) return false;
		for (int i = 0; i < sn.length()-1; i++){
			sm = sm.charAt(sm.length() - 1) + sm.substring(0, sm.length() - 1);
			if (sm.equals(sn)) return true;
		}
		return false;
	}
}

package qual1;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;

public class C {
	public static void main(String[] args) throws IOException {
		// Use BufferedReader rather than RandomAccessFile; it's much faster
		BufferedReader f = new BufferedReader(new FileReader("C-small-attempt0.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("C-small-attempt0.out")));

		int cases = Integer.parseInt(f.readLine());
		for (int zz = 1; zz <= cases; zz++) {
			StringTokenizer st = new StringTokenizer(f.readLine());
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			
			int[] list = new int[b-a+1];
			for (int i=a; i<=b; i++) {
				list[i-a] = i;
			}
			
			HashMap<Integer,ArrayList<Integer>> dump = new HashMap<Integer, ArrayList<Integer>>();
			
			long ans = 0;
			for (int i=a; i<=b; i++)  {
				if (list[i-a]==0)
					continue;
				String x = i+"";
				ArrayList<Integer> xzc = new ArrayList<Integer>();
				for (int j=1; j<x.length(); j++) {
					int t1 = (int) (i%(Math.pow(10,j)));
					int tx = (int) (t1*(Math.pow(10,(x.length()-j))));
					int t2 = (int) (tx + i/(Math.pow(10,j)));
					if (t2==i)
						continue;
					if (t2>=a && t2<=b) {
						if (list[t2-a]==0) {
							continue;
						}
						list[t2-a] = 0;
						xzc.add(t2);
					}
				}
				dump.put(i, xzc);
			}
			
			for (int key : dump.keySet()) {
				ArrayList<Integer> xzc = dump.get(key);
				if (xzc.isEmpty())
					continue;
				//Collections.sort(xzc);
				//System.out.println(key+" : "+xzc);
				if (xzc.size()==1)
					ans++;
				else
					ans+= ((xzc.size()+1)*(xzc.size())/2);
					
			}
			System.out.println("Case #" + zz + ": "+ans);
			
			out.println("Case #" + zz + ": "+ans);
		}

		out.close(); // close the output file
		System.exit(0); // don't omit this!
	}

}