package it.simone.google.code.jam2012;

import java.util.HashSet;
import java.util.Set;

public class RecycledNumber implements GoogleCodeExercise {

	int a = 0;
	int b = 0;
	Set<Couple> distinctCouple = null;
	long maxTime = 0;

	private class Couple {
		int x = 0;
		int y = 0;

		public Couple(int x, int y) {
			this.x = x;
			this.y = y;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + x;
			result = prime * result + y;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Couple other = (Couple) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (x == other.x && y == other.y)
				return true;
			if (x == other.y && y == other.x)
				return true;
			return false;
		}

		private RecycledNumber getOuterType() {
			return RecycledNumber.this;
		}

	}

	@Override
	public String execute(String line) {
		int result = 0;

		String[] data = line.split(" ");
		a = Integer.parseInt(data[0]);
		b = Integer.parseInt(data[1]);
		initialize();

		int n = a;
		//
		while (n <= b) {
			result += analize(n);
			n++;
//			if (n % 1000 == 0)
//				System.out.println(n);
		}

		System.out.println(result);
		return "" + result;
	}

	@Override
	public void initialize() {
		distinctCouple = new HashSet<Couple>();
	}

	private int analize(int number) {
		String numString = "" + number;
		initialize();
		for (int i = 1; i < numString.length(); i++) {
			int m = shift(numString, i);
			if (a <= number && number < m && m <= b) {
				Couple couple=new Couple(number,m);
				if (!distinctCouple.contains(couple))
					distinctCouple.add(couple);
			}
		}
		return distinctCouple.size();
	}

	private int shift(String numString, int posx) {

		String result = null;
		result = "" + numString.substring(posx) + numString.substring(0, posx);

		return Integer.parseInt(result.toString());
	}

}

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





public class CodeJamProblem implements Runnable {
//Problem C
	public boolean InRisultati(ArrayList<String> array, String controllo) {
		for(int i =0; i<array.size();i++) {
			if(array.get(i)==controllo) {
				System.out.println("Risutlato gia contato");
				return true;
			}
		}
		return false;
	}
	
	public int getRisultato(int[] items, int caso) {
		// n=6 s=2 p=8 29 20 8 18 18 21
		int vincitori = 0;
		
		Arrays.sort(items);
		int A = items[0];
	    int B = items[1];
	    int current_n =-1;
	    int current_m =-1;
	    // 1 digit, no solution:
	    if (A<10 && B<10)
	    {
	      return 0;
	    }
	    int[] range = new int[B-A+1];
	    //System.out.println("Range LEN:"+ range.length);
	    for(int i=0;i<range.length;i++) {
	    	range[i] = i+A;
	    	//if(caso==3)
	    	//	System.out.print(range[i]+ " ");
	    }
	    int count = 0;
	    
	    ArrayList<String> risultati = new ArrayList<String>();
	    
	    if(caso==3)
	    System.out.println(" ");
	    for (int i = 0; i < range.length; i++)
	    {
	      current_n = range[i];
	      current_m = range[i];
	      String rot_m = ""+current_m;
	      //if(caso==3)
	    	//  System.out.println("Numero: " +current_m);
	      
	      int len = (range[i]+"").length();
	      for (int j = 0; j < len; j++)
	      {
	    	  if(caso==3)
	    		  System.out.print(" " + rot_m.substring(rot_m.length()-1) + 
	    			  rot_m.substring(0,len-1));
	    	  rot_m = (   
	    			  rot_m.substring(rot_m.length()-1) + 
	    			  rot_m.substring(0,len-1)
	    			  );
	        // A ² n < m ² B
	        if (Integer.parseInt(rot_m) <= B && Integer.parseInt(rot_m) > current_n && Integer.parseInt(rot_m) >= A)
	        {
	        	//if(InRisultati(risultati,current_n+rot_m)==false) {
	        	//	risultati.add(current_n+rot_m);
	        		vincitori++;
	        		
	        	//}
	        	
	        }
	      }
	      if(caso==3)
	    	  System.out.println(" ");
	    }
	    if(caso==3)
	    	System.out.println("Winner :"+ vincitori);
		return vincitori;
	}
	
	
	public void solve() throws IOException {
		String test = "small";
		//String test = "large";
		Scanner in = new Scanner(new File("C-large.in")).useDelimiter(System.getProperty("line.separator")); 
        PrintWriter out = new PrintWriter(new File("C-large.out"));
        
        int lineNum = 0;
        int testNum = 0;
        int totalTests = 0;
        int[] points = new int[1];
        while (in.hasNext()) {
        	String currentLine = in.next();
        	if(lineNum ==0 ) {
        		//Do Nothing!
        		totalTests = Integer.parseInt(currentLine);
        		lineNum++;
        		continue;
        	}
        	if(lineNum>0) {
        		String[] x = currentLine.split(" ");
        		points = new int[x.length];
                for(int i = 0;i<x.length;i++) {
               		points[i] = Integer.parseInt(x[i]);
                }
        		int risultato = getRisultato(points, testNum);
        		//if(testNum==3)
        		System.out.println("Case #"+(testNum+1)+": "+ risultato);
        		out.println("Case #"+(testNum+1)+": "+ risultato);
        		testNum++;
        	}
            lineNum++;
        }
        in.close();
        out.close();
        
		
	}
	public void run() {
        try {
            solve();
        } catch (IOException e) {
            System.exit(1);
        }
    }

    public static void main(String[] s) {
        new Thread(new CodeJamProblem()).start();
    }
	
	
	
}
