package Main;

import com.sun.deploy.util.ArrayUtil;
import org.apache.commons.lang3.ArrayUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: arran
 * Date: 14/04/12
 * Time: 3:12 PM
 * To change this template use File | Settings | File Templates.
 */
public class Round {
    public StringBuilder parse(BufferedReader in) throws IOException {
        StringBuilder out = new StringBuilder();
        String lineCount = in.readLine();

        for (int i = 1; i <= Integer.parseInt(lineCount); i++)
        {
            out.append("Case #"+i+": ");
            System.err.println("Case #"+i+": ");
            String line = in.readLine();
            String[] splits = line.split(" ");
            int p1 = Integer.valueOf(splits[0]);
            int p2 = Integer.valueOf(splits[1]);
            int r = pairRecyclable(p1, p2);
            out.append(r);

//            if (i < Integer.parseInt(lineCount))
                out.append("\n");
        }
        return out;
    }

    public static int pairRecyclable(int i1, int i2) {
        HashSet<String> hash = new HashSet<String>();
        for (int i = i1; i < i2; i++)
        {
            String istr = String.valueOf(i);
            if (istr.length() < 2) continue;
            for (int p = 0; p < istr.length() ; p++)
            {
                String nistr = istr.substring(p,istr.length()).concat(istr.substring(0,p));
                if (Integer.valueOf(nistr) < i1) continue;
                if (Integer.valueOf(nistr) > i2) continue;
                if (nistr.equals(istr)) continue;
                String cnistr = (Integer.valueOf(nistr) > Integer.valueOf(istr)) ?  istr + "," + nistr : nistr + "," + istr;
                hash.add(cnistr);
            }
        }
        return hash.size();
    }

    public static void main(String[] args) {
        InputStreamReader converter = null;
        try {
            int attempt = 0;
            String quest = "C";
//            String size = "small-attempt";
            String size = "large";
            converter = new InputStreamReader(new FileInputStream("src/resource/"+quest+"-"+size+".in"));
            BufferedReader in = new BufferedReader(converter);
            Round r = new Round();
            String str = r.parse(in).toString();
            System.out.print(str);
            FileOutputStream fos = new FileOutputStream(quest+"-"+size+".out");
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(str);
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

package code.jam;

import java.io.*;
import java.util.*;

public class CodeJam {
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        
        String name = "test";
        
        Scanner in = new Scanner( new BufferedReader( new FileReader(name + ".in") ) );
        BufferedWriter out = new BufferedWriter( new FileWriter(name + ".out") );
        
        int req_count;
        int seed , count  = 0, factor , temp;
        int c2;
        int i , j , k = 1;
        int[] parts = null;
        boolean[] arr = null;
        
        int N = in.nextInt();
        
        while(in.hasNext())
        {
            int start = in.nextInt();
            int end = in.nextInt();
            req_count = 0;
            
            arr = new boolean[end+1];
            
            filterArray(arr);
            
            for(i=start ; i<end ; i++)
            {
                if(!arr[i])
                {
                    seed = i;
                    count = digitsCount(seed);
                    
                    if(count > 2)
                    {
                        parts = new int[count-1];
                        factor = 10;

                        for(j=(count-2) ; j >= 0 ; j--)
                        {
                            parts[j] = seed/factor;
                            factor = factor*10;
                        }
                        
                        for(j=1 ; j<=parts.length ; j++)
                        {
                            factor = (int)Math.pow(10,count-j);
                            temp = seed%factor;
                            temp = temp*((int)Math.pow(10,j))+parts[j-1];
                            
                            c2 = digitsCount(temp);
                            
                            if(count == c2)
                            {
                                if( (temp >= start) && (temp <= end) )
                                {
                                    if(!arr[seed])
                                        arr[seed] = true;
                                    
                                    if(!arr[temp])
                                        req_count++;
                                }
                            }
                        }
                    }
                    else if(count == 2)
                    {
                        temp = seed%10;
                        temp = (temp*10)+(seed/10);
                        
                        if( (temp >= start) && (temp <= end) )
                        {
                            req_count++;
                            arr[temp] = true;
                            arr[seed] = true;
                        }
                    }
                }
            }
            
            out.write("Case #" + k + ": " + req_count);
            out.newLine();
            k++;
        }
        
        out.close();
    }
    
    //----------------------------------------------------------------------
    
    public static int digitsCount(int num)
    {
        int count = 0;
        
        while(num > 0)
        {
            count++;
            num = num/10;
        }
        
        return count;
    }
    
    //----------------------------------------------------------------------
    
    public static void filterArray(boolean[] arr)
    {
        int length = arr.length;
        
        int i;
        int temp = 0;
        
        
        if( (length >= 10) && (length < 100) )
        {
            temp = 11;
            for(i=2 ; i<=10 && temp <= length ; i++)
            {
                arr[temp] = true;
                temp = i*11;
            }
        }
        
        if( (length >= 100) && (length < 1000) )
        {
            temp = 111;
            for(i=2 ; i<=10 && temp <= length ; i++)
            {
                arr[temp] = true;
                temp = i*111;
            }
        }
        
        if( (length >= 1000) && (length < 10000) )
        {
            temp = 1111;
            for(i=2 ; i<=10 && temp <= length ; i++)
            {
                arr[temp] = true;
                temp = i*1111;
            }
        }
        
        if( (length >= 10000) && (length < 100000) )
        {
            temp = 11111;
            for(i=2 ; i<=10 && temp <= length; i++)
            {
                arr[temp] = true;
                temp = i*11111;
            }
        }
        
        if( (length >= 100000) && (length < 1000000) )
        {
            temp = 111111;
            for(i=2 ; i<=10 && temp <= length ; i++)
            {
                arr[temp] = true;
                temp = i*111111;
            }
        }
        
        if( (length >= 1000000) && (length <= 2000000) )
            arr[1111111] = true;
    }
}
