import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t=sc.nextInt();
		int casos=1, a, b, n, m, cont;
		while(t!=0){
			a=sc.nextInt();
			b=sc.nextInt();
			if(a>b){
				int aux=a;
				a=b;
				b=aux;
			}
			System.out.printf("Case #%d: ",casos++);
			if(a==b){
				System.out.printf("%d\n",0);
			}else{
				cont=0;
				for(n=a;n<b;n++){					
					for(m=n+1;m<=b;m++){					
						if(isRecycled(n,m)) cont++;
					}
				}
				System.out.printf("%d\n",cont);
			}
			t--;
		}		
	}
	public static boolean isRecycled(int n1, int n2){
		String s1, s2, aux;
		s1 =  String.valueOf( n1 );
		s2 =  String.valueOf( n2 );		
		boolean r = false;		
		for(int i=0;i<s1.length();i++){
			aux="";
			aux=s1.substring(i,s1.length())+s1.substring(0,i);
//			System.out.println(aux);
			if(aux.equals(s2)){
				r=true;
				break;
			}
		}		
		return r;
	}
}

package com.phonescreen;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

public class RecycledNumber {

	private static int count;

	public static void main(String[] args) {
		try {
			DataInputStream br = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("H:\\C-small-attempt1.in"))));
			String s = br.readLine();
			count = Integer.parseInt(s);
			if (count < 1 || count > 50) {
				return;
			}
			for (int i = 0; i < count; i++) {
				String[] testCase = br.readLine().split(" ");
				if (testCase[0].length() != testCase[1].length()) {
					return;
				}
				int a = Integer.parseInt(testCase[0]);
				int b = Integer.parseInt(testCase[1]);
				if (a < 1 || b > 1000 || a > b) {
					return;
				}
				 System.out.println("Case #" + ( i+1) + ": " + getRecycledNumberCount(a, b));
			}

		} catch (Exception e) {
			System.err.println("Error:" + e.getMessage());
		}
	}

	private static int getRecycledNumberCount(int a, int b) {
		int count = 0;
		for (int i = a; i <= b; i++) {
			for (int j = i; j <= b; j++) {
				if (isRecycledNumbers(i + "", j + "")) {
					count++;
				}
			}
		}
		return count;
	}

	private static boolean isRecycledNumbers(String num1, String num2) {
		for (int i = 0; i < num1.length(); i++) {
			if (num2.indexOf(num1.charAt(i)) == -1 || num1.indexOf(num2.charAt(i)) == -1) {
				return false;
			}
		}
		for (int i = num1.length() - 1; i >= 0; i--) {
			String subStr = num1.substring(i);
			if (num2.indexOf(subStr) == -1) {
				subStr = num1.substring(i + 1);
				String s = subStr + num1.substring(0, (num1.length() - subStr.length()));
				if (s.equals(num2)) {
					return true;
				}
			}
		}
		return false;
	}
}
