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 mgg.problems;

import java.util.ArrayList;
import java.util.List;

import mgg.utils.CombinatoryUtils;
import mgg.utils.FileUtil;
import mgg.utils.StringUtils;
import mgg.utils.Triplet;

/**
 * @author manolo
 * @date 14/04/12
 */
public class ProblemB {

	public final static String EXAMPLE_IN = "B-example.in";
	public final static String EXAMPLE_OUT = "B-example.out";

	public final static String B_SMALL_IN = "B-small-practice.in";
	public final static String B_SMALL_OUT = "B-small-practice.out";

	public final static String B_LARGE_IN = "B-large-practice.in";
	public final static String B_LARGE_OUT = "B-large-practice.out";

	public final static String delim = " ";

	public static String solve(FileUtil util) {
		
		String line = util.getLine();
		List<Integer> listOfArgs = StringUtils.stringWithIntegersToList(line, delim);
		//System.out.println("\tArgs = " + listOfArgs);
		
		int googlers = listOfArgs.get(0);
		System.out.println("\tGooglers = " + googlers);
		
		int surprisingScores = listOfArgs.get(1);
		System.out.println("\tSurprising scores = " + surprisingScores);
		
		int leastScore = listOfArgs.get(2);
		System.out.println("\tMinimum = " + leastScore);
		
		List<Integer> listOfScores = listOfArgs.subList(3, listOfArgs.size());
		System.out.println("\tScores = " + listOfScores);
		
		List<Triplet> listOfTriplets;
		List<List<Triplet>> listOfListOfTriplets = new ArrayList<List<Triplet>>();
		for(int score : listOfScores){
			
			listOfListOfTriplets.add(CombinatoryUtils.possibleTriplets(score));
		}
		
		System.out.println("\tPossible triplets: " + listOfListOfTriplets);
		
		List<List<Triplet>> scenarios = calculateScenarios(listOfListOfTriplets, surprisingScores);
		
		System.out.println("\tPossible scenarios: " + scenarios);

		return null;
	}

	private static List<List<Triplet>> calculateScenarios(
			List<List<Triplet>> listOfListOfTriplets, int surprisingScores) {
		
		return null;
	}

}
