import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Recycle {

	private static HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer, HashSet<Integer>>();
	private static HashSet<Integer> toSkip = new HashSet<Integer>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int T = reader.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int a = reader.nextInt();
			int b = reader.nextInt();
			long before = System.currentTimeMillis();
			int res = doit(a, b);
			System.out.printf("Case #%d: %d\n", tc, res);
			long after = System.currentTimeMillis();
//			System.out.println("time taken: " + (after - before));
		}
	}

	private static int doit(int a, int b) {
		int count = 0;
		HashSet<Integer> skip = new HashSet<Integer>(toSkip);
		for (int i = a; i <= b; i++) {
			if (skip.contains(i))
				continue;
			HashSet<Integer> arr = generate(i);
//			if(arr.size() > 1) {
//				map.put(i, arr);
//			}
			// System.out.println(i + " --> " +
			// Arrays.deepToString(arr.toArray()));
			int temp = 0;
			if (arr.size() > 1) {
				for (int x : arr) {
					if (x >= a && x <= b) {
						temp++;
						skip.add(x);
					}
				}
				count += temp * (temp - 1) / 2;
			}
//			System.out.println("not skipping " + i + " gen: " + arr.size()+ " " + (temp * (temp - 1) / 2));
		}
		return count;
	}

	private static HashSet<Integer> generate(int num) {
		if(map.containsKey(num)) {
			HashSet<Integer> list = map.get(num);
			return list;
		}
		HashSet<Integer> list = new HashSet<Integer>();
		String s = "" + num;
		list.add(num);
		int len = s.length();
		for (int i = 1; i < len; i++) {
			String temp = s.substring(i) + s.substring(0, i);
			// System.out.println("temp: " + temp);
			if (temp.charAt(0) == '0')
				continue;
			int x = Integer.parseInt(temp);
			if( x <= 2000000) {
				list.add(x);
			}
		}
		if(list.size() > 1) {
			map.put(num, list);
		} else {
			toSkip.add(num);
		}
		return list;
	}

}

package com.edwarddrapkin;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Numbers {
	
	private boolean isRecylced(int m, int n) {
		String sm = m + "";
		String sn = n + "";
		
		//eliminate some common cases
		
		// same number apparently doesn't count...
		
		if(sm.equals(sn)) {
			return false;
		}
		
		//not the same length
		while(sm.length() < sn.length()) {
			sm = "0" + sm;
		}
		
		while(sn.length() < sm.length()) {
			sn = "0" + sn;
		}
		
		//chars in m not in n
		for(char c : sm.toCharArray()) {
			if(!sn.contains("" + c)) {
				return false;
			}
		}
		
		//vice versa
		for(char c : sn.toCharArray()) {
			if(!sm.contains("" + c)) {
				return false;
			}
		}
		
		for(int i = 1; i < sm.length(); i++) {
			String slice = sm.substring(i) + sm.substring(0, i);
			if(slice.equals(sn)) {
				return true;
			}
		}
		
		return false;
	}
	
	private static String[] readFile(String file) throws IOException {
		Scanner scanner = new Scanner(new File(file));
		
		String numLines = scanner.nextLine();
		
		int nl = Integer.parseInt(numLines);
		
		String[] lines = new String[nl];
		int i = 0;
		
		while(scanner.hasNextLine() && i < nl) {
			lines[i] = scanner.nextLine();
			++i;
		}
		
		return lines;
	}
	
	public static void main(String[] args) throws Exception {
		Numbers num = new Numbers();

		String[] lines = readFile("C:/Users/Eddie/Desktop/google_input.txt");
		
		int i = 1;
		for(String line : lines) {
			Scanner lScanner = new Scanner(line);
			int low = lScanner.nextInt();
			int high = lScanner.nextInt();
			
			int numPossible = 0;
			for(int idx = low; idx <= high; idx++) {
				for(int inner = idx; inner <= high; inner++) {
					if(num.isRecylced(idx, inner)) {
						//System.out.println(high + " " + low + " " + idx + " " + inner);
						numPossible++;
					}
				}
				
			}
			
			System.out.println("Case #" + i + ": " + numPossible);
			i++;
		}
	}
}













