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

public class C {

    void solve() throws IOException {
        in("C-large.in"); out("C-large.out");

        long tm = System.currentTimeMillis();
        boolean[] mask = new boolean[2000000];
        int[] r = new int[10];
        int t = readInt();
        for (int cs = 1; cs <= t; ++cs) {
            int a = readInt();
            int b = readInt();
            int ans = 0;
            for (int m = a + 1; m <= b; ++m) {
                char[] d = String.valueOf(m).toCharArray();
                int c = 0;
                for (int i = 1; i < d.length; ++i) {
                    if (d[i] != '0' && d[i] <= d[0]) {
                        int n = 0;
                        for (int j = 0; j < d.length; ++j) {
                            int jj = i + j;
                            if (jj >= d.length) jj -= d.length;
                            n = n * 10 + (d[jj] - '0');
                        }
                        if (n >= a && n < m && !mask[n]) {
                            ++ans;
                            mask[n] = true;
                            r[c++] = n;
                        }
                    }
                }
                for (int i = 0; i < c; ++i) {
                    mask[r[i]] = false;
                }
            }
            println("Case #" + cs + ": " + ans);
            //System.out.println(cs);
        }
        System.out.println("time: " + (System.currentTimeMillis() - tm));
        exit();
    }

    void in(String name) throws IOException {
        if (name.equals("__std")) {
            in = new BufferedReader(new InputStreamReader(System.in));
        } else {
            in = new BufferedReader(new FileReader(name));
        }
    }

    void out(String name) throws IOException {
        if (name.equals("__std")) {
            out = new PrintWriter(System.out);
        } else {
            out = new PrintWriter(name);
        }
    }

    void exit() {
        out.close();
        System.exit(0);
    }

    int readInt() throws IOException {
        return Integer.parseInt(readToken());
    }

    long readLong() throws IOException {
        return Long.parseLong(readToken());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readToken());
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

    String readToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    boolean eof() throws IOException {
        return !in.ready();
    }

    void print(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void println(String format, Object ... args) {
        out.println(new Formatter(Locale.US).format(format, args));
    }

    void print(Object value) {
        out.print(value);
    }

    void println(Object value) {
        out.println(value);
    }

    void println() {
        out.println();
    }

    StringTokenizer st;

    BufferedReader in;
    PrintWriter out;

    public static void main(String[] args) throws IOException {
        new C().solve();
    }
}

package com.amyth.gc2012;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ProblemC {

	/**
	 * @param args
	 */
	private static HashMap<Integer,Integer> factorial = new HashMap<Integer, Integer>();
	private final static String INPUT_DIR_NM = "./Input/2012/";
	private final static String OUTPUT_DIR_NM = "./Output/2012/";
	private static Integer totalTestCase;
	private static ArrayList<ArrayList<Long>> dataList;
	public static void main(String[] args) {
		String fileName = args[0];
		//Read and Create necessary data structures. 
		try {
			readAndBuild(INPUT_DIR_NM + fileName);
		}
		catch (IOException e) {
			e.printStackTrace();

		}
		//TODO Implement program logic
		
		List<Integer> outputList = play();
		
		
		//write in out file
		try{
			// Create file 
			FileWriter fstream = new FileWriter(OUTPUT_DIR_NM+args[0]+".out");
			BufferedWriter out = new BufferedWriter(fstream);
			for (int caseId = 0; caseId < totalTestCase; caseId++) {
				System.out.print("Case #" + (caseId+1) + ": ");
				System.out.println(outputList.get(caseId));
				out.write("Case #" + (caseId+1) + ": ");
				out.write(outputList.get(caseId)+""); 
				if(caseId != totalTestCase)
					out.newLine();
			}
//			Close the output stream
			out.close();
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
	
	
	
	private static List<Integer> play() {
		List<Integer> outList = new ArrayList<Integer>(totalTestCase);
		for (int caseId = 0; caseId < totalTestCase; caseId++) {
			List<Long> testList = dataList.get(caseId);
			Long lRanger = testList.get(0);
			Long hRange = testList.get(1);
			int count = 0;
			List<Long> backupList = new ArrayList<Long>();
			for(long lRange = lRanger;lRange<=hRange;lRange++){
				String str = String.valueOf(lRange);
				int length = str.length();
				backupList=new ArrayList<Long>();
				for(int i=0;i<(length-1);i++){
					Long valCounter = (long) (Math.pow(10,(i+1)));
					int lastValue = (int) (lRange%valCounter);
//					System.out.println("lRange: "+lRange);
					Long lcounter = (long) Math.pow(10, (length-(i+1)));
					int hFirstValue = (int)  (hRange/valCounter);
					if(((lastValue*lcounter) > hRange) || 
							(lRanger > (lastValue*lcounter))
							){
						continue;
					}else{
						Long number = (long) (lastValue*(lcounter));
						number += (lRange/valCounter);
						if((number >= lRanger)&&(number > lRange) && (number <=hRange)){
							if(backupList != null && !backupList.isEmpty()){
								if(!backupList.contains(number)){
//									System.out.println("oldNumber :"+lRange+" newNumber: "+number);
									count++;
								}
							}else{
								backupList.add(number);
//								System.out.println("oldNumber :"+lRange+" newNumber: "+number);
								count++;
							}
							
						}
							
					}
				}
				
				/*Long lcounter = (long) Math.pow(10, length);
				Long rcounter = (long) 10;*/
			}
			outList.add(caseId,count);
		}
		return outList;
	}



		//Build input data structure
		private static void readAndBuild(String dataPath) throws IOException {
			File f = new File(dataPath);
			BufferedReader bf = new BufferedReader(new FileReader(f));
			String text = bf.readLine();

			if (text != null) {
//				first text will be number of test cases
				totalTestCase = Integer.valueOf(text);
				dataList = new ArrayList<ArrayList<Long>>(totalTestCase);
				ArrayList<Long> testObj = null;

				do {
					text = bf.readLine();
					if (text != null) {
						BreakIterator bi = BreakIterator.getWordInstance();
						bi.setText(text);
						testObj = new ArrayList<Long>(2);
						dataList.add(testObj);
						Long N = Long.valueOf(text.substring(0, bi.next()));
						testObj.add(N);
						Long Pd = Long.valueOf(text.substring(bi.next(), bi.next()));
						testObj.add(Pd);
					}
				}while (text != null && !text.equals(""));
			}
		}
		
		
		private static int getFactorial(int length) {
			if(factorial.containsKey(length))
				return factorial.get(length);
			return length*getFactorial(length-1);
		}

}
