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

	}
}

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.io.IOException;
import java.util.List;

public class WaterSheds extends JamProblem {

    public static void main(String[] args) throws IOException {
        WaterSheds p = new WaterSheds();
        p.go();
    }

    Character[][] res;
    WSCase cs;
    char curr;

    @Override
    String solveCase(JamCase jamCase) {
        cs = (WSCase) jamCase;
        curr = 'a';
        res = new Character[cs.h][cs.w];
        for (int y = 0; y < cs.h; y++) {
            for (int x = 0; x < cs.w; x++) {
                res[y][x] = ' ';
            }
        }
        for (int y = 0; y < cs.h; y++) {
            for (int x = 0; x < cs.w; x++) {
                res[y][x] = update(y, x);
            }
        }
        return "\n" + JamUtil.printTable(res);  //To change body of implemented methods use File | Settings | File Templates.
    }

    private char update(int y, int x) {
        if (res[y][x] != ' ') {
            return res[y][x];
        }
        int min = cs.table[y][x];
        int dir = -1;
        if (y > 0) {
            int curr = cs.table[y - 1][x];
            if (curr < min) {
                min = curr;
                dir = 0;
            }
        }
        if (x > 0) {
            int curr = cs.table[y][x - 1];
            if (curr < min) {
                min = curr;
                dir = 1;
            }
        }
        if (x < cs.w - 1) {
            int curr = cs.table[y][x + 1];
            if (curr < min) {
                min = curr;
                dir = 2;
            }
        }
        if (y < cs.h - 1) {
            int curr = cs.table[y + 1][x];
            if (curr < min) {
                min = curr;
                dir = 3;
            }
        }
        if (dir == -1) {
            return res[y][x] = curr++;
        } else {
            switch (dir) {
                case 0:
                    return res[y][x] = update(y - 1, x);
                case 1:
                    return res[y][x] = update(y, x - 1);
                case 2:
                    return res[y][x] = update(y, x + 1);
                case 3:
                    return res[y][x] = update(y + 1, x);
                default:
                    throw new RuntimeException();
            }
        }
    }

    @Override
    JamCase parseCase(List<String> file, int line) {
        int i = line;
        String firstLine = file.get(i++);
        int[] pair = JamUtil.parseIntList(firstLine, 2);
        WSCase cas = new WSCase();
        cas.h = pair[0];
        cas.w = pair[1];
        cas.lineCount = cas.h + 1;
        cas.table = new int[cas.h][];
        for (int ih = 0; ih < cas.h; ih++) {
            cas.table[ih] = JamUtil.parseIntList(file.get(i++), cas.w);
        }
        return cas;
    }
}

class WSCase extends JamCase {
    int w, h;
    int[][] table;
}
