import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

public class ReadFile {
	public static void main(String[] args) throws Exception {
		String srcFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt1.in.txt";
		String destFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt0.out";
		File file = new File(srcFile);
		List<String> readData = new ArrayList<String>();
		FileReader fileInputStream = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileInputStream);
		StringBuilder stringBuilder = new StringBuilder();
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			readData.add(line);
		}
		stringBuilder.append(generateOutput(readData));
		File writeFile = new File(destFile);
		if (!writeFile.exists()) {
			writeFile.createNewFile();
		}
		FileWriter fileWriter = new FileWriter(writeFile);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		bufferedWriter.write(stringBuilder.toString());
		bufferedWriter.close();
		System.out.println("output" + stringBuilder);
	}

	/**
	 * @param number
	 * @return
	 */
	private static String generateOutput(List<String> number) {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < number.size(); i++) {
			if (i == 0) {
				continue;
			} else {
				String tempString = number.get(i);
				String[] temArr = tempString.split(" ");
				stringBuilder.append("Case #" + i + ": ");
				stringBuilder.append(getRecNumbers(temArr[0], temArr[1]));
				stringBuilder.append("\n");
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}

	// /* This method accepts method for google code jam */
	// private static String method(String value) {
	// int intValue = Integer.valueOf(value);
	// double givenExpr = 3 + Math.sqrt(5);
	// double num = Math.pow(givenExpr, intValue);
	// String valArr[] = (num + "").split("\\.");
	// int finalVal = Integer.valueOf(valArr[0]) % 1000;
	//
	// return String.format("%1$03d", finalVal);
	//
	// }
	//
	// /* This method accepts method for google code jam */
	// private static String method2(String value) {
	// StringTokenizer st = new StringTokenizer(value, " ");
	// String test[] = value.split(" ");
	// StringBuffer str = new StringBuffer();
	//
	// for (int i = 0; i < test.length; i++) {
	// // test[i]=test[test.length-i-1];
	// str.append(test[test.length - i - 1]);
	// str.append(" ");
	// }
	// str.deleteCharAt(str.length() - 1);
	// String strReversedLine = "";
	//
	// while (st.hasMoreTokens()) {
	// strReversedLine = st.nextToken() + " " + strReversedLine;
	// }
	//
	// return str.toString();
	// }

	private static int getRecNumbers(String no1, String no2) {

		int a = Integer.valueOf(no1);
		int b = Integer.valueOf(no2);
		Set<String> dupC = new HashSet<String>();
		for (int i = a; i <= b; i++) {
			int n = i;
			List<String> value = findPerm(n);
			for (String val : value) {
				if (Integer.valueOf(val) <= b && Integer.valueOf(val) >= a
						&& n < Integer.valueOf(val)) {
					dupC.add(n + "-" + val);
				}
			}
		}
		return dupC.size();
	}

	private static List<String> findPerm(int num) {
		String numString = String.valueOf(num);
		List<String> value = new ArrayList<String>();
		for (int i = 0; i < numString.length(); i++) {
			String temp = charIns(numString, i);
			if (!temp.equalsIgnoreCase(numString)) {
				value.add(charIns(numString, i));
			}
		}
		return value;

	}

	public static String charIns(String str, int j) {
		String begin = str.substring(0, j);
		String end = str.substring(j);
		return end + begin;
	}
}
package nithin.codejam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
	static int valid_pairs[][];
	static int no_valid_pairs=0;
	public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int t,i;
		t=Integer.parseInt(br.readLine());
		
		for(i=0;i<t;i++)
		{
			st=new StringTokenizer(br.readLine());
			int a,b;
			int result=0;
			a=Integer.parseInt(st.nextToken());
			b=Integer.parseInt(st.nextToken());
			
			no_valid_pairs=0;
			valid_pairs=new int[1000][2]; // Will not work for Large Input
			boolean ex1=true,ex2=true,ex3=true;
			
			if(b<=10)
			{
				if(b!=10)
				{
					ex1=false;
					ex2=false;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						ex1=false;
						ex2=false;
						ex3=false;
						result=1;
					}
					
				}
			}
			else if(b<=100)
			{
				if(b!=100)
				{
					ex1=true;
					ex2=false;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						//Extracting 2 digits from 100 will give 1
						ex1=true;
						ex2=false;
						ex3=false;
						result=1;
					}
				}
			}
			else if(b<=1000)
			{
				if(b!=1000)
				{
					ex1=true;
					ex2=true;
					ex3=false;
					result=0;
				}
				else
				{
					if(a==1)
					{
						//Extracting 3 digits from 1000 will give 1
						ex1=true;
						ex2=true;
						ex3=false;
						result=1;
					}
				}
			}
			else
			{
				ex1=true;
				ex2=true;
				ex3=true;
			}
			
			if(ex1==true)
			{
				int j;
				for(j=a;j<=b;j++)
				{
					int last=j%10;
					int rem=j/10;
					int num=-1;
					if(rem!=0)
					{
						if(rem<10)
						{
							num=last*10+rem;
						}
						else if(rem<100)
						{
							num=last*100+rem;
						}
						else if(rem<1000)
						{
							num=last*1000+rem;
						}
						else
						{
							System.out.println("Not a Valid Case for Small Input");
						}
						if(num>=a && num<=b && j!=num)
						{
							if(j<=num)
							{
								if(add(j,num))
									result++;
							}
							else
							{
								if(add(num,j))
									result++;
							}
						}
					}
					

				}
			}
			if(ex2==true)
			{
/*				int j;
				for(j=101;j<=b;j++)
				{
					int last=j%100;
					int rem=j/100;
					int num=-1;
					if(rem!=0)
					{
						if(rem<10) //if rem=10 then it means it was obtained from a number like 1025 by extracting last 2 digits, so you have to multiply last by 100
						{
							num=last*100+rem;
						}
						else if(rem<100)
						{
							num=last*1000+rem;
						}
						else if(rem<1000)
						{
							num=last*10000+rem;
						}
						else
						{
							System.out.println("Not a Valid Case for Small Input");
						}
						if(num>=a && num<=b && j!=num)
						{

							if(add(j,num)) //num is actually m, it is obtained by moving some digits from back of j to the front.
								result++;

						}
					}
					

				}*/
			}
			if(ex3==true)
			{
				//Do Nothing
			}
			System.out.println("Case #"+(i+1)+": "+result);
/*			int j;
			for(j=0;j<no_valid_pairs;j++)
				System.out.println("0th: "+valid_pairs[j][0]+"\t1th: "+valid_pairs[j][1]);*/
		}
	}
	static boolean add(int m,int n)
	{
		//Pass such that m<n
		boolean uniq_pair=true;
		int i;
		
		for(i=0;i<no_valid_pairs;i++)
		{
			int a=valid_pairs[i][0];
			int b=valid_pairs[i][1];
			if((a==m && b==n) || (b==m && a==n))
				return false;
		}
		valid_pairs[no_valid_pairs][0]=m;
		valid_pairs[no_valid_pairs][1]=n;
		no_valid_pairs++;
		return uniq_pair;
	}
}

