import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Recycled {
	public static void main(String[] args) throws Exception{
		String inputFile = "C-small-attempt0.in";
		
		BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output")));
		long num_cases = Long.parseLong(input.readLine());
		int current_case = 0;
		
		while (current_case < num_cases){
			current_case++;
			
			String[] fields = input.readLine().split(" ");
			int A = Integer.parseInt(fields[0]);
			int B = Integer.parseInt(fields[1]);
			int total = 0;
			
			for (int n = A; n < B; n++){
				for (int m = n+1; m <= B; m++){
					if (isRecycled(n,m)) total++;
				}
			}
			
			System.out.println("Case #" + current_case + ": " + total);
			output.write("Case #" + current_case + ": " + total + "\n");
		}
		output.close();
	}

	private static boolean isRecycled(int n, int m) {
		String sn = ""+n;
		String sm = ""+m;
		if (sn.length() != sm.length()) return false;
		int totaln = 0, totalm = 0;
		for (int i = 0; i < sn.length(); i++){
			totaln += sn.charAt(i);
			totalm += sm.charAt(i);
		}
		if (totaln != totalm) return false;
		for (int i = 0; i < sn.length()-1; i++){
			sm = sm.charAt(sm.length() - 1) + sm.substring(0, sm.length() - 1);
			if (sm.equals(sn)) return true;
		}
		return false;
	}
}

package cj.sample;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class C2012 {

	BufferedReader br;
	PrintWriter pw;
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		C2012 c2012 = new C2012();
		c2012.processInput();		
	}

	public void processInput(){	
		initInFile("/home/Vijitha/GCJ/Testdata/C-small-attempt0.in");
		initOutFile("/home/Vijitha/GCJ/Testdata/C-small-attempt0.out");
		try{
			String l = "";
			// First line
			l = br.readLine();				
			int indx = 1;
			while((l = br.readLine()) != null){				
				String[] ary = l.split(" ");			
				int firstNumber = Integer.parseInt(ary[0]);
				int secondNumber = Integer.parseInt(ary[1]);
				
				int count = getCount(firstNumber, secondNumber);				
				pw.println("Case #" + indx + ": " + count);
				indx++;
			}					
			br.close();		
			pw.close();
		}catch(IOException ex){
			ex.printStackTrace();
		}	
	}  
	
	private int getCount(int firstN, int secondN){		
		int count = 0;	
		
		String firstString = String.valueOf(firstN);
		String secondString = String.valueOf(secondN);
		
		if(firstString.length() != secondString.length())
			return 0;
		
		String fDigit = Character.toString(firstString.charAt(0));
		
		if(firstString.length() == 1)
			return 0;
		
		if(firstString.length() == 2){			
			String sDigit = Character.toString(firstString.charAt(1));			
			while(firstN < secondN){
				firstString = String.valueOf(firstN);				
				fDigit = Character.toString(firstString.charAt(0));
				sDigit = Character.toString(firstString.charAt(1));
				int swapN = Integer.parseInt(sDigit + fDigit);				
				if(swapN > firstN && swapN <= secondN){
					count++;
				}
				firstN++;
			}			
		}
		
		if(firstString.length() == 3){
			String sDigit = Character.toString(firstString.charAt(1));
			String tDigit = Character.toString(firstString.charAt(2));
			while(firstN < secondN){
				firstString = String.valueOf(firstN);
				fDigit = Character.toString(firstString.charAt(0));
				sDigit = Character.toString(firstString.charAt(1));
				tDigit = Character.toString(firstString.charAt(2));
				
				int swapl = Integer.parseInt(tDigit + fDigit + sDigit);
				if(swapl > firstN && swapl <= secondN)
					count++;
				
				int swapBoth = Integer.parseInt(sDigit + tDigit + fDigit);
				if(swapBoth > firstN && swapBoth <= secondN)
					count++;
				
				firstN++;
			}	
			
		}		
		return count;		
	}
	
	
	public void initInFile(String name){		
		try{		
		 br = new BufferedReader(new FileReader(name));		
		}catch(FileNotFoundException fnfe){
			fnfe.printStackTrace();
		}		
	}
	
	public void initOutFile(String name){		
		try{		
		 pw = new PrintWriter(new FileWriter(name));		
		}catch(IOException ex){
			ex.printStackTrace();
		}		
	}
}
