import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main {

//	static int MAX = 10000;
	static int MAX = 2000000;
	static Object[] All = new Object[MAX+1];
	
	static int size( int x ) {
		if(x>999999) return 7;
		if(x>99999)  return 6;
		if(x>9999)   return 5;
		if(x>999)    return 4;
		if(x>99)     return 3;
		if(x>9)      return 2;
		return 1;
	}
	static int[] rotate( int value ) {
		List<Integer> result = new ArrayList<Integer>();
		int[] V = new int[8];
		int s = size(value);
		int x = value;
		
		for(int i=s-1;i>=0;i--) {
			V[i] = x%10;
			x /=10;
		}
		int rot;
		for(int i=1; i<s; i++) {
			if(V[i]!=0){
				rot=0;
				for(int j=0,ii=i;j<s; j++,ii=(ii+1)%s){
					rot=rot*10+V[ii];
				}
				if(rot>value && !result.contains(rot))
					result.add(rot);
			}
		}
		if( result.isEmpty() )
			return null;
		int[] r = new int[result.size()];
		for(int i=0; i<r.length; i++)
			r[i]=result.get(i);
		return r;
	}
	
	static void precalculate() {
		for(int i=1; i<=MAX; i++){
			All[i] = rotate(i);
		}
	}
	static int solve( Scanner in ) {
		int A, B, sol=0;
		
		A = in.nextInt();
		B = in.nextInt();

		for( int i=A; i<=B; i++) {
//			List<Integer> result = rotate(i);
			if( All[i]!=null ) {
				for(int value: (int[])All[i] ) {
					if( value <= B )
						sol++;
				}
			}
		}

		return sol;
	}
	
	public static void main ( String args[] ) {
		int T; 
		Scanner in = new Scanner(System.in);
		T = in.nextInt();
		int cnt=0;
		int sol;
		precalculate();
		for( cnt=1; cnt<=T; cnt++ ) {
			sol = solve( in );
			System.out.printf("Case #%d: %d\n", cnt, sol);
		}

	}
}

package jamo;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RecycledNumbers {

	public static void main(String[] args) {
		List<String> lines = readFromFile("rnumbers.txt");
		int number = Integer.parseInt(lines.get(0));
		int a = 0;
		int b = 0;

		for (int i = 1; i <= number; i++) {
			String[] a_b = lines.get(i).split(" ");
			a = Integer.parseInt(a_b[0]);
			b = Integer.parseInt(a_b[1]);

			System.out.println( "Case #"+i+": "+checkPair(a, b));

		}

	}

	public static int sumOfDigits(Integer number) {
		int sum = 0;
		String n = number.toString();
		char[] digits = n.toCharArray();
		for (int i = 0; i < digits.length; i++) {

			sum += Integer.parseInt(Character.toString(digits[i]));
		}

		return sum;
	}

	public static boolean same(String a, String b) {
		char[] _a = a.toCharArray();
		char[] _b = b.toCharArray();
		if (_a.length != _b.length) {
			return false;

		}
		Arrays.sort(_a);
		Arrays.sort(_b);
		String a_ = new String(_a);
		String b_ = new String(_b);

		return a_.equals(b_);
	}

	public static List<Integer> iterations(Integer a) {
		char[] a_ = a.toString().toCharArray();

		List<Integer> out = new ArrayList<Integer>();
		String start = new String(a_);

		for (int i = 0; i < a_.length; i++) {
			if (out.contains(Integer.parseInt(start))) {

			} else {
				out.add(Integer.parseInt(start));
				start = next(start);
			}

		}

		return out;
	}

	public static String next(String val) {
		char[] a_ = val.toCharArray();
		char[] x = new char[a_.length];
		for (int i = 1; i < a_.length; i++) {
			x[i - 1] = a_[i];
		}
		x[a_.length - 1] = a_[0];
		return new String(x);
	}

	public static int nextPair(Integer a) {
		char[] a_ = a.toString().toCharArray();
		char[] x = new char[a_.length];
		for (int i = 1; i < a_.length; i++) {
			x[i - 1] = a_[i];
		}
		x[a_.length - 1] = a_[0];
		return Integer.parseInt(new String(x));
	}

	public static int checkPair(int a, int b) {
		int pairs = 0;
		int previ = 0;
		int prevnext = 0;
		int next = 0;
		if (b < 10)
			return 0;
		for (int i = a; i <= b; i++) {
			List<Integer> its = iterations(i);
			for (int j = 0; j < its.size(); j++) {
				next = its.get(j);
				if (next <= b && next > i) {
					 
					pairs += 1;
				}
				prevnext = next;

			}
			previ = i;

		}
		return pairs;
	}

	public static List<String> readFromFile(String filename) {
		List<String> lines = new ArrayList<String>();
		try {
			FileInputStream fstream = new FileInputStream(filename);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				lines.add(strLine);
				// System.out.println(strLine);
			}

			in.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}

		return lines;
	}

}
