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 nithin.codejam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
	static int valid_pairs[][];
	static int no_valid_pairs=0;
	public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int t,i;
		t=Integer.parseInt(br.readLine());
		
		for(i=0;i<t;i++)
		{
			st=new StringTokenizer(br.readLine());
			int a,b;
			int result=0;
			a=Integer.parseInt(st.nextToken());
			b=Integer.parseInt(st.nextToken());
			
			no_valid_pairs=0;
			valid_pairs=new int[1000][2]; // Will not work for Large Input
			boolean ex1=true,ex2=true,ex3=true;
			
			if(b<=10)
			{
				if(b!=10)
				{
					ex1=false;
					ex2=false;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						ex1=false;
						ex2=false;
						ex3=false;
						result=1;
					}
					
				}
			}
			else if(b<=100)
			{
				if(b!=100)
				{
					ex1=true;
					ex2=false;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						//Extracting 2 digits from 100 will give 1
						ex1=true;
						ex2=false;
						ex3=false;
						result=1;
					}
				}
			}
			else if(b<=1000)
			{
				if(b!=1000)
				{
					ex1=true;
					ex2=true;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						//Extracting 3 digits from 1000 will give 1
						ex1=true;
						ex2=true;
						ex3=false;
						result=1;
					}
				}
			}
			else
			{
				ex1=true;
				ex2=true;
				ex3=true;
			}
			
			if(ex1==true)
			{
				int j;
				for(j=a;j<=b;j++)
				{
					int last=j%10;
					int rem=j/10;
					int num=-1;
					if(rem!=0)
					{
						if(rem<10)
						{
							num=last*10+rem;
						}
						else if(rem<100)
						{
							num=last*100+rem;
						}
						else if(rem<1000)
						{
							num=last*1000+rem;
						}
						else
						{
							System.out.println("Not a Valid Case for Small Input");
						}
						if(num>=a && num<=b && j!=num)
						{
							if(j<=num)
							{
								if(add(j,num))
									result++;
							}
							else
							{
								if(add(num,j))
									result++;
							}
						}
					}
					

				}
			}
			if(ex2==true)
			{
/*				int j;
				for(j=101;j<=b;j++)
				{
					int last=j%100;
					int rem=j/100;
					int num=-1;
					if(rem!=0)
					{
						if(rem<10) //if rem=10 then it means it was obtained from a number like 1025 by extracting last 2 digits, so you have to multiply last by 100
						{
							num=last*100+rem;
						}
						else if(rem<100)
						{
							num=last*1000+rem;
						}
						else if(rem<1000)
						{
							num=last*10000+rem;
						}
						else
						{
							System.out.println("Not a Valid Case for Small Input");
						}
						if(num>=a && num<=b && j!=num)
						{

							if(add(j,num)) //num is actually m, it is obtained by moving some digits from back of j to the front.
								result++;

						}
					}
					

				}*/
			}
			if(ex3==true)
			{
				//Do Nothing
			}
			System.out.println("Case #"+(i+1)+": "+result);
/*			int j;
			for(j=0;j<no_valid_pairs;j++)
				System.out.println("0th: "+valid_pairs[j][0]+"\t1th: "+valid_pairs[j][1]);*/
		}
	}
	static boolean add(int m,int n)
	{
		//Pass such that m<n
		boolean uniq_pair=true;
		int i;
		
		for(i=0;i<no_valid_pairs;i++)
		{
			int a=valid_pairs[i][0];
			int b=valid_pairs[i][1];
			if((a==m && b==n) || (b==m && a==n))
				return false;
		}
		valid_pairs[no_valid_pairs][0]=m;
		valid_pairs[no_valid_pairs][1]=n;
		no_valid_pairs++;
		return uniq_pair;
	}
}

