/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codejam;

/**
 *
 * @author eblanco
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.JFileChooser;

public class RecycledNumbers {

    public static String DatosArchivo[] = new String[10000];

    public static int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
        if (sarray != null) {
            int intarray[] = new int[sarray.length];
            for (int i = 0; i < sarray.length; i++) {
                intarray[i] = Integer.parseInt(sarray[i]);
            }
            return intarray;
        }
        return null;
    }

    public static boolean CargarArchivo(String arch) throws FileNotFoundException, IOException {
        FileInputStream arch1;
        DataInputStream arch2;
        String linea;
        int i = 0;

        if (arch == null) {
            //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
            int rc = fc.showDialog(null, "Select a File");
            if (rc == JFileChooser.APPROVE_OPTION) {
                arch = fc.getSelectedFile().getAbsolutePath();
            } else {
                System.out.println("No hay nombre de archivo..Yo!");
                return false;
            }
        }
        arch1 = new FileInputStream(arch);
        arch2 = new DataInputStream(arch1);
        do {
            linea = arch2.readLine();
            DatosArchivo[i++] = linea;
            /* if (linea != null) {
            System.out.println(linea);
            }*/
        } while (linea != null);
        arch1.close();
        return true;
    }

    public static boolean GuardaArchivo(String arch, String[] Datos) throws FileNotFoundException, IOException {
        FileOutputStream out1;
        DataOutputStream out2;
        int i;
        out1 = new FileOutputStream(arch);
        out2 = new DataOutputStream(out1);

        for (i = 0; i < Datos.length; i++) {
            if (Datos[i] != null) {
                out2.writeBytes(Datos[i] + "\n");
            }
        }
        out2.close();
        return true;
    }

    public static void echo(Object msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) throws IOException, Exception {
        String[] res = new String[10000], num = new String[2];
        int i, j, k, ilong;
        int ele = 1;
       ArrayList al = new ArrayList();
        String FName = "C-small-attempt0",  istr, irev, linea;
        if (CargarArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".in")) {
            for (j = 1; j <= Integer.parseInt(DatosArchivo[0]); j++) {
                linea = DatosArchivo[ele++];
                num = linea.split(" ");
                   al.clear();
                // echo("A: "+num[0]+" B: "+num[1]);
                for (i = Integer.parseInt(num[0]); i <= Integer.parseInt(num[1]); i++) {
                    istr = Integer.toString(i);
                    ilong = istr.length();
                 
                    for (k = 1; k < ilong; k++) {
                        if (ilong > k) {
                            irev = istr.substring(istr.length() - k, istr.length()) + istr.substring(0, istr.length() - k);
                            //echo("caso: " + j + ": isrt: " + istr + " irev: " + irev);
                            if ((Integer.parseInt(irev) > Integer.parseInt(num[0])) && (Integer.parseInt(irev) > Integer.parseInt(istr)) && (Integer.parseInt(irev) <= Integer.parseInt(num[1]))) {
                                al.add("(" + istr + "," + irev + ")");
                            }
                        }
                    }
                }
                HashSet hs = new HashSet();
                hs.addAll(al);
                al.clear();
                al.addAll(hs);
                res[j] = "Case #" + j + ": " + al.size();
                echo(res[j]);
                LeerArchivo.GuardaArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".out", res);
            }
        }
    }
}

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

public class Qual2012 {

