/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;

/**
 *
 * @author imgps
 */
public class C {
    public static void main(String args[]) throws Exception{
        int A,B;
        int ctr = 0;
        int testCases;
        
        Scanner sc = new Scanner(new FileInputStream("C-small-attempt0.in "));
        testCases = sc.nextInt();
        int input[][] = new int[testCases][2];
        for(int i=0;i<testCases;i++)
        {
          //  System.out.print("Enter input A B: ");
            input[i][0] = sc.nextInt();
            input[i][1] = sc.nextInt();
            
        }
  

for(int k=0;k<testCases;k++){
      ctr = 0;
      A = input[k][0];
      B = input[k][1];
        for(int i = A;i<=B;i++){
            int num = i;
            int nMul = 1;
            int mul = 1; 
            int tnum = num/10;
            while(tnum > 0){
                mul = mul *10;
                tnum= tnum/10;
            }
            tnum = num;
            int n = 0;
            while(tnum > 0 && mul >= 10){
                nMul = nMul * 10;
                n = (num % nMul) * mul + (num / nMul);
                mul = mul / 10;
                tnum = tnum / 10;
                for(int m=num;m<=B;m++){
                    
                    if(n == m && m > num){
                        ctr++;
                    }
                }
                
                
                
         
            }
        }
        System.out.println("Case #"+(k+1)+": "+ctr);
        
    }
    }
}

package fixjava;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

public class CountedSet<T> implements Set<T> {

	HashMap<T, Integer> map = new HashMap<T, Integer>();
	private static final Integer ONE = new Integer(1);

	public CountedSet(Iterable<T> collection) {
		for (T t : collection)
			add(t);
	}

	@Override
	public boolean add(T item) {
		Integer count = map.get(item);
		if (count == null) {
			map.put(item, ONE);
			return true;
		} else {
			map.put(item, count + 1);
			return false;
		}
	}

	/** New method to fetch count, not in normal Sets.  Returns null if key is not in set. */
	public Integer get(String key) {
		return map.get(key);
	}

	/** New method to fetch count, not in normal Sets.  Returns zero if key is not in set. */
	public int getCount(String key) {
		Integer count = map.get(key);
		return count == null ? 0 : count;
	}

	/** Extra method to iterate over entries consisting of both items and counts */
	public Iterator<Entry<T, Integer>> iteratorWithCounts() {
		return map.entrySet().iterator();
	}

	/** Extra method to iterate over entries consisting of both items and counts */
	public Iterable<Entry<T, Integer>> iteratableWithCounts() {
		return map.entrySet();
	}

	/** Convert into sorted list of <item, count> pairs */
	public ArrayList<Pair<T, Integer>> toListSortedByCount(boolean ascending) {
		ArrayList<Pair<T, Integer>> list = Pair.mapToList(map);
        Pair.sortPairsByRight(list, ascending);
		return list;
	}

	// ----- boilerplate stuff: -----

	@Override
	public boolean addAll(Collection<? extends T> collection) {
		boolean changed = false;
		for (T item : collection) {
			add(item);
			changed = true;
		}
		return changed;
	}

	@Override
	public void clear() {
		map.clear();
	}

	@Override
	public boolean contains(Object item) {
		return map.containsKey(item);
	}

	@Override
	public boolean containsAll(Collection<?> collection) {
		boolean containsAll = true;
		for (Object item : collection) {
			if (!contains(item)) {
				containsAll = false;
				break;
			}
		}
		return containsAll;
	}

	@Override
	public boolean isEmpty() {
		return map.isEmpty();
	}

	/** Just iterates over keys, not counts */
	@Override
	public Iterator<T> iterator() {
		return map.keySet().iterator();
	}

	@Override
	public boolean remove(Object item) {
		return map.remove(item) != null;
	}

	@Override
	public boolean removeAll(Collection<?> collection) {
		boolean changed = false;
		for (Object item : collection) {
			if (remove(item)) {
				changed = true;
				break;
			}
		}
		return changed;
	}

	@Override
	public boolean retainAll(Collection<?> arg0) {
		throw new RuntimeException("Not implemented");
	}

	@Override
	public int size() {
		return map.size();
	}

	/** Just returns keys in array, not counts */
	@Override
	public Object[] toArray() {
		return map.keySet().toArray();
	}

	/** Just returns keys in array, not counts */
	@Override
	public <U> U[] toArray(U[] arr) {
		return map.keySet().toArray(arr);
	}
}
