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

public class recycled_numbers {
	public static void main(String[]a) throws Exception{
		
		Scanner f = new Scanner(new File(a[0]));
		int result=0;
		int A,B;
		String n,m;
		HashSet<String> s=new HashSet<String>();
		
		int T=f.nextInt();	//T total case count
		for (int t=1; t<=T;t++){//for each case t
			
			s.clear();
			result=0;				//reset
			A=f.nextInt();B=f.nextInt();	//get values for next case
			
			if (A==B)result=0;//remove case not meeting problem limits 
			else{
				for(int i=A;i<=B;i++){//for each int in range
					n=Integer.toString(i);
					m=new String(n);
					//System.out.println(n);
					for (int j=1;j<n.length();j++){//move each digit to the front & test
						m = m.substring(m.length()-1)+m.substring(0,m.length()-1);
						if(m.matches("^0+[\\d]+")!=true &&
								Integer.parseInt(m)<=B &&
								Integer.parseInt(n)<Integer.parseInt(m)){
							s.add(new String(n+","+m));
							//result++;
							//System.out.println(" matched: "+m);
						}
						
					}
				}
				result = s.size();
			}

			//display output
			System.out.println("Case #" + t + ": " + result);
		}
	}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * template class for code jam Contest
 * 
 * @author Tudor
 * 
 */

public class Main
{
    public static final String INPUT_FILE = "src/in.txt";
    public static final String OUTPUT_FILE = "out.txt";
    java.io.BufferedWriter br;

    /*
     * the number of tests
     */
    int tests;
    /*
     * the size of the test
     */
    int a, b;
    /*
     * the array
     */
    int[] tree;

    public static void main(String args[])
    {
	Main classMain = new Main();
	try
	{
	    Scanner scanner = new Scanner(new File(Main.INPUT_FILE));
	    classMain.br = new java.io.BufferedWriter(new FileWriter(Main.OUTPUT_FILE));
	    classMain.setData(scanner);

	} catch (FileNotFoundException e)
	{
	    e.printStackTrace();
	} catch (IOException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    /**
     * method to set the initial data and run the tests
     * 
     * @param br
     *            the reader obtained from file
     */

    public void setData(Scanner scanner)
    {
	try
	{
	    tests = scanner.nextInt();
	    // System.out.println("Tests " + tests);
	    for (int i = 0; i < tests; i++)
	    {
		a = scanner.nextInt();
		b = scanner.nextInt();
		solve(i + 1);
	    }
	    br.close();

	} catch (Exception e)
	{
	    e.printStackTrace();
	}
    }

    /**
     * method to solve the problem
     * 
     * @param testNr
     *            the index of the test
     */

    public void solve(int testNr)
    {
	int result = 0;
	int digits = getDigits(a);

	MyDictionary recycledNumbers = new MyDictionary();

	for (int i = a; i <= b; i++)
	{
	    int number = i;
	    for (int d = 0; d < digits; d++)
	    {
		number = number / 10 + (number % 10) * ((int) Math.pow(10, digits - 1));
		if ((number != i) && (a <= number && number <= b))
		{
		    recycledNumbers.put(Math.min(i, number), Math.max(i, number));
		    // System.out.println(i + "    " + number);
		}
	    }
	}
	System.out.println(recycledNumbers);

	result = recycledNumbers.size();

	try
	{

	    br.write("Case #" + testNr + ": " + result + "\n");
	} catch (IOException e)
	{
	    e.printStackTrace();
	}

    }

    int getDigits(int x)
    {
	int digits = 0;
	while (x != 0)
	{
	    x = x / 10;
	    digits++;
	}

	return digits;
    }
}

class MyDictionary
{
    private Map map;

    MyDictionary()
    {
	this.map = new HashMap();
    }

    public List get(int key)
    {
	return (List) this.map.get(key);
    }

    public void put(int key, int value)
    {
	List list = get(key);
	if (list == null)
	{
	    list = new ArrayList();
	    this.map.put(key, list);
	}
	if (!list.contains(value))
	    list.add(value);
    }

    public int size()
    {
	int totalSize = 0;
	for (Object valueList : map.values())
	{
	    totalSize += ((List) valueList).size();
	}

	return totalSize;
    }
}
