package it.simone.google.code.jam2012;

import java.util.HashSet;
import java.util.Set;

public class RecycledNumber implements GoogleCodeExercise {

	int a = 0;
	int b = 0;
	Set<Couple> distinctCouple = null;
	long maxTime = 0;

	private class Couple {
		int x = 0;
		int y = 0;

		public Couple(int x, int y) {
			this.x = x;
			this.y = y;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + x;
			result = prime * result + y;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Couple other = (Couple) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (x == other.x && y == other.y)
				return true;
			if (x == other.y && y == other.x)
				return true;
			return false;
		}

		private RecycledNumber getOuterType() {
			return RecycledNumber.this;
		}

	}

	@Override
	public String execute(String line) {
		int result = 0;

		String[] data = line.split(" ");
		a = Integer.parseInt(data[0]);
		b = Integer.parseInt(data[1]);
		initialize();

		int n = a;
		//
		while (n <= b) {
			result += analize(n);
			n++;
//			if (n % 1000 == 0)
//				System.out.println(n);
		}

		System.out.println(result);
		return "" + result;
	}

	@Override
	public void initialize() {
		distinctCouple = new HashSet<Couple>();
	}

	private int analize(int number) {
		String numString = "" + number;
		initialize();
		for (int i = 1; i < numString.length(); i++) {
			int m = shift(numString, i);
			if (a <= number && number < m && m <= b) {
				Couple couple=new Couple(number,m);
				if (!distinctCouple.contains(couple))
					distinctCouple.add(couple);
			}
		}
		return distinctCouple.size();
	}

	private int shift(String numString, int posx) {

		String result = null;
		result = "" + numString.substring(posx) + numString.substring(0, posx);

		return Integer.parseInt(result.toString());
	}

}

import java.io.*;
import java.util.*;

class Recycled
{
	static boolean[] check = new boolean[2000001];

	public static void main (String [] args) throws IOException 
	{
		BufferedReader in = new BufferedReader(new FileReader("C-large.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("C-large.out")));
		
		StringTokenizer st = new StringTokenizer(in.readLine());
		int numCases = Integer.parseInt(st.nextToken());
		
		for (int caseIndex = 0; caseIndex < numCases; caseIndex++)
		{
			out.print("Case #" + (caseIndex + 1) + ": ");
			
			st = new StringTokenizer(in.readLine());
			int min = Integer.parseInt(st.nextToken());
			int max = Integer.parseInt(st.nextToken());
			
			// Reset check
			for (int i = 0; i <= 2000000; i++)
			{
				check[i] = false;
			}
			
			int result = 0;
			for (int i = min; i <= max; i++)
			{
				result += countRecycledNumbers(i, min, max);
			}
			
			out.print(result);
			
			out.println();
		}
		
		out.close();
	}
	
	public static ArrayList<Integer> generateRecycledNumbers(int x)
	{
	
		// Count number of digits
		int y = x;
		int numDigits = 0;
		while (y > 0)
		{
			numDigits ++;
			y /= 10;
		}
		
		// Get the list of digits
		int[] listDigits = new int[numDigits];
		int index = numDigits-1;
		while (x > 0)
		{
			listDigits[index] = x%10;
			x /= 10;
			index --;
		}
		
		// Generate all possible recycled number from x
		ArrayList<Integer> result = new ArrayList<Integer>();
		
		for (int i = 0; i < numDigits; i++)
		{
			if (listDigits[i] == 0)
				continue;
			
			int num = 0;
			
			for (int j = 0; j < numDigits; j++)
			{
				num = num*10 + listDigits[(i+j)%numDigits];
			}
			
			
			if (num <= 2000000)
				result.add(num);
		}
		
		return result;
	}
	
	public static int countRecycledNumbers(int x, int min, int max)
	{
		if (check[x])
		{
			return 0;
		}
		else
		{
			ArrayList<Integer> recycledNumbers = generateRecycledNumbers(x);
			Collections.sort(recycledNumbers);
			
			int numDistinctNumbers = 0;
			
			for (int i = 0; i < recycledNumbers.size(); i++)
			{
				check[recycledNumbers.get(i)] = true;
				
				if (i > 0 && recycledNumbers.get(i) > recycledNumbers.get(i-1) && recycledNumbers.get(i) >= min && recycledNumbers.get(i) <= max)
				{
					numDistinctNumbers++;
				}
				else if (i == 0 && recycledNumbers.get(i) >= min && recycledNumbers.get(i) <= max)
				{
					numDistinctNumbers++;
				}
			}
			
			return (numDistinctNumbers*(numDistinctNumbers-1)/2);
		}
	}
}