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;
	}

}

import java.util.Scanner;
public class plain
{
    static int A[],B[],n[];
public static void main(String args[])
{
    int t=0;
    Scanner in=new Scanner(System.in);
    t=in.nextInt();
    in.nextLine();
    A=new int[t];
    B=new int[t];
    n=new int[t];
    for(int i=0;i<t;i++)
    {
        String a=in.nextLine();
        int q=Integer.parseInt(a.substring(0,a.indexOf(" ")));
        int c=Integer.parseInt(a.substring(a.indexOf(" ")+1));
        A[i]=q;
        B[i]=c;        
    }
    int num=A[0];
    for(int i=0;i<t;i++)
    {
        n[i]=getTot(i);
        System.out.println("Case #1:"+n[i]);
    }    
}


public static int getTot(int i)
{
    int n=A[i];
    int   g=0,l=(Math.abs(B[i]-A[i]))-2,tot=0;       
    
    for(int j=0;j<l;j++)
    {           
        int n1=getNo(n,i);                
        tot+=n1;        
        n++;
    }
 
    return tot;
}
public static int getNo(int no,int i)
{
    int num=no,a=0,b=0,len=0;
    while(num!=0)        
    {
        a=num%10;        
        num=num/10;
        len++;
    }  
    num=no;
    int arr[]=new int[len];
    a=0;
    int l=0;
    while(num!=0)
    {
       a=num%10;
       arr[l++]=a;       
       num/=10;
    }
    num=no;       
    int no2[]=new int[Math.abs(A[i]-B[i])],g=0;
    int ln=-1,tot=0;
    for(int o=arr.length-1;o>=0;o--)
    {       
     b=arr[0]; 
    for(int p=0;p<(len-1);p++)
    {
        arr[p]=arr[p+1];  
    }
    arr[len-1]=b;        
    int rev=0;
    a=0;
    for(int p=0;p<len;p++)
    {
      rev+=arr[p]*Math.pow(10,p);
    }      
    if(rev>=A[i] && rev<=B[i] && rev>num )
        {            
          for(int k=0;k<(A[i]-B[i]);k++)  
          {  
              if(rev==no2[i])
              {
                  g=1;
                  break;
              }
          }
          if(g==0)
          {
            ln++;
            no2[ln]=rev;
            tot++;      
          }          
          g=0;          
        }             
    }
    return tot;
}

}
