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

public class recycled_numbers {
	public static void main(String[]a) throws Exception{
		
		Scanner f = new Scanner(new File(a[0]));
		int result=0;
		int A,B;
		String n,m;
		HashSet<String> s=new HashSet<String>();
		
		int T=f.nextInt();	//T total case count
		for (int t=1; t<=T;t++){//for each case t
			
			s.clear();
			result=0;				//reset
			A=f.nextInt();B=f.nextInt();	//get values for next case
			
			if (A==B)result=0;//remove case not meeting problem limits 
			else{
				for(int i=A;i<=B;i++){//for each int in range
					n=Integer.toString(i);
					m=new String(n);
					//System.out.println(n);
					for (int j=1;j<n.length();j++){//move each digit to the front & test
						m = m.substring(m.length()-1)+m.substring(0,m.length()-1);
						if(m.matches("^0+[\\d]+")!=true &&
								Integer.parseInt(m)<=B &&
								Integer.parseInt(n)<Integer.parseInt(m)){
							s.add(new String(n+","+m));
							//result++;
							//System.out.println(" matched: "+m);
						}
						
					}
				}
				result = s.size();
			}

			//display output
			System.out.println("Case #" + t + ": " + result);
		}
	}
}
package com.sam.googlecodejam.recyclenumbers;

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

import com.sam.googlecodejam.helper.InputReader;

public class RecycleNumbers {
	long pairCount;

	public void getPairCount(long a, long b) {
		int length = Long.toString(a).length();

		long power = 1;
		for (int i = 1; i < length; i++) {
			power = power * 10;
		}

		for (long m = a; m < b; m++) {
			long value = m;
			Set<Long> pairCountSet = new HashSet<>();
			for (int i = 0; i < length; i++) {
				value = value / 10 + (value % 10) * power;

				if (value > m && value <= b) {

					pairCountSet.add(value);
				}
			}
			pairCount = pairCount + pairCountSet.size();
		}
	}

	public static void main(String[] args) {

		InputReader reader = new InputReader("c://input.in");
		reader.readNextLine(); // read the line number off - we don't need it

		String lineRead = null;
		int i = 1;
		while ((lineRead = reader.readNextLine()) != null) {
			RecycleNumbers recycleNumbers = new RecycleNumbers();

			String values[] = lineRead.split(" ");
			recycleNumbers.getPairCount(Long.parseLong(values[0]),
					Long.parseLong(values[1]));
			System.out.println("Case #" + i + ": " + recycleNumbers.pairCount);
			i++;
		}
	}
}
