package jp.funnything.competition.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.commons.io.IOUtils;

public class QuestionReader {
    private final BufferedReader _reader;

    public QuestionReader( final File input ) {
        try {
            _reader = new BufferedReader( new FileReader( input ) );
        } catch ( final FileNotFoundException e ) {
            throw new RuntimeException( e );
        }
    }

    public void close() {
        IOUtils.closeQuietly( _reader );
    }

    public String read() {
        try {
            return _reader.readLine();
        } catch ( final IOException e ) {
            throw new RuntimeException( e );
        }
    }

    public BigDecimal[] readBigDecimals() {
        return readBigDecimals( " " );
    }

    public BigDecimal[] readBigDecimals( final String separator ) {
        final String[] tokens = readTokens( separator );

        final BigDecimal[] values = new BigDecimal[ tokens.length ];
        for ( int index = 0 ; index < tokens.length ; index++ ) {
            values[ index ] = new BigDecimal( tokens[ index ] );
        }

        return values;
    }

    public BigInteger[] readBigInts() {
        return readBigInts( " " );
    }

    public BigInteger[] readBigInts( final String separator ) {
        final String[] tokens = readTokens( separator );

        final BigInteger[] values = new BigInteger[ tokens.length ];
        for ( int index = 0 ; index < tokens.length ; index++ ) {
            values[ index ] = new BigInteger( tokens[ index ] );
        }

        return values;
    }

    public int readInt() {
        final int[] values = readInts();

        if ( values.length != 1 ) {
            throw new IllegalStateException( "Try to read single interger, but the line contains zero or multiple values" );
        }

        return values[ 0 ];
    }

    public int[] readInts() {
        return readInts( " " );
    }

    public int[] readInts( final String separator ) {
        final String[] tokens = readTokens( separator );

        final int[] values = new int[ tokens.length ];
        for ( int index = 0 ; index < tokens.length ; index++ ) {
            values[ index ] = Integer.parseInt( tokens[ index ] );
        }

        return values;
    }

    public long readLong() {
        final long[] values = readLongs();

        if ( values.length != 1 ) {
            throw new IllegalStateException( "Try to read single interger, but the line contains zero or multiple values" );
        }

        return values[ 0 ];
    }

    public long[] readLongs() {
        return readLongs( " " );
    }

    public long[] readLongs( final String separator ) {
        final String[] tokens = readTokens( separator );

        final long[] values = new long[ tokens.length ];
        for ( int index = 0 ; index < tokens.length ; index++ ) {
            values[ index ] = Long.parseLong( tokens[ index ] );
        }

        return values;
    }

    public String[] readTokens() {
        return readTokens( " " );
    }

    public String[] readTokens( final String separator ) {
        return read().split( separator );
    }
}

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

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

/**
 *
 * @author Ari
 */
public class ProblemC {
    static String in = "src/c.in";
    static String out = "src/c.out";
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Scanner scan = new Scanner(new File(in));
        BufferedWriter write = new BufferedWriter(new FileWriter(out));
        int numlines = scan.nextInt();
        String mini;
        String maxi;
        byte[] min = new byte[7];
        byte[] max = new byte[7];
        byte[] iter = new byte[7];
        int numDigits;
        int numPossible;
        int answer;
        int[] rots = new int[7];
        byte currentRots;
        byte compare = 0;
        
        //Loop vars:
        int x;
        int y;
        int d;
        int m;
        int n;
        int rot;
        int count;
        
        for(x = 0; x < numlines; x++) {
            write.write("Case #" + (x+1) + ": ");
            mini = scan.next();
            maxi = scan.next();
            answer = 0;
            
            numPossible = Integer.parseInt(maxi) - Integer.parseInt(mini);
            numDigits = mini.length();
            
            for(y = 0; y < numDigits; y++) {
                min[y] = Byte.parseByte(""+mini.charAt(y));
                iter[y] = min[y];
                max[y] = Byte.parseByte(""+maxi.charAt(y));
            }
            
            for(count = 0; count < numPossible; count++) {
                currentRots = 0;
                for(rot = 1; rot < numDigits; rot++) {
                    compare = 0;
                    for(d = 0; d < numDigits - 1; d++) {
                        compare = iter[(d + rot) % numDigits];
                        if(compare > iter[d]) {
                            compare = -2;
                            break;
                        }
                        else if(compare < iter[d]) {
                            compare = -1;
                            break;
                        }
                    }
                    if(compare > -1) {
                        if(iter[(numDigits + rot - 1) % numDigits] > iter[numDigits - 1]) {
                            compare = -2;
                        }
                    }
                    if(compare == -2) {//rotation is greater
                        for(d = 0; d < numDigits; d++) {
                            compare = iter[(d + rot) % numDigits];
                            if(compare > min[d]) {
                                compare = 1;
                                break;
                            }
                            else if(compare < min[d]) {
                                compare = -1;
                                break;
                            }
                        }
                        if(compare > -1) {//>=min
                            for(d = 0; d < numDigits; d++) {
                                compare = iter[(d + rot) % numDigits];
                                if(compare > max[d]) {
                                    compare = -1;
                                    break;
                                }
                                else if(compare < max[d]) {
                                    compare = 1;
                                    break;
                                }
                            }
                            if(compare > -1) {//<=max
                                rots[currentRots] = rot;
                                currentRots++;
                                answer++;
                            }
                        }
                    }
                }
                
                for(n = 0; n < currentRots-1; n++) {
                    for(m = n+1; m < currentRots; m++) {
                        for(d = 0; d < numDigits; d++) {
                            compare = iter[(d + rots[n]) % numDigits];
                            compare -= iter[(d + rots[m]) % numDigits];
                            if(compare != 0) {
                                compare = 1;
                                break;
                            }
                        }
                        if(compare == 0) {
                            answer--;
                            break;
                        }
                    }
                }
                
                for(d = numDigits-1; d >= 0; d--) {
                    if(iter[d] == 9) {
                        iter[d] = 0;
                    }
                    else {
                        iter[d]++;
                        break;
                    }
                }
            }
            
            write.write("" + answer);
            write.write('\n');
        }
        scan.close();
        write.close();
    }

    private static void print(byte[] iter, int rot) {
        for(int d = 0; d < iter.length; d++) {
            System.out.print(""+iter[(d + rot) % iter.length]);
        }
    }
    
    private static int get(byte[] iter, int rot, int length) {
        String s = "";
        for(int d = 0; d < length; d++) {
            s+=iter[(d + rot) % length];
        }
        return Integer.parseInt(s);
    }
}
