import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Recycled {
	public static void main(String[] args) throws Exception{
		String inputFile = "C-small-attempt0.in";
		
		BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output")));
		long num_cases = Long.parseLong(input.readLine());
		int current_case = 0;
		
		while (current_case < num_cases){
			current_case++;
			
			String[] fields = input.readLine().split(" ");
			int A = Integer.parseInt(fields[0]);
			int B = Integer.parseInt(fields[1]);
			int total = 0;
			
			for (int n = A; n < B; n++){
				for (int m = n+1; m <= B; m++){
					if (isRecycled(n,m)) total++;
				}
			}
			
			System.out.println("Case #" + current_case + ": " + total);
			output.write("Case #" + current_case + ": " + total + "\n");
		}
		output.close();
	}

	private static boolean isRecycled(int n, int m) {
		String sn = ""+n;
		String sm = ""+m;
		if (sn.length() != sm.length()) return false;
		int totaln = 0, totalm = 0;
		for (int i = 0; i < sn.length(); i++){
			totaln += sn.charAt(i);
			totalm += sm.charAt(i);
		}
		if (totaln != totalm) return false;
		for (int i = 0; i < sn.length()-1; i++){
			sm = sm.charAt(sm.length() - 1) + sm.substring(0, sm.length() - 1);
			if (sm.equals(sn)) return true;
		}
		return false;
	}
}

package jp.funnything.competition.util;

import java.util.Arrays;
import java.util.Iterator;

/**
 * Do NOT change the element in iteration
 */
public class Permutation implements Iterable< int[] > , Iterator< int[] > {
    public static int[] fromNumber( long value , final int n ) {
        final int[] data = new int[ n ];

        for ( int index = 0 ; index < n ; index++ ) {
            data[ index ] = index;
        }

        for ( int index = 1 ; index < n ; index++ ) {
            final int pos = ( int ) ( value % ( index + 1 ) );

            value /= index + 1;

            final int swap = data[ index ];
            data[ index ] = data[ pos ];
            data[ pos ] = swap;
        }

        return data;
    }

    public static long toNumber( final int[] perm ) {
        final int[] data = Arrays.copyOf( perm , perm.length );

        long c = 0;

        for ( int index = data.length - 1 ; index > 0 ; index-- ) {
            int pos = 0;
            for ( int index_ = 1 ; index_ <= index ; index_++ ) {
                if ( data[ index_ ] > data[ pos ] ) {
                    pos = index_;
                }
            }

            final int t = data[ index ];
            data[ index ] = data[ pos ];
            data[ pos ] = t;

            c = c * ( index + 1 ) + pos;
        }

        return c;
    }

    private final int _n;
    private final int[] _data;
    private final int[] _count;
    int _k;

    public Permutation( final int n ) {
        _n = n;

        _data = new int[ n ];
        for ( int index = 0 ; index < n ; index++ ) {
            _data[ index ] = index;
        }

        _count = new int[ n + 1 ];
        for ( int index = 1 ; index <= n ; index++ ) {
            _count[ index ] = index;
        }

        _k = 1;
    }

    @Override
    public boolean hasNext() {
        return _k < _n;
    }

    @Override
    public Iterator< int[] > iterator() {
        return this;
    }

    @Override
    public int[] next() {
        final int i = _k % 2 != 0 ? _count[ _k ] : 0;

        final int t = _data[ _k ];
        _data[ _k ] = _data[ i ];
        _data[ i ] = t;

        for ( _k = 1 ; _count[ _k ] == 0 ; _k++ ) {
            _count[ _k ] = _k;
        }
        _count[ _k ]--;

        return _data;
    }

    @Override
    public void remove() {
    }
}
