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 boy0;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CodeJam_C_Small {
	public static void main(String[] args) throws IOException {
		String strLine;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		CodeJam_C_Small me = new CodeJam_C_Small();

		// me.init();

		strLine = br.readLine();
		int T = Integer.parseInt(strLine);
		String[] as;
		char[] a, b;
		int y;
		for (int x = 1; x <= T; x++) {
			strLine = br.readLine();
			as = strLine.split(" ");
			a = as[0].toCharArray();
			b = as[1].toCharArray();
			y = me.get_num_recycled_pairs(a, b);
			System.out.println("Case #" + x + ": " + y);
		}
	}

	// 1 ¡Â A ¡Â B ¡Â 2000000. max length = 7
	private char[] recycled = new char[16]; // (max+1)*2 = 8*2 = 16
	private int r_length = 0;
	private char[] b;
	private int b_length = 0;

	// A ¡Â n < m ¡Â B
	public int get_num_recycled_pairs(char[] a, char[] b_) {
		int y = 0;
		int a_length = a.length;
		b = b_;
		b_length = b.length;

		// r = a (&& a);
		r_length = a_length;
		for (int i = 0; i < a_length; i++) {
			recycled[i] = a[i];
			recycled[i + r_length] = a[i];
		}

		// while r <= b
		while (comp(recycled, 0, r_length, b, 0, b_length) <= 0) {
			// check each r-pair
			for (int i = 1; i < r_length; i++) {
				if (is_recycled_pair(i)) {
					y++;
				}
			}
			// r++;
			add_1_r();
		}

		return y;
	}

	// < : -x , == : 0, > : +x
	private int comp(char[] c1, int c1_offset, int c1_length, char[] c2,
			int c2_offset, int c2_length) {
		int x = c1_length - c2_length;
		if (x == 0) {
			for (int i = 0; i < c2_length; i++) {
				x = c1[c1_offset + i] - c2[c2_offset + i];
				if (x != 0) {
					break;
				}
			}
		}
		return x;
	}

	private boolean is_recycled_pair(int offset) {
		// n = r[0] , m = r[offset]
		// n >= m ? false
		if (comp(recycled, 0, r_length, recycled, offset, r_length) >= 0) {
			return false;
		}
		// m > b ? false
		if (comp(recycled, offset, r_length, b, 0, b_length) > 0) {
			return false;
		}
		// r[offset] == r[x] ? false (0 < x < offset)
		for (int x = 1; x < offset; x++) {
			if (comp(recycled, offset, r_length, recycled, x, r_length) == 0) {
				return false;
			}
		}
//		System.out.println("r:" + new String(recycled) + "\t offset:" + offset);
		return true;
	}

	// r++
	private void add_1_r() {
		for (int i = r_length - 1; i >= 0; i--) {
			if (recycled[i] == '9') {
				recycled[i] = '0';
				recycled[i + r_length] = recycled[i];
			} else {
				recycled[i] = (char) (recycled[i] + 1);
				recycled[i + r_length] = recycled[i];
				return;
			}
		}
		// here, length overflow
		r_length++;
		for (int i = r_length - 1; i >= 1; i--) {
			recycled[i] = recycled[i - 1];
			recycled[i + r_length] = recycled[i];
		}
		recycled[r_length] = recycled[0] = '1';

	}

}
