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.Iterator;
import java.util.List;

public class Q3M {
	public static void main(String[] args) throws Exception {
		compute();
	}

	private static void compute() throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(new File(
				"C:\\work\\Q3\\C-small-attempt0.in")));
		String line = null;
		int i = Integer.parseInt(br.readLine());
		List l = new ArrayList();
		for (int j = 0; j < i; j++) {
			line = br.readLine();
			String[] nums = line.split(" ");
			l.add(calculate(nums));
		}
		writeOutput(l);
	}

	private static int calculate(String[] nums) {
		int min = Integer.parseInt(nums[0]);
		int max = Integer.parseInt(nums[1]);
		int count = 0;
		List l = new ArrayList();
		for (int i = min; i <= max; i++) {
			for (int times = 1; times < countDigits(i); times++) {
				int res = shiftNum(i, times);
				if (res <= max && i < res) {
					if ((!l.contains((i + ":" + res)))) {
						l.add(i + ":" + res);
						l.add(res + ":" + i);
						count++;
					}
				}
			}
		}
		return count;
	}

	private static boolean checkZeros(int temp, int res) {
		if (temp % 10 == 0 || res % 10 == 0)
			return false;
		return true;
	}

	private static int shiftNum(int n, int times) {
		int base = (int) Math.pow(10, times);
		int temp2 = n / base;
		int placeHolder = (int) Math.pow((double) 10,
				(double) countDigits(temp2));
		int res = placeHolder * (n % base) + temp2;
		if (countDigits(res) == countDigits(n)) {
			return res;
		} else {
			return 2000001;
		}
	}

	public static int countDigits(int x) {
		if (x < 10)
			return 1;
		else {
			return 1 + countDigits(x / 10);
		}
	}

	private static void writeOutput(List l) throws Exception {
		StringBuffer b = new StringBuffer();
		int i = 1;
		BufferedWriter br = new BufferedWriter(new FileWriter(new File(
				"C:\\work\\Q3\\ans.txt")));
		for (Iterator iterator = l.iterator(); iterator.hasNext();) {
			br.write("Case #" + i++ + ": " + iterator.next());
			br.newLine();
		}
		br.close();
	}

}
package codejam2012;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

/**
 *
 * @author devashish
 */
public class ProblemC {

    private static Set<PairedNumber> pairedNumbers = new HashSet<PairedNumber>();
    private static Map<PairedNumber, Integer> pairedNumbersMap = new HashMap<PairedNumber, Integer>();

    public static void main(String[] args) {
        StringTokenizer st = null;
        BufferedReader in;
        PrintWriter out = null;

        try {
            in = new BufferedReader(new FileReader(new File("E:/c.in")));
            out = new PrintWriter(new FileWriter(new File("E:/c.out")));

            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(in.readLine());
            }

            int t = Integer.parseInt(st.nextToken());

            for (int i = 1; i <= t; i++) {
                while (st == null || !st.hasMoreTokens()) {
                    st = new StringTokenizer(in.readLine());
                }

                int A = Integer.parseInt(st.nextToken());
                int B = Integer.parseInt(st.nextToken());

                int outputLine = new ProblemC().getNumberOfRecycledPairs(A, B);

                out.print("Case #" + i + ": " + outputLine + "\n");
            }
        } catch (IOException e) {
        }
        out.flush();
    }

    private int getNumberOfRecycledPairs(int A, int B) {
        pairedNumbers = new HashSet<PairedNumber>();
        pairedNumbersMap = new HashMap<PairedNumber, Integer>();

        String strB = B + "";
        //System.out.println("Count from " + A + " to " + B);

        if (strB.length() == 1) {
            return 0;
        }

        for (int i = A; i <= B; i++) {
            if (isPalindrome(i)) {
                continue;
            }
            if (isSameDigits(i)) {
                continue;
            }

            PairedNumber pairedNumber = new PairedNumber(i);
            boolean added = pairedNumbers.add(pairedNumber);
            //System.out.println("i = " + i + ", added: " + added);
            if (!added) {
                int occurence = 2;
                if (pairedNumbersMap.containsKey(pairedNumber)) {
                    occurence = pairedNumbersMap.get(pairedNumber) + 1;
                }
                pairedNumbersMap.put(pairedNumber, occurence);
            }
        }

        int finalCount = 0;
        Iterator it = pairedNumbersMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            //    System.out.println(pairs.getKey() + " = " + pairs.getValue());
            it.remove();
            int val = (Integer) pairs.getValue();
            finalCount += (val * (val - 1)) / 2;
        }

        return finalCount;
    }

    private boolean isPalindrome(int number) {
        int reversedNumber = 0;
        while (number > 0) {
            int temp = number % 10;
            number = number / 10;
            reversedNumber = reversedNumber * 10 + temp;
        }

        if (number == reversedNumber) {
            return true;
        }

        return false;
    }

    private boolean isSameDigits(int num) {
        String str = num + "";
        char ch = str.charAt(0);
        String check = "";
        for (int i = 0; i < str.length(); i++) {
            check += ch;
        }
        if (str.equals(check)) {
            return true;
        }

        return false;
    }

    public Set<Integer> getPermutations(Integer num, int A, int B) {
        Set<Integer> perms = new HashSet<Integer>();
        int length = (num == 0) ? 1 : (int) Math.log10(num) + 1;
        String numString = num + "";
        for (int i = 1; i < length; i++) {
            String str = numString.substring(i) + numString.substring(0, i);
            //System.out.println(str);
            perms.add(Integer.parseInt(str));
        }
        //perms.add(num);

        return perms;
    }

    class PairedNumber {

        private String pairedNumber = "";

        public PairedNumber(int number) {
            this.pairedNumber = number + "";
        }

        public Set<Integer> getPermutations(Integer num) {
            Set<Integer> perms = new HashSet<Integer>();
            int length = (num == 0) ? 1 : (int) Math.log10(num) + 1;
            String numString = num + "";
            for (int i = 1; i < length; i++) {
                String str = numString.substring(i) + numString.substring(0, i);
                perms.add(Integer.parseInt(str));
            }

            return perms;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final PairedNumber other = (PairedNumber) obj;

            Set<Integer> perms = getPermutations(Integer.parseInt(this.pairedNumber));
            if (perms.contains(Integer.parseInt(other.pairedNumber))) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 13 * hash + (this.pairedNumber != null ? this.pairedNumber.length() : 0);
            return hash;
        }

        @Override
        public String toString() {
            return "PairedNumber{" + "pairedNumber=" + pairedNumber + '}';
        }
    }
}
