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 net.anzix.learn.codejam.qr12;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class RecycledNumbers {

	private InputStream input;
	private PrintStream output;


	public RecycledNumbers() {

	}

	public RecycledNumbers(InputStream input, PrintStream output) {
		super();
		this.input = input;
		this.output = output;
	}

	public RecycledNumbers(File inputFile, File outputFile) throws Exception {
		this.input = new FileInputStream(inputFile);
		output = new PrintStream(outputFile);

	}

	public void run() throws Exception {
		Scanner scanner = new Scanner(input);
		int lines = scanner.nextInt();
		for (int i = 0; i < lines; i++) {
			int from = scanner.nextInt();
			int to = scanner.nextInt();
			output.println("Case #" + (i + 1) + ": " + calcNo(from, to));
		}
		output.close();
	}

	public static void main(String[] args) {
		try {
			new RecycledNumbers(new File(args[0]), new File(args[1])).run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int calcNo(int from, int to) {
		int digits = digit(from);
		if (digits < 1) {
			return 0;
		}
		int pow = (int) Math.pow(10, digits - 1);
		
		int res = 0;
		for (int m = from; m <= to; m++) {
			for (int p : convertedNumbers(m,digits,pow)) {
				if (p > m && p <= to) {
					res++;
				}
			}
		}
		return res;
	}

	public Set<Integer> convertedNumbers(int original,int digits, int pow) {		
		int l = pow;
		int s = 10;
		Set<Integer> res = new HashSet<Integer>();
		for (int i = 0; i < digits - 1; i++) {
			if (original % s != 0) {
				int x = (original % s) * pow + original / s;
				if (x != original && x > l) {
					res.add(x);
				}
			}
			s *= 10;
			pow /= 10;
		}
		return res;
	}

	public int digit(int i) {
		return (int) Math.ceil(Math.log10(i+0.5));
	}
}
