package Main;

import com.sun.deploy.util.ArrayUtil;
import org.apache.commons.lang3.ArrayUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: arran
 * Date: 14/04/12
 * Time: 3:12 PM
 * To change this template use File | Settings | File Templates.
 */
public class Round {
    public StringBuilder parse(BufferedReader in) throws IOException {
        StringBuilder out = new StringBuilder();
        String lineCount = in.readLine();

        for (int i = 1; i <= Integer.parseInt(lineCount); i++)
        {
            out.append("Case #"+i+": ");
            System.err.println("Case #"+i+": ");
            String line = in.readLine();
            String[] splits = line.split(" ");
            int p1 = Integer.valueOf(splits[0]);
            int p2 = Integer.valueOf(splits[1]);
            int r = pairRecyclable(p1, p2);
            out.append(r);

//            if (i < Integer.parseInt(lineCount))
                out.append("\n");
        }
        return out;
    }

    public static int pairRecyclable(int i1, int i2) {
        HashSet<String> hash = new HashSet<String>();
        for (int i = i1; i < i2; i++)
        {
            String istr = String.valueOf(i);
            if (istr.length() < 2) continue;
            for (int p = 0; p < istr.length() ; p++)
            {
                String nistr = istr.substring(p,istr.length()).concat(istr.substring(0,p));
                if (Integer.valueOf(nistr) < i1) continue;
                if (Integer.valueOf(nistr) > i2) continue;
                if (nistr.equals(istr)) continue;
                String cnistr = (Integer.valueOf(nistr) > Integer.valueOf(istr)) ?  istr + "," + nistr : nistr + "," + istr;
                hash.add(cnistr);
            }
        }
        return hash.size();
    }

    public static void main(String[] args) {
        InputStreamReader converter = null;
        try {
            int attempt = 0;
            String quest = "C";
//            String size = "small-attempt";
            String size = "large";
            converter = new InputStreamReader(new FileInputStream("src/resource/"+quest+"-"+size+".in"));
            BufferedReader in = new BufferedReader(converter);
            Round r = new Round();
            String str = r.parse(in).toString();
            System.out.print(str);
            FileOutputStream fos = new FileOutputStream(quest+"-"+size+".out");
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(str);
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}



import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class C {
    class Solver implements Runnable {
        int testId;
        boolean failed = false;
        int result = 0;
        
        Solver(int testId) {
            this.testId = testId;
        }

        String in, out = "";
        
        ArrayList<String> text = new ArrayList<String>();
        int a,b, p;

        private void readInput() {
        	a = nextInt();
        	b = nextInt();
        }

        int res;
        
        public void run() {
			//System.out.println("SOLVING  a = " + a + "   b = " + b );        	
        	
        	for (int i=a; i<b; i++){
        		int power = 1;
        		//A and B have the same num of digits, but to be sure...
        		     if (i>999999) power = 1000000; //to save some CPU cycles 
        		else if (i>99999)  power = 100000;
        		else if (i>9999)   power = 10000;
        		else if (i>999)    power = 1000;
        		else if (i>99)     power = 100;
        		else if (i>9)      power = 10;
        		
        		//k = i;
        		
        		int cycled = i;
        		int right = i;
        		
        		while (right>9) { //13205 //51320 //ignore 05132 //20513
        			cycled /= 10; //shift >>
        			cycled += (right % 10)*power; //add rightmost digit in front
        			right /= 10;
        		    
        			if (cycled == i ) right = 0;
        			if ( cycled <= b && cycled > i) {// no leading 0s
        				//System.out.println(" i = " + i + " cycled " + (cycled) );
        				result++;
        			}
        		}        	
        	}
        }


        private void printOutput() {
            if (failed) {
                writer.println("0");
                System.out.println("FAILURE!!!");
            } else {
                writer.println(String.format("Case #%d: %d", testId, result));
            }
        }
    }

	// solve the problem here
	private void solve(){
		int numTests = nextInt();
		for (int testId = 1; testId <= numTests; testId++) {
			Solver solver = new Solver(testId);
			solver.readInput();
			solver.run();
			solver.printOutput();
		}

	}


	BufferedReader reader;
	StringTokenizer tokenizer = null;
	PrintWriter writer;

	String encrypted = "ejp mysljylc kd kxveddknmc re jsicpdrysi"+
	         "rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd"+
	         "de kr kd eoya kw aej tysr re ujdr lkgc jvq";
    String output    = "our language is impossible to understand"+
	         "there are twenty six factorial possibilities"+ 
	         "so it is okay if you want to just give upz";

    char[] code = new char[256];
    	
    private void preparehalfcode() {
    	for (int i = 0; i<encrypted.length(); i++) {
    		code[encrypted.charAt(i)] = output.charAt(i);
    	}    	
    }
    
    private void prepare() {
    	preparehalfcode();
    	encrypted = encrypted.toUpperCase();
    	output = output.toUpperCase();
    	preparehalfcode();
    }
    
    public void run() {
        try {
            long now = System.currentTimeMillis();
            String problemfilename = this.getClass().getSimpleName();
            reader = new BufferedReader(new FileReader(problemfilename + ".in"));
            writer = new PrintWriter(problemfilename + ".out");
            prepare();
            solve();
            reader.close();
            writer.close();
            System.out.println(System.currentTimeMillis() - now + "ms");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

	int nextInt() {
        return Integer.parseInt(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

    long nextLong() {
        return Long.parseLong(nextToken());
    }

    String nextToken() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    /**
	 * @param args
	 */
	public static void main(String[] args) {
		new C().run();
	}

}
