import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

public class ReadFile {
	public static void main(String[] args) throws Exception {
		String srcFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt1.in.txt";
		String destFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt0.out";
		File file = new File(srcFile);
		List<String> readData = new ArrayList<String>();
		FileReader fileInputStream = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileInputStream);
		StringBuilder stringBuilder = new StringBuilder();
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			readData.add(line);
		}
		stringBuilder.append(generateOutput(readData));
		File writeFile = new File(destFile);
		if (!writeFile.exists()) {
			writeFile.createNewFile();
		}
		FileWriter fileWriter = new FileWriter(writeFile);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		bufferedWriter.write(stringBuilder.toString());
		bufferedWriter.close();
		System.out.println("output" + stringBuilder);
	}

	/**
	 * @param number
	 * @return
	 */
	private static String generateOutput(List<String> number) {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < number.size(); i++) {
			if (i == 0) {
				continue;
			} else {
				String tempString = number.get(i);
				String[] temArr = tempString.split(" ");
				stringBuilder.append("Case #" + i + ": ");
				stringBuilder.append(getRecNumbers(temArr[0], temArr[1]));
				stringBuilder.append("\n");
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}

	// /* This method accepts method for google code jam */
	// private static String method(String value) {
	// int intValue = Integer.valueOf(value);
	// double givenExpr = 3 + Math.sqrt(5);
	// double num = Math.pow(givenExpr, intValue);
	// String valArr[] = (num + "").split("\\.");
	// int finalVal = Integer.valueOf(valArr[0]) % 1000;
	//
	// return String.format("%1$03d", finalVal);
	//
	// }
	//
	// /* This method accepts method for google code jam */
	// private static String method2(String value) {
	// StringTokenizer st = new StringTokenizer(value, " ");
	// String test[] = value.split(" ");
	// StringBuffer str = new StringBuffer();
	//
	// for (int i = 0; i < test.length; i++) {
	// // test[i]=test[test.length-i-1];
	// str.append(test[test.length - i - 1]);
	// str.append(" ");
	// }
	// str.deleteCharAt(str.length() - 1);
	// String strReversedLine = "";
	//
	// while (st.hasMoreTokens()) {
	// strReversedLine = st.nextToken() + " " + strReversedLine;
	// }
	//
	// return str.toString();
	// }

	private static int getRecNumbers(String no1, String no2) {

		int a = Integer.valueOf(no1);
		int b = Integer.valueOf(no2);
		Set<String> dupC = new HashSet<String>();
		for (int i = a; i <= b; i++) {
			int n = i;
			List<String> value = findPerm(n);
			for (String val : value) {
				if (Integer.valueOf(val) <= b && Integer.valueOf(val) >= a
						&& n < Integer.valueOf(val)) {
					dupC.add(n + "-" + val);
				}
			}
		}
		return dupC.size();
	}

	private static List<String> findPerm(int num) {
		String numString = String.valueOf(num);
		List<String> value = new ArrayList<String>();
		for (int i = 0; i < numString.length(); i++) {
			String temp = charIns(numString, i);
			if (!temp.equalsIgnoreCase(numString)) {
				value.add(charIns(numString, i));
			}
		}
		return value;

	}

	public static String charIns(String str, int j) {
		String begin = str.substring(0, j);
		String end = str.substring(j);
		return end + begin;
	}
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;


public class Main {

    public static void main(String args[]) {
        {
            FileWriter fostream = null;
            FileInputStream fis = null;
            DataInputStream in = null;
            BufferedReader br = null;
            BufferedWriter bw = null;
            int count = 0;

            int testCase;

           
            ArrayList<Integer> a;
            try {

                fis = new FileInputStream("src\\io\\C-small-attempt1.in");
                //fis = new FileInputStream("src\\io\\C-large-attempt0.in");
                //fis = new FileInputStream("src\\io\\C-small-practice.txt");
                in = new DataInputStream(fis);
                br = new BufferedReader(new InputStreamReader(in));

                fostream = new FileWriter("src\\io\\C-out-small2.txt", false);
               // fostream = new FileWriter("src\\io\\C-out-large.txt", false);
                bw = new BufferedWriter(fostream);
                StringBuffer str, res = new StringBuffer();
                String temp,n ;
                boolean flag;
                char c[],check;
                testCase = Integer.parseInt(br.readLine());
                for (int i = 1; i <= testCase; i++) {
                    
                    a = new ArrayList<Integer>();
                    String input[] = br.readLine().split(" ");
                    str = new StringBuffer();
                    int A = Integer.parseInt(input[0]);
                    int B = Integer.parseInt(input[1]);
                    int total = 0;
                    for (int j = A; j <= B; j++) {
                        //  System.out.println("number " + j);

                        n= String.valueOf(j);
                        flag = true;
                        c = n.toCharArray();
                        check = c[0];
                        for (int p = 1; p < c.length; p++) {
                            if (c[p] == check) {
                                continue;
                            } else {
                                flag = false;
                                break;
                            }
                        }
                        if (!flag) {
                            for (int l = 1; l < c.length; l++) {
                                str = new StringBuffer();
                                for (int k = 0, m = (l % c.length); k < c.length; k++) {
                                    //System.out.print(m);
                                    str.append(c[m]);
                                    m = ((m + 1) % c.length);

                                }

                                temp = str.toString();

                                temp = temp.replaceFirst("^0*", "");

                                //System.out.println(temp);
                                if (Integer.parseInt(temp) >= A && Integer.parseInt(temp) <= B) {
                                    //   System.out.println(j + " "+temp);
                                        a.add(j);
                                        a.add(Integer.parseInt(temp));
                                        
/*                                        if(s.contains(j)){
                                                System.out.print(true + " "+ count+" ");
                                                count++;
                                            }else{
                                                s.add(j); 
                                            }
                                            if(s.contains(Integer.parseInt(temp))){
                                               // System.out.print(true);
                                                //count++;
                                            }
                                            else{
                                            s.add(Integer.parseInt(temp));
                                            }
                  */                  
                                }
                                
                                str = new StringBuffer();
                            }


                        }
                        str = null;



                    }


                    //System.out.println(s.size() / 2);
                        Collections.sort(a);
                        count= 0;
                        for(int q=0;q<a.size();q++){
                            int diff = a.lastIndexOf(a.get(q))-a.indexOf(a.get(q));
                            //System.out.println(diff);
                            count += (diff+1)/2;
                            q = a.lastIndexOf(a.get(q));
                            //q += diff;
                            
                        }
                      //  System.out.println(count/2);
                       
                    if (i == testCase) {
                        bw.write("Case #" + i + ": " + count / 2);
                        bw.flush();
                    } else {
                        bw.write("Case #" + i + ": " + count / 2 + "\n");
                        bw.flush();
                    }
                count =0;
                }
              //  System.out.println();
            //    System.out.println(a.size());
            } catch (FileNotFoundException ex) {
            } catch (IOException ex) {
            } finally {
                try {
                    fostream.close();
                    fis.close();
                    br.close();

                } catch (IOException ex) {
                }
            }
        }
    }
}
