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

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class RecycledNumbers {

    public static int recycle(int source, int digits) {
        int pow = (int) Math.pow(10, digits - 1);
        int mod = source % 10;
        return pow * mod + source / 10;
    }

    public static class Range {
        public final int A;
        public final int B;

        public Range(int A, int B) {
            this.A = A;
            this.B = B;
        }
    }

    public static Iterator<Range> iterator(Readable input) {
        final Scanner scanner = new Scanner(input);

        return new Iterator<Range>() {
            private int cases = scanner.nextInt();

            @Override
            public boolean hasNext() {
                return cases > 0;
            }

            @Override
            public Range next() {
                int A = scanner.nextInt();
                int B = scanner.nextInt();

                cases--;
                return new Range(A, B);
            }

            @Override
            public void remove() {
                throw new AbstractMethodError();
            }
        };
    }

    public static int recycledPairs(Range scores) {
        int digits = Integer.toString(scores.A).length();
        HashSet<String> pairs = new HashSet<String>();
        for (int i = scores.A; i <= scores.B; i++) {
            int recycled = recycle(i, digits);
            while (recycled != i) {
                if (recycled >= scores.A && recycled <= scores.B) {
                    int first = i < recycled ? i : recycled;
                    int second = i > recycled ? i : recycled;

                    pairs.add(String.format("%d%d", first, second));
                }
                recycled = recycle(recycled, digits);
            }
        }

        return pairs.size();
    }

    public static void main(String[] args) throws FileNotFoundException {
        FileReader fileReader = new FileReader(args[0]);

        int caseId = 1;
        Iterator<Range> iterator = iterator(fileReader);

        while (iterator.hasNext()) {
            int result = recycledPairs(iterator.next());
            System.out.format("Case #%s: %s\n", caseId, result);
            caseId++;
        }
    }
}
