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.
 */


import java.io.*;
/**
 *
 * @author Joydeep
 */
public class J3 {
    public static void main(String[] args) throws IOException{
        String test = "150";

        DataInputStream ds = new DataInputStream(new FileInputStream("d:/C-small-attempt0.in"));
        Writer os = new BufferedWriter(new FileWriter("d:/output.txt"));
        int N = Integer.parseInt(ds.readLine());
        for(int i = 1;i<=N;i++)
        {
            int A=0,B=0,l,counter=0,n,m,pairs=0;
            String temp="";
            String output="Case #"+i+": ";
            String AB = ds.readLine()+" ";
            
            l = AB.length();
            for(int j = 0;j<l;j++)
            {
                char c = AB.charAt(j);
                if(c != ' ')
                {
                    temp = temp+c;
                }    
                else
                {
                  if(counter == 0)  
                  {
                      A = Integer.parseInt(temp);
                      temp = "";
                  }
                  else
                  {
                      B = Integer.parseInt(temp);
                      break;
                  }
                  counter++;
                 
                }
            }    

          
           
            for(n = A;n<=B;n++)
            {
                String check3=""+n;
                String part1,part2;
                String[] matches = new String[100];
                int c = 0;

                    String check = ""+n;

                    int l2 = check.length();
                    int p1 = l2-2;
                    int p2 = l2-1;
                    for(int x = 1;x<l2;x++)
                    {
                        if(p1==0)
                        {
                            part1 =""+ check.charAt(0);
                        }
                        else
                        {
                            part1=check.substring(0, p2);
                        }
                        
                      part2 = ""+check.charAt(p2);
                      check = part2+part1;
                      int num = Integer.parseInt(check);
                      if(num>n && num<=B)
                      {
                          int flag = 0;
                          for(int y = 0 ;y<c;y++)
                          {
                              if(check.equals(matches[y]))
                              {
                               flag = 1 ;  
                               break;
                              }
                          }
                          if(flag == 0)
                          {
                              pairs++;
                              matches[c++]=check;
                          }
                      }
                    }
            }
            output = output +pairs;
            os.write(output);
            String newLine = System.getProperty("line.separator");
            os.write(newLine);
        }
        ds.close();
        os.close();
    }
}