import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;


public class GoogleC {
	public String szamol(String input){
		String result="";
		String a=input;
		Integer min=Integer.parseInt(a.substring(0,a.indexOf(' ')));
		a=a.substring(a.indexOf(' ')+1);
		Integer max=Integer.parseInt(a);
		Long count=0l;
		for(Integer i=min; i<=max; i++){
			String e1=i.toString();
			Set<Integer> db=new HashSet<Integer>(); 
			for (int j=1; j<e1.length();j++){
				
				String e2=e1.substring(j)+e1.substring(0,j);
				Integer k=Integer.parseInt(e2);
				if (k>i && k<=max){
					db.add(k);
					//System.out.println(e1+"\t"+e2);
				}
				
			}
			count+=db.size();
		}
		
		return count.toString();
	}
	public static void main(String[] args) {
		GoogleC a=new GoogleC();
		//String filename="input.txt";
		//String filename="C-small-attempt0.in";
		String filename="C-large.in";
		String thisLine;
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			BufferedWriter bw= new BufferedWriter(new FileWriter(filename+".out"));
			thisLine=br.readLine();
			Integer tnum=Integer.parseInt(thisLine);
		     for(int i=0;i<tnum;i++) { // while loop begins here
		    	 	thisLine=br.readLine();
		           System.out.println(thisLine);
		           System.out.println("Case #"+(i+1)+": "+a.szamol(thisLine)+"\n");
		           bw.write("Case #"+(i+1)+": "+a.szamol(thisLine)+"\n");
		         } // end while 
		        // end try
		     br.close();
		     bw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

}

package codeJam;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class Problem3 extends JFrame
{
	//TODO Set file names
	static final String INFILE = "codeJam/inFile3l.txt";
	static final String OUTFILE = "codeJam/outFile3l.txt";
	
	// variable declaration
	JTextArea inText, outText;
	
	public static void main(String[] args)
	{
		new Problem3();
	}
	
	public Problem3()
	{
		DefaultHandler aHandler = new DefaultHandler();
		
		setTitle("Title");
		Container content = this.getContentPane();
		content.setLayout(null);
		
		//////////////////////////////////////////////////////////////////////// LABELS ////////
		JLabel aLabel = new JLabel("Input:");
		aLabel.setBounds(258, 5, 50, 20);
		content.add(aLabel);
		
		aLabel = new JLabel("Output:");
		aLabel.setBounds(791, 5, 50, 20);
		content.add(aLabel);
		
		/////////////////////////////////////////////////////////////////////// TEXTAREAS //////
		inText = new JTextArea();
		JScrollPane scroll = new JScrollPane(inText);
		scroll.setBounds(33, 33, 500, 200);
		content.add(scroll);
		
		outText = new JTextArea();
		scroll = new JScrollPane(outText);
		scroll.setBounds(566, 33, 500, 200);
		content.add(scroll);
		
		//////////////////////////////////////////////////////////////////////// BUTTONS ///////
		JButton aButton = new JButton("Test File");
		aButton.addActionListener(aHandler);
		aButton.setBounds(33, 250, 233, 40);
		content.add(aButton);
		
		aButton = new JButton("Run File");
		aButton.addActionListener(aHandler);
		aButton.setBounds(300, 250, 233, 40);
		content.add(aButton);
		
		aButton = new JButton("Test TextBox");
		aButton.addActionListener(aHandler);
		aButton.setBounds(566, 250, 233, 40);
		content.add(aButton);
		
		aButton = new JButton("Run TextBox");
		aButton.addActionListener(aHandler);
		aButton.setBounds(833, 250, 233, 40);
		content.add(aButton);
		
		setSize(1100, 325);
		setResizable(false);
		setLocation(100, 100);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private class DefaultHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if (e.getActionCommand().equals("Test File"))
			{
				ArrayList<String> fileContents = new ArrayList<String>();
				
				Scanner scanner;
				try
				{
					scanner = new Scanner(new File(INFILE));
					
					while (scanner.hasNextLine())
						fileContents.add(scanner.nextLine());
					
					scanner.close();
				}
				catch (FileNotFoundException e1)
				{
					e1.printStackTrace();
				}
				
				String[] output = new String[fileContents.size()];
				fileContents.toArray(output);
				
				outText.setText("");
				for (int i = 0; i < output.length; i++)
					outText.setText(outText.getText() + "." + output[i] + "." + "\n");
			}
			else if (e.getActionCommand().equals("Run File"))
			{
				ArrayList<String> fileContents = new ArrayList<String>();
				
				Scanner scanner;
				try
				{
					scanner = new Scanner(new File(INFILE));
					
					while (scanner.hasNextLine())
						fileContents.add(scanner.nextLine());
					
					scanner.close();
				}
				catch (FileNotFoundException e1)
				{
					e1.printStackTrace();
				}
				
				String[] output = new String[fileContents.size()];
				fileContents.toArray(output);
				
				String[] answer = myAlgorithm(output);
				
				
				Writer out;
				
				try
				{
					out = new OutputStreamWriter(new FileOutputStream(OUTFILE));
					
					for (int i = 0; i < answer.length; i++)
						out.write(answer[i] + "\n");
					
					out.close();
				}
				catch (FileNotFoundException e1)
				{
					e1.printStackTrace();
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
				}
				
				outText.setText("File Written");
			}
			else if (e.getActionCommand().equals("Test TextBox"))
			{
				String[] output = inText.getText().split("\n");
				
				outText.setText("");
				for (int i = 0; i < output.length; i++)
					outText.setText(outText.getText() + "." + output[i] + "." + "\n");
			}
			else if (e.getActionCommand().equals("Run TextBox"))
			{
				String[] output = inText.getText().split("\n");
				
				String[] answer = myAlgorithm(output);
				
				outText.setText("");
				for (int i = 0; i < answer.length; i++)
					outText.setText(outText.getText() + answer[i] + "\n");
			}
		}
		
		
		// IMPORTANT NOTE:: INFILE is read and OUTFILE is written
		//					in the Project Directory.
		
		// TODO:: Write all of your algorithm code here
		// 		  String[] myData is the lines of input
		//		  You must return a String[]
		public String[] myAlgorithm(String[] myData)
		{
			int numProbs = Integer.parseInt(myData[0]);
			
			String[] workSet = new String[numProbs];
			for (int i = 0; i < numProbs; i++)
			{
				// mess with myData[i + 1] here and output to workSet[i]
				String[] testVals = myData[i + 1].split(" ");
				
				int myMin = Integer.parseInt(testVals[0]);
				int myMax = Integer.parseInt(testVals[1]);
				
				int count = 0;
				
				int mySize = 2000001;
				int[] hugeArray = new int[mySize];
				for (int j = 0; j < mySize; j++)
					hugeArray[j] = 0;
				
				for (int j = myMin; j <= myMax; j++)
				{
					String curNum = Integer.toString(j);
					
					int[] myInts = new int[curNum.length()];
					for (int k = 0; k < curNum.length(); k++)
						myInts[k] = Integer.parseInt("" + curNum.charAt(k));
					
					int smallestInt = 2000004;
					for (int k = 0; k < myInts.length; k++)
					{
						String nextStr = "";
						for (int l = k; l < myInts.length; l++)
							nextStr += "" + myInts[l];
						for (int l = 0; l < k; l++)
							nextStr += "" + myInts[l];
						
						int finalInt = Integer.parseInt(nextStr);
						if (finalInt < smallestInt)
							smallestInt = finalInt;
					}
					
					count += hugeArray[smallestInt];
					hugeArray[smallestInt]++;
				}
				
				workSet[i] = "" + count;
			}
			
			String[] myOutput = new String[numProbs];
			for (int i = 0; i < numProbs; i++)
				myOutput[i] = "Case #" + (i + 1) + ": " + workSet[i];
			
			return myOutput;
		}
	}
}
