package jp.funnything.competition.util;

import java.math.BigInteger;

/**
 * Utility for BigInteger
 */
public class BI {
    public static BigInteger ZERO = BigInteger.ZERO;
    public static BigInteger ONE = BigInteger.ONE;

    public static BigInteger add( final BigInteger x , final BigInteger y ) {
        return x.add( y );
    }

    public static BigInteger add( final BigInteger x , final long y ) {
        return add( x , v( y ) );
    }

    public static BigInteger add( final long x , final BigInteger y ) {
        return add( v( x ) , y );
    }

    public static int cmp( final BigInteger x , final BigInteger y ) {
        return x.compareTo( y );
    }

    public static int cmp( final BigInteger x , final long y ) {
        return cmp( x , v( y ) );
    }

    public static int cmp( final long x , final BigInteger y ) {
        return cmp( v( x ) , y );
    }

    public static BigInteger div( final BigInteger x , final BigInteger y ) {
        return x.divide( y );
    }

    public static BigInteger div( final BigInteger x , final long y ) {
        return div( x , v( y ) );
    }

    public static BigInteger div( final long x , final BigInteger y ) {
        return div( v( x ) , y );
    }

    public static BigInteger mod( final BigInteger x , final BigInteger y ) {
        return x.mod( y );
    }

    public static BigInteger mod( final BigInteger x , final long y ) {
        return mod( x , v( y ) );
    }

    public static BigInteger mod( final long x , final BigInteger y ) {
        return mod( v( x ) , y );
    }

    public static BigInteger mul( final BigInteger x , final BigInteger y ) {
        return x.multiply( y );
    }

    public static BigInteger mul( final BigInteger x , final long y ) {
        return mul( x , v( y ) );
    }

    public static BigInteger mul( final long x , final BigInteger y ) {
        return mul( v( x ) , y );
    }

    public static BigInteger sub( final BigInteger x , final BigInteger y ) {
        return x.subtract( y );
    }

    public static BigInteger sub( final BigInteger x , final long y ) {
        return sub( x , v( y ) );
    }

    public static BigInteger sub( final long x , final BigInteger y ) {
        return sub( v( x ) , y );
    }

    public static BigInteger v( final long value ) {
        return BigInteger.valueOf( value );
    }
}

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

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/**
 *
 * @author agraa1
 */
public class Recycle {
    public static void main(String[] args) throws Exception {
        Recycle puzzle = new Recycle();
        puzzle.solve();
    }
    
    private void solve() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("E:\\C-small-attempt0.in"));
        String line = br.readLine();
        for (int count=0; count<Integer.valueOf(line); count++) {
            showResult(count+1, br.readLine());
        }
        br.close();
    }
    
    private void showResult(int caseNum, String input) {
        List<Integer> recNumbers = new ArrayList<Integer>();
        StringTokenizer tokenizer = new StringTokenizer(input, " ");
        String A = tokenizer.nextToken();
        String B = tokenizer.nextToken();
        if (Integer.valueOf(B) > 10) {
            findSolution(A, B, recNumbers);
        }
        StringBuilder builder = new StringBuilder("Case #");
        builder.append(caseNum);
        builder.append(": ");
        builder.append(recNumbers.size());
        System.out.println(builder.toString());
    }
    
    private void findSolution(String A, String B, List recNumbers) {
        int numA = Integer.valueOf(A);
        int numB = Integer.valueOf(B);
        for (int num = numA; num < numB; num++) {
            getRecycles(num, numA, numB, recNumbers);
        }
    }
    
    private void getRecycles(int num, int numA, int numB, List recNumbers) {
        String toBeRec = ""+num;
        //System.out.println("Recycles for " + num);
        Set<Integer> recyclesForNum = new HashSet<Integer>();
        for (int count=1; count<toBeRec.length(); count++) {
            int recycledNum = Integer.valueOf(toBeRec.substring(toBeRec.length() - count, toBeRec.length()) + toBeRec.substring(0, toBeRec.length() - count));
            //System.out.println("" + count + ":" + recycledNum);
            if ( recycledNum <= numB && recycledNum >= numA && recycledNum > num) {
                //System.out.println("#" + (recNumbers.size()+1) + " ----------------" + num + "--" + recycledNum);
                recyclesForNum.add(recycledNum);
            }
        }
        recNumbers.addAll(recyclesForNum);
    }
}
