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

}

package com.prob1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class ClassB {
	public static void main(String args[]){
		   try{
		      BufferedReader in = new BufferedReader(new FileReader("D://NewFolder2//C-small-attempt0.in")); 
		      //BufferedReader in = new BufferedReader(new FileReader("D://NewFolder2//C-large.in")); 
		      PrintWriter out = new PrintWriter(new FileWriter("D://NewFolder2//test1.txt")); 
		      int c =1;
		      //int x =0;
		      while (in.ready()){
		    	  String text = in.readLine();
		    	  
		    	  if(c != 1){
		    		  StringTokenizer tokenizer = new StringTokenizer(text," "); 
			    	  String[] allText = new String[2]; 
			    	  int pos = 0; 
			    	  while (tokenizer.hasMoreTokens()) 
			    	        allText[pos++] = tokenizer.nextToken();
			    	long lower_limit = Long.parseLong(allText[0]);
					long upper_limit = Long.parseLong(allText[1]);
					long result = countRecycledNums(lower_limit,upper_limit);
    	    		  String text1 = "Case #"+(c-1)+": "+result;
			          out.println(text1);
			          c++;
		    	  }
		    	  else{
		    		 // x = Integer.parseInt(text); 
		    		  c++;
		    	  }
		      }
		      out.close();
		      in.close();
		   }catch(IOException e){
		      System.out.print("Exception");
		   }	
	 } //end of main

	
  private static int countNumDigits( long number)
	{
		int count = 1;
		count = 1 + (int)Math.floor(Math.log10(number));
		return count;
	}
  
  
//Rotate a number by specified positioon
  private static long cyclicRotate( long num, int numLen, int pos )
  {
  	long new_num, suffix,prefix;
  	long compute_by = (long)(Math.pow((double)10,(numLen - pos )));
  	suffix =  num / compute_by ;
  	prefix =  num % compute_by ;

  	new_num = prefix *  (long)Math.pow((double)10, pos ) + suffix;

  	return new_num;
  }
  
  
  static long countRecycledNums(long lower_limit, long upper_limit)
  {
  	long uniq_recycle_pair_count = 0;
  	int num_digits =  countNumDigits ( lower_limit);
  	if ( num_digits == 1 ){ 
  		return 0;
  		}

  	for ( int counter = (int)lower_limit ; counter < upper_limit ; counter++)
  	{
  		Set set = new HashSet();

  		for ( int pos = 1 ; pos < num_digits ; pos++ ) 
  		{
  			long recycled_num = cyclicRotate(counter,num_digits,pos);
  			if ( recycled_num <= counter)
  			{
  				continue;
  			}
  			if ( lower_limit <= recycled_num && upper_limit >= recycled_num )  
  			{
  				uniq_recycle_pair_count++;
  				set.add(recycled_num);
  				
  			}
  		}
  	}
  	return uniq_recycle_pair_count;
  }
}