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.File;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class Recycle {
	
	static Set<Integer> chosen ;
	static int a = 0;
	static int b = 0;
	public static PrintWriter pw;
	
	public static void main(String[] args) throws Exception {
		try{
			File f = new File("\\\\filesrv/stufiles$/matt.tough/out.txt");
			f.createNewFile();
			pw = new PrintWriter(f);
		}catch(Exception e){
			
		}
		Scanner in = new Scanner(new File("\\\\filesrv/stufiles$/matt.tough/C-large.in"));
		//Scanner in = new Scanner(System.in);
		String line = in.nextLine();
		int cases = Integer.parseInt(line);
		for (int t=0; t < cases; t++){
			line = in.nextLine();
			Scanner thisLine = new Scanner (line);
			a = thisLine.nextInt();
			b = thisLine.nextInt();
			
			int minDigits = ((int)Math.floor(Math.log10(a)))+1;
			int maxDigits = ((int)Math.floor(Math.log10(b)))+1;
			
			int count = 0;
			chosen = new HashSet<Integer>();
			
			for (int digits = minDigits; digits <= maxDigits; digits++){
				if (digits < 2){
					count += 0;
				}else{
					int minNum =(int) Math.pow(10, digits-1);
					int maxNum =(int) Math.pow(10, digits);
					for (int i = minNum; i < maxNum; i++){
						
						if(i > b){
							
							break;
						}
						
						
						
						if(i>=a){
						String thisNum = i+"";
						count += countPairsAdded(thisNum);
						}
						
					}
					
				}
			}
			
			pw.printf("Case #%d: %d\n", t+1,count);
			
			
			
			
		}
		pw.flush();
	}
	
	public static int countPairsAdded(String s){
		
		
		int count =0;
		int num = Integer.parseInt(s);
		if (chosen.contains(num))
			return 0;
		
		
		for (int shift = 0; shift < s.length() ; shift++){
			String test = shift(s,shift);
			int i = Integer.parseInt(test);
			if(i <= b && i >=a && i != num &&  !chosen.contains(i)){
				//System.out.println(test);
				count++;
				chosen.add(i);
			}
			
		}
		
		//System.out.println(count);
		
		
		
		count *= count+1;
		count /=2;
		
		return count;
	}
	
	public static String shift(String s, int shift){
		return s.substring(shift) + s.substring(0,shift);
		
	}

}
