import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Recycle {

	private static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
	private static HashSet<Integer> toSkip = new HashSet<Integer>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int T = reader.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int a = reader.nextInt();
			int b = reader.nextInt();
			long before = System.currentTimeMillis();
			int res = doit(a, b);
			System.out.printf("Case #%d: %d\n", tc, res);
			long after = System.currentTimeMillis();
//			System.out.println("time taken: " + (after - before));
		}
	}

	private static int doit(int a, int b) {
		int count = 0;
		HashSet<Integer> skip = new HashSet<Integer>(toSkip);
		for (int i = a; i <= b; i++) {
			if (skip.contains(i))
				continue;
			HashSet<Integer> arr = generate(i);
//			if(arr.size() > 1) {
//				map.put(i, arr);
//			}
			// System.out.println(i + " --> " +
			// Arrays.deepToString(arr.toArray()));
			int temp = 0;
			if (arr.size() > 1) {
				for (int x : arr) {
					if (x >= a && x <= b) {
						temp++;
						skip.add(x);
					}
				}
				count += temp * (temp - 1) / 2;
			}
//			System.out.println("not skipping " + i + " gen: " + arr.size()+ " " + (temp * (temp - 1) / 2));
		}
		return count;
	}

	private static HashSet<Integer> generate(int num) {
		if(map.containsKey(num)) {
			HashSet<Integer> list = map.get(num);
			return list;
		}
		HashSet<Integer> list = new HashSet<Integer>();
		String s = "" + num;
		list.add(num);
		int len = s.length();
		for (int i = 1; i < len; i++) {
			String temp = s.substring(i) + s.substring(0, i);
			// System.out.println("temp: " + temp);
			if (temp.charAt(0) == '0')
				continue;
			int x = Integer.parseInt(temp);
			if( x <= 2000000) {
				list.add(x);
			}
		}
		if(list.size() > 1) {
			map.put(num, list);
		} else {
			toSkip.add(num);
		}
		return list;
	}

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gjam;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

/**
 *
 * @author iNahoo
 */
public class GJam {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/iNahoo/Desktop/C-small-attempt0.in"));
        String line="";
        int i=1;
        
        File output=new File("/Users/iNahoo/Desktop/gtest/test_.out");
        FileWriter fw = new FileWriter(output.getName());
    	    BufferedWriter bw = new BufferedWriter(fw);
    	    

        
        
        line = bufferedReader.readLine();
        while ((line = bufferedReader.readLine()) != null) {
               String out_="Case #"+i+": "+reclyC(line);
               System.out.println(out_);
               bw.write(out_);
               i++;
        }
        
        
        bw.close();
        
        
        
        //System.out.print(reclyString("2"));
        //System.out.println("Case #"+1+": "+langVert("ejp mysljylc kd kxveddknmc re jsicpdrysi"));
        // TODO code application logic here
    }
    
    public static int reclyC(String s){
        String[] ij=s.split(" ");
        int i=Integer.parseInt(ij[0]);
        int j=Integer.parseInt(ij[1]);
        int temp=0;
        String[] input=new String[j-i+1];
        for(int k= 0;k<input.length;k++){
            input[k]=""+i;
            i++;
        }
        ArrayList past=new ArrayList();
        
        for(int k= 0;k<input.length;k++){
            String temp_t=input[k];

            if(!temp_t.equals("xxx")){
            for(int l=1;l<input[k].length();l++){

                if(l==1)temp_t=reclyString(input[k]);
                else temp_t=reclyString(temp_t);
                
                String temp_=""+Integer.parseInt(temp_t);
                
                //System.out.println(input[k]+"-->"+temp_);                
                for(int m=0;m<input.length;m++){
                    if(temp_.equals(input[m])){

                      if((k!=m)&&(!past.contains(""+input[m]+input[k]))){
                          //System.out.println(input[k]+"::"+input[m]);  
                          past.add(""+input[k]+input[m]);
                          temp++;
                      }
                    }
                }
            }}
        }
        
        
        return temp;
    }
    
    public static String reclyString(String s){
        String temp="";
        temp = s.substring(1)+s.charAt(0);
        return temp;
        
    }
    
    
    public static String langVert(String s){
        String temp="";
        String[] spl=s.split(" ");
        
        for(int i=0;i<spl.length;i++){
            for(int j=0;j<spl[i].length();j++){
              temp=temp+charVert(spl[i].charAt(j));
            }
            temp=temp+" ";
        }
        
        return temp;
    }
    
    public static Character charVert(char ch){
        Character temp=null;
        switch(ch){
            case 'a':temp='y';break;
            case 'b':temp='h';break;
            case 'c':temp='e';break;
            case 'd':temp='s';break;
            case 'e':temp='o';break;
            case 'f':temp='c';break;
            case 'g':temp='v';break;
            case 'h':temp='x';break;                
            case 'i':temp='d';break;
            case 'j':temp='u';break;
            case 'k':temp='i';break;
            case 'l':temp='g';break;                
            case 'm':temp='l';break;
            case 'n':temp='b';break;
            case 'o':temp='k';break;
            case 'p':temp='r';break;
            case 'q':temp='z';break;
            case 'r':temp='t';break;
            case 's':temp='n';break;
            case 't':temp='w';break;                
            case 'u':temp='j';break;
            case 'v':temp='p';break;
            case 'w':temp='f';break;
            case 'x':temp='m';break;
            case 'y':temp='a';break;
            case 'z':temp='q';break;                 
        
        }
        return temp;
    } 
}
