package qual;

import java.util.Scanner;

public class RecycledNumbers {

	public static void main(String[] args) {
		new RecycledNumbers().run();

	}

	private void run() {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int t = 0; t < T; t++) {
			int A = sc.nextInt();
			int B = sc.nextInt();
			int result = solve(A, B);
			System.out.printf("Case #%d: %d\n", t + 1, result);
		}
	}

	public int solve(int A, int B) {
		int result = 0;
		for (int i = A; i <= B; i++) {
			int dig = (int) Math.log10(A/*same as B*/) + 1;
			int aa = i;
			for (int d = 0; d < dig - 1; d++) {
				aa = (aa % 10) * (int)Math.pow(10, dig - 1) + (aa / 10);
				if (i == aa) {
					break;
				}
				if (i < aa && aa <= B) {
//					System.out.printf("(%d, %d) ", i, aa);
					result++;
				}
			}
		}
		return result;
	}

}

package hk.polyu.cslhu.codejam.solution.impl.qualificationround;

import java.util.List;

import hk.polyu.cslhu.codejam.solution.Solution;

public class RecycledNumbers extends Solution {
	private int min, max;
	
	@Override
	public void setProblem(List<String> problemDesc) {
		// TODO Auto-generated method stub
		String[] splitArray = problemDesc.get(0).split(" ");
		this.min = Integer.valueOf(splitArray[0]);
		this.max = Integer.valueOf(splitArray[1]);
	}

	@Override
	public void solve() {
		// TODO Auto-generated method stub
		int numOfPairs = 0;
		
		for (int i = this.min; i < this.max; i++) {
			for (int j = i + 1; j <= this.max; j++) {
				if (this.isRecycledPair(i, j))
					numOfPairs++;
			}
		}
		
		this.result = String.valueOf(numOfPairs);
	}

	private boolean isRecycledPair(int i, int j) {
		// TODO Auto-generated method stub
		
		if (i >= j)
			return false;
		
		String m = String.valueOf(j);
		String n = String.valueOf(i);
		
		for (int startIndex = 1; startIndex < n.length(); startIndex++) {
			String partToMove = n.substring(startIndex, n.length());
			String newString = partToMove + n.substring(0, startIndex);
			
			if (newString.equals(m))
				return true;
		}
		
		return false;
	}

}
