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

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author daniele
 */
public class GCJ_C {
    
    public static void main(String[] args) throws Exception{
        
        
        
        Scanner in = new Scanner(new File(args[0]));
        FileWriter out = new FileWriter("/home/daniele/Scrivania/Output");
        

        int prove=in.nextInt();
        int min,max;
        int cifre;
        int cifreaus;
        String temp;
        int aus;
        int count=0;
        ArrayList<Integer> vals = new ArrayList<Integer>();
        int jcount=0;
        ArrayList<Integer> perm = new ArrayList<Integer>();
        
        out.write("");
        out.flush();
        
        for(int i=1;i<=prove;i++){
            
            out.append("Case #" +i+": ");out.flush();
            
            min=in.nextInt();
            max=in.nextInt();
            
            for(int j=min;j<=max;j++){
               if(!vals.contains(j)){
                    temp=""+j;
                    cifre=temp.length();
                    aus=j;
                    perm.add(j);
                    for(int k=1;k<cifre;k++){
                        aus=rotateOnce(aus);
                        temp=""+aus;
                        cifreaus=temp.length();//elusione zeri iniziali
                        if(aus>=min && aus<=max && aus!=j && cifreaus==cifre && nobody(vals,perm)){ 
                            perm.add(aus);
                            jcount ++;
                            
                        }
                        
                    }
                    while(jcount>0){count+=jcount; jcount--;}
                    vals.addAll(perm);
                    perm= new ArrayList<Integer>();
                    
                }
            }
            out.append(count+"\n");out.flush();
            count=0;
            vals= new ArrayList<Integer>();
        }
        
        
        
    }
        static public int rotateOnce(int n){
            String s=""+n;
            if(s.length()>1){
                s=s.charAt(s.length()-1) + s.substring(0,s.length()-1);
                n=Integer.parseInt(s);
            }
            return n;
        }    
        
        static public boolean nobody(ArrayList<Integer> v,ArrayList<Integer> a){;
            for(int i=0;i<a.size();i++)
                if(v.contains(a.get(i))) return false;
            return true;
        } 
    
}


import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class GoogleC {

	public static void main(String[] args) throws FileNotFoundException{
		Scanner scan = new Scanner(new File("B-small.txt"));
		//finit2();
		int n = scan.nextInt();
		for (int i = 0; i < n; i++){
			ht.clear();
			System.out.println("Case #" + (i+1) + ": " + sum(scan.nextInt(), scan.nextInt()));
		}
	}

	public static long sum(int a, int b){
		ht = new HashSet<Pair>();
		long ret = 0;
		for (int i = a; i <= b; i++){
			ret += run(i, a, b);
		}
		return ret;
	}

	public static HashSet<Pair> ht = new HashSet<Pair>();

	private static class Pair{
		int m, n;
		public Pair(int m, int n){
			this.m = m;
			this.n =n;
		}
		public int hashCode(){
			return 31*m+n;
		}

		@Override
		public boolean equals(Object o){
			Pair p = (Pair)o;
			return p.m == m && p.n == n;
		}
	}

	public static int run(Integer n, int a, int b){
		/*StringBuilder sb = new StringBuilder(n.toString());
		StringBuilder orig = new StringBuilder(n.toString());
		for (int i = 0; i < sb.length(); i++){
			sb = sb.substring(1);

		}*/
		int count = 0;
		String orig = n.toString();
		String cur = n.toString();
		for (int i = 0; i < orig.length()-1; i++){
			char c = cur.charAt(0);

			cur = cur.substring(1) + c;
			if (cur.charAt(0) == '0') continue;
			int k = Integer.parseInt(cur);
			if (a <= k && k < n && n <= b && cur.length() == orig.length()){
				Pair p = new Pair(k, n);
				if (!ht.contains(p)){
					count++;
					//System.out.println(k + " " + n);
					ht.add(p);
				}
			}
		}
		return count;
		/*char[] cur = n.toString().toCharArray();
		int count = 0;
		for (int start = 1; start < cur.length; start++){
			boolean failed = false;
			if (arr[start] == 0){
				failed = true;
			}
			for (int i = 0; !failed && i < cur.length; i++){
				if (cur[(start+i)%cur.length] != cur[i]){
					failed = true;
				}
			}
			if (!failed){
				count++;
			}
		}*/
	}

	public static void init2(){
		for (int i = 2; i < arr.length; i++){
			//run(i);
		}
	}

	public static boolean check(Integer a, Integer b){
		if (a.toString().length() != b.toString().length()) return false;
		if (a==123 && b==231){
			System.out.println("here");
		}
		String str = a.toString() + a.toString();
		return str.contains(b.toString());
	}

	public static void init(){
		for (int i = 2; i < arr.length; i++){
			int count = 0;
			for (int j = 1; j < i; j++){
				if (check(j, i)) count++;
			}
			arr[i] = count + arr[i-1];
		}
	}

	static int cap = 3000;
	public static int[] arr = new int[cap+1];

	public static int doit(int n, int m){
		return arr[m] - arr[n];
	}

}
