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.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Google Code Jam 2012
 * 	Qualifier Round
 * 	Problem C
 * 
 * @author Brian Crockford
 */
public class GCJ20120C {
	public static final String inputFilename = "C-large.in";	

	
	
	static class LongPair {		
		final long[] data = new long[2];
		
		public LongPair(long x, long y) {
			set(x, y);
		}
		
		public long getX() { return data[0]; }
		public long getY() { return data[1]; }
		public void set(long x, long y) {
			data[0] = Math.min(x,y); data[1] = Math.max(x,y);
		}
		
		@Override public boolean equals(Object o) {
			LongPair p = (LongPair) o;
			if (null != p) {
				return (data[0] == p.getX() && data[1] == p.getY())
						|| (data[1] == p.getY() && data[0] == p.getX());
			}
			return false;
		}
		
		@Override public int hashCode() {
	        long hash = 1;
	        hash = hash * 31 + data[0];
	        hash = hash * 31 + data[1];
	        return (int) hash;
		}
	}
	
	static class DataSet {
		private static HashSet<LongPair> pairs;
		static {
			pairs = new HashSet<LongPair>(500000);
		}
		
		public long begin,
					end;
		
		@Override
		public String toString() {
			return new StringBuilder("<$DataSet: ").append(begin).append(", ").append(end).append(">").toString();
		}
		
		public long countRecycles() {
			int digits = countDigits(begin);
			for (long i=begin; i<=end; ++i) {
				for (int j=1; j<digits; ++j) {
					long result = shiftLong(i, j);
					if (i != result && result >= begin && result <= end)
						pairs.add(new LongPair(i, result));
				}
			}
			long result = pairs.size();
			pairs.clear();
			return result;
		}
		
		public static DataSet parse(String s) {
			DataSet result = new DataSet();
				result.begin = Long.parseLong(s.substring(0, s.indexOf(' ')));
				result.end = Long.parseLong(s.substring(1+s.indexOf(' ')));
			return result;
		}
	}
	
	
	
	public static Reader getFileReader(String filename) {
		BufferedReader result = null;
		try {
			result = new BufferedReader(new FileReader(filename));
		}
		catch (FileNotFoundException e) {
			System.err.println("Couldn't find the file \""+filename+"\"");
			e.printStackTrace();
		}
		return result;
	}
	
	
	
	public static String readLine(BufferedReader reader) {
		try {
			return reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	public static void closeReader(Reader r) {
		try { 
			r.close(); 
		} catch (IOException e) { e.printStackTrace(); }
	}
	
	
	
	public static Writer getFileWriter(String filename) {
		BufferedWriter result = null;
		try {
			result = new BufferedWriter(new FileWriter(filename));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	
	public static void writeLine(BufferedWriter writer, String line) {
		try {
			writer.write(line);
			writer.newLine();
		} catch (IOException e) { e.printStackTrace(); }
	}
	
	
	
	public static void closeWriter(Writer w) {
		try { 
			w.close(); 
		} catch (IOException e) { e.printStackTrace(); }
	}
	
	
	
	public static String formatCase(int caseNumber, long result) {
		return new StringBuilder("Case #").append(caseNumber).append(": ").append(result).toString();
	}
	
	
	
	public static int countDigits(long n) {
		return (int) 1 + (int)Math.floor(Math.log10(n));
	}
	
	
	public static int getDigit(long n, int digit) {
		return(int)(n/(long)Math.pow(10, digit-1)) % 10;
	}
	
	
	public static long shiftLong(long n, int digits) {
		long mask = (long) Math.pow(10, digits);
		long upshift = (long) Math.pow(10, (countDigits(n)-digits));
		return (upshift*(n%mask)) + (n/mask);		
	}
	
	
	
	public static void main(String[] args) {		
		BufferedReader in = (BufferedReader) getFileReader(inputFilename);
		int lineCount = Integer.parseInt(readLine(in));
		List<DataSet> dataSets = new ArrayList<DataSet>();
		for (int i=0; i<lineCount; ++i) {
			dataSets.add(DataSet.parse(readLine(in)));
		}
		closeReader(in);
		
		BufferedWriter out = (BufferedWriter) getFileWriter(inputFilename.replace(".in", ".out"));
		for (int i=0; i<lineCount; ++i) {
			long result = dataSets.get(i).countRecycles();
			System.out.println(formatCase(i+1, result));
			writeLine(out, formatCase(i+1, result));
		}
		closeWriter(out);
	}

}