import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class CopyOfCopyOfMain {

	static int ndigits(int n) {
		return (int) (Math.log10(n) + 1);
	}

	static int rot(int n, int dig) {
		return (int) ((n % 10) * Math.pow(10, dig - 1)) + (int) (n / 10);
	}

	public static void main(String[] args) throws FileNotFoundException {
		String file = "C-large";
		Scanner in = new Scanner(new File(file + ".in"));
		PrintWriter out = new PrintWriter(new File(file + ".out"));

		int T = in.nextInt();
		for (int t = 1; t <= T; t++) {
			int A = in.nextInt();
			int B = in.nextInt();

			int c = 0;
			int[] list = new int[ndigits(B)];
			numbers: for (int i = A; i <= B; i++) {
				int digs = ndigits(i);
				int rot = i;
				int pairs = 0;

				Arrays.fill(list, -1);
				list[0] = i;

				digits: for (int j = 1; j <= digs + 1; j++) {

					rot = rot(rot, digs);
					if(rot < i)
						continue;

					if (A <= rot && rot <= B) {
						for (int k = 0; k < j; k++) {
							if (list[k] == rot) {
								continue digits;
							}
						}
						pairs++;
						list[j] = rot;
					}
				}

				//c += pairs * (pairs + 1) / 2;
				c += pairs ;
				// c+=digs;
			}
			out.println("Case #" + t + ": " + c);
		}

		in.close();
		out.close();
	}
}

import java.text.*;
import java.util.*;
import java.lang.*;


public class solutionRunner {

	public solutionRunner(){
		run();
	}
	
	private void run(){

		int numberOfLines = 1;
		inpOutHandler io1 = new inpOutHandler();
		testCase TC = io1.readInput("a.txt", numberOfLines);
		String result="";int _i=1;
		for(String case1:TC.cases){
			//int[] a1 = intAfromString(case1.substring(case1.indexOf('\n') +1));
			pairs = new HashSet<Long>();
			long[] a1 = longAfromString(case1.substring(case1.indexOf('\n') +1));
			if(_i==TC.cases.length){
				 result +="Case #" + (_i++) +": " + numberOfG(a1);
			}else{
				 result +="Case #" + (_i++) +": " + numberOfG(a1) + "\n";
			}
		}
		io1.createOutput("OUTPUT.txt", result);
		//System.out.println(result);
		 
		/*
		double xx = 1231212.9;
		DecimalFormat threeDec = new DecimalFormat(".0000000");
		String shortString = (threeDec.format(xx));
		*/
		
	}


	private long numberOfG(long[] a) {
		long min = a[0];long max = a[1];
		//System.out.println("<" + min + " , " + max + ">");
		for(long n=min; n<max ; n++){
			long[] rs = largerRecycles(n);
			if(rs!=null){
				for(long r:rs){
					if(r>=min && r<=max){
						long temp = encodePair(n,r, max+1);
						if(!pairs.contains(temp))
							pairs.add(temp);
					}
				}
			}
		}
		/*
		for(long _x:pairs){
			long[] _tem = decodePair(_x, max+1);
			System.out.println("<" + _tem[0] + " , " + _tem[1] + ">");
		}
		*/
		return pairs.size();
	}
	// returns numbers r : r_i > n and r_i can be recycled from n
	private long[] largerRecycles(long n) {
		if(n<10) return null;
		String _sN = ""+n;int len = _sN.length();
		ArrayList<Long> recycles = new ArrayList<Long>();
		long r = leftRotate(n,len);
		while(r!=n){
			if(r>n)
				recycles.add(r);
			r = leftRotate(r,len);
		}
		if(recycles.size()<1) return null;
		long[] rs = new long[recycles.size()];
		for(int i=0; i<recycles.size();i++)
			rs[i] = recycles.get(i);
		
		return rs;
	}
	private long leftRotate(long r, int len) {
		long base = (long)Math.pow(10, len-1);
		return ((r%base)*10 + r/base);
	}
	// x < y < STEP
	private long encodePair(long x, long y, long STEP){
		return (x + y*STEP) ;
	}
	// a[0] < a[1] < STEP
	private long[] decodePair(long x, long STEP){
		long[] a = new long[2];
		a[0] = x%STEP; a[1] = x/STEP;
		return a;
	}
	HashSet<Long> pairs;// = new HashSet<Long>();

	
	private int[] intAfromString(String s) {
		String[] words = s.split(" ");
		int[] a = new int[words.length];
		for(int i=0;i<a.length;i++)
			a[i] = Integer.parseInt(words[i]);
		
		return a;
	}
	private double[] doubleAfromString(String s) {
		String[] words = s.split(" ");
		double[] a = new double[words.length];
		for(int i=0;i<a.length;i++)
			a[i] = Double.parseDouble(words[i]);
		
		return a;
	}
	private long[] longAfromString(String s) {
		String[] words = s.split(" ");
		long[] a = new long[words.length];
		for(int i=0;i<a.length;i++)
			a[i] = Long.parseLong(words[i]);
		
		return a;
	}
	private void swap(int[] a, int i, int j) {
		if(i==j || a.length<2) return;
		int temp = a[i];
		a[i]=a[j];a[j]=temp;
	}
	private int max(int x, int y){
		if(x>y){
			return x;
		}else{
			return y;
		}
	}
}
