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();
    }
}

import java.awt.Container;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JTextArea;


public class Recycling extends JFrame
{
	private final int WIDTH = 800;
	private final int HEIGHT = 400;
	private Container pane; 
	
	private int[] A;
	private int[] B;
	private int[] numberOfSolutions;
	
	private String[][] permutations;
	
	private int numberOfLines;
	private int lastI = 0;
	private int lastWorkingOnIt = 0;
	
	
	private JTextArea input, output;
	
	
	private Scanner scanner;
	private PrintWriter out, outDebug;
	
	
	public Recycling() throws IOException
	{
		pane = new Container();
		pane = getContentPane();
		
		scanner = new Scanner(new File("c:/users/Christopher/Desktop/input.in"));
		out = new PrintWriter(new FileWriter("c:/users/Christopher/Desktop/output.out")); 
		outDebug = new PrintWriter(new FileWriter("c:/users/Christopher/Desktop/debug.out")); 
		
		numberOfLines = Integer.parseInt(scanner.nextLine());
		
		A = new int[numberOfLines];
		B = new int[numberOfLines];
		numberOfSolutions = new int[numberOfLines];
		
		
		
		input = new JTextArea(100, numberOfLines);
		input.setSize(300,200);
		input.setLocation (10, 10);
		
		output = new JTextArea(120, numberOfLines);
		output.setSize(300,200);
		output.setLocation(410, 10);
		
		pane.setLayout(null);
		pane.add(input);
		pane.add(output);
		
		
		
		for (int i = 0; i < numberOfLines; i++)
		{
			A[i] = scanner.nextInt();
			B[i] = scanner.nextInt();
			
			numberOfSolutions[i] = findSolutions(i);
			
			//input.append(A[i] + " " + B[i] + "\n");
			//output.append("Case #" + (i + 1) + ": " + numberOfSolutions[i] + "\n");
			out.println("Case #" + (i + 1) + ": " + numberOfSolutions[i]);
		}
		
		
		
		System.out.println("I made it!");
		
		scanner.close();
		out.close();
		outDebug.close();
		
		setVisible(true);
		setSize(WIDTH, HEIGHT);
		setTitle("Recycling");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		
	}
	
	public static void main(String args[]) throws IOException
	{
		Recycling awesome = new Recycling();
	}
	
	
	
	int findSolutions(int whichLine)
	{
		int difference = B[whichLine] - A[whichLine] + 1;
		permutations = new String[difference][];
		int workingOnIt;
		
		int numberOfDigits;
		int whichEntry;
		
		for (int i = A[whichLine]; i < B[whichLine] + 1; i++)
		{
			whichEntry = i - A[whichLine];
			numberOfDigits = (int)Math.floor(Math.log10((double)i)) + 1;
			permutations[i - (A[whichLine])] = new String[numberOfDigits];
			
			for (int j = 0; j < numberOfDigits; j++)
			{
				
				permutations[whichEntry][j] = rotator(Integer.toString(i), j);
				
				workingOnIt = Integer.parseInt(permutations[whichEntry][j]);
				
				if (workingOnIt > i && workingOnIt <= B[whichLine] && permutations[whichEntry][j].charAt(0) != '0' && (i != lastI || workingOnIt != lastWorkingOnIt))
				{
					numberOfSolutions[whichLine]++;
					lastI = i;
					lastWorkingOnIt = workingOnIt;
				}
			}
		}
		
		return numberOfSolutions[whichLine];
	}
	
	
	
	
	String rotator(String original, int numberOfTimes)
	{
		char[] originalChar = original.toCharArray();
		
		char temp; 
		
		for (int i = 0; i < numberOfTimes; i++)
		{
			temp = originalChar[0];
			
			for (int j = 1; j < originalChar.length; j++)
			{
				originalChar[j - 1] = originalChar[j];
			}
			
			originalChar[originalChar.length - 1] = temp;
		}
		
		String rotatedString = new String(originalChar);
		
		return rotatedString;
	}
}