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.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;


public class Recycle {
	
	public static void main (String[] args){

			String output = "";
			try {
				System.out.println("start : "  + new java.util.Date().toString());
				FileOutputStream fo = null;
				FileInputStream fi = null;
				fi = new FileInputStream("F:\\adarsh\\codejam10\\codejam12\\src\\C-small-attempt0.in");
				BufferedReader reader = new BufferedReader(new InputStreamReader(fi));
				Scanner sc = new Scanner(reader);
				int testCases = sc.nextInt();
				System.out.println("test cases : " + testCases);
				 
				fo = new FileOutputStream("F:\\adarsh\\codejam10\\codejam12\\src\\C-small.out");
				PrintStream ps = new PrintStream(fo);
				sc.nextLine();
				
				for (int i = 1; i <= testCases;  i++) {
					output = "";
					int j = 0;
					String data = sc.nextLine();
					System.out.println("data : " + data);
					String[] dataarray = data.split(" ");
					int lowerlimit = Integer.parseInt(dataarray[j++]);
					int upperlimit = Integer.parseInt(dataarray[j++]);
					sout("lowerlimit" , ""+lowerlimit);
					sout("upperlimit" , ""+upperlimit);
					int out = performWork(lowerlimit , upperlimit);	
					sout("out" , ""+out);
					ps.println("Case #"+i+": "  + out);
				}
				ps.close();
				fo.close();
				System.out.println("end : " + new java.util.Date().toString());

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
	
		private static int performWork(int lowerlimit , int upperlimit)
		{
			int output = 0;
			java.util.ArrayList pairs = new ArrayList();
			
			int templow = lowerlimit;
			for(; templow<= upperlimit ; templow++)
			{
				String temp = ""+templow;
				sout("temp" , temp);
				for(int i = 1 ; i < temp.length() ; i++)
				{
					int newnumber = Integer.parseInt(temp.substring(i) + temp.substring(0, i));
					sout("newnumber" , ""+newnumber);
					if(newnumber >= lowerlimit && newnumber <= upperlimit && !((""+newnumber).equals(temp)))
					{
						if(!pairexists(pairs , temp , ""+newnumber))
						{
							String[] t = new String[2];
							t[0] = temp;
							t[1] = ""+newnumber;
							pairs.add(t);
							output++;
						}
					}
				}
				
			}
			sout("output" , ""+output);
			return output;
		}

		private static boolean pairexists(java.util.ArrayList pairs , String temp , String newnumber)
		{
			for(int i = 0 ; i < pairs.size(); i++)
			{
				String[] t = (String[]) pairs.get(i);
				if((t[0].equals(temp) && t[1].equals(newnumber)) || (t[1].equals(temp) && t[0].equals(newnumber)))
				{
					sout("pair exists" , "true");
					return true;
				}
			}
			sout("pair exists" , "false");
			return false;
		}
		
		private static void sout(String str1 , String str2)
		{
			System.out.println(str1 + " : " + str2);
		}
	}
			


	