	public static void lame() {
		try {
			FileReader fr = new FileReader("input.txt");
			BufferedReader reader = new BufferedReader(fr);
			PrintWriter writer = new PrintWriter("output.txt");

			HashMap<String, String> dictionary = new HashMap<String, String>();
			dictionary.put("a", "y");
			dictionary.put("b", "h");
			dictionary.put("c", "e");
			dictionary.put("d", "s");
			dictionary.put("e", "o");
			dictionary.put("f", "c");
			dictionary.put("g", "v");
			dictionary.put("h", "x");
			dictionary.put("i", "d");
			dictionary.put("j", "u");
			dictionary.put("k", "i");
			dictionary.put("l", "g");
			dictionary.put("m", "l");
			dictionary.put("n", "b");
			dictionary.put("o", "k");
			dictionary.put("p", "r");
			dictionary.put("q", "z");
			dictionary.put("r", "t");
			dictionary.put("s", "n");
			dictionary.put("t", "w");
			dictionary.put("u", "j");
			dictionary.put("v", "p");
			dictionary.put("w", "f");
			dictionary.put("x", "m");
			dictionary.put("y", "a");
			dictionary.put("z", "q");
			dictionary.put(" ", " ");
			int numCases = Integer.parseInt(reader.readLine());

			for (int i = 1; i <= numCases; i++) {
				String sentence = reader.readLine();
				String newSentence = "";
				for (char c : sentence.toCharArray()) {
					char[] temp = new char[1];
					temp[0] = c;
					newSentence += dictionary.get(new String(temp));
				}
				writer.println("Case #" + i + ": " + newSentence);
			}

			writer.close();
			fr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static boolean canMatch(int score, int target) {
		if (score == 0)
			return target <= 0;
		if (score == 1 || score == 2)
			return target <= 1;
		if (score % 3 == 0) {
			return score / 3 >= target;
		}
		return ((score / 3) + 1 >= target);
	}

	public static boolean canMatchSpecial(int score, int target) {
		if (score == 0)
			return target <= 0;
		if (score == 1)
			return target <= 1;
		if (score == 2)
			return target <= 2;
		if (score % 3 == 1)
			return (score / 3 + 1) >= target;
		return (score / 3 + 2) >= target;
	}

	public static void dance() {
		try {
			FileReader fr = new FileReader("input.txt");
			BufferedReader reader = new BufferedReader(fr);
			PrintWriter writer = new PrintWriter("output.txt");

			int numCases = Integer.parseInt(reader.readLine());
			for (int i = 1; i <= numCases; i++) {
				StringTokenizer line = new StringTokenizer(reader.readLine(),
						" ");
				int numDancers = Integer.parseInt(line.nextToken());
				int numSurprising = Integer.parseInt(line.nextToken());
				int bestScore = Integer.parseInt(line.nextToken());
				List<Integer> scores = new LinkedList<Integer>();
				for (int j = 0; j < numDancers; j++)
					scores.add(Integer.parseInt(line.nextToken()));
				int count = 0;
				for (Integer score : scores) {
					if (canMatch(score, bestScore))
						count++;
					else if (canMatchSpecial(score, bestScore)
							&& numSurprising > 0) {
						numSurprising--;
						count++;
					}
				}
				writer.println("Case #" + i + ": " + count);
			}

			writer.close();
			fr.close();

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

	public static void recycle() {
		try {
			FileReader fr = new FileReader("input.txt");
			BufferedReader reader = new BufferedReader(fr);
			PrintWriter writer = new PrintWriter("output.txt");
			
			int numCases = Integer.parseInt(reader.readLine());
			for (int i = 1; i <= numCases; i++) {
				StringTokenizer line = new StringTokenizer (reader.readLine(), " ");
				int A = Integer.parseInt(line.nextToken());
				int B = Integer.parseInt(line.nextToken());
				int count = 0;
				for (int k = A; k <= B; k++) {
					if (k < 10) {
						continue;
					}
					String temp = "" + k;
					String cmp = "" + k;
					int length = temp.length();
					temp = temp.substring(length-1) + temp.substring(0, length - 1);
					while (!temp.equals(cmp)) {
						int cycle = Integer.parseInt(temp);
						if (A <= cycle && cycle <= B)
							count++;
						temp = temp.substring(length-1) + temp.substring(0, length - 1);
					}
				}
				writer.println ("Case #" + i + ": " + count/2);
			
				
			}
			
			writer.close();
			fr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// lame();
		// dance();
		recycle();
		
	}
}