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 fixjava;

import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;

public class ParallelWorkQueue<I, O> implements Iterable<Future<O>> {

	public interface CallableFactory<I, O> {
		public Callable<O> newInstance(I input);
	}

	private ExecutorService threadPool;
	private LinkedBlockingQueue<Future<O>> workQueue;
	private Future<O> poisonPill = null;

	public ParallelWorkQueue(final int numThreads, final Iterable<I> inputs, final CallableFactory<I, O> callableFactory) {
		threadPool = Executors.newFixedThreadPool(numThreads);

		// Keep a max of (100x the number of threads) items in the work queue at a time 
		workQueue = new LinkedBlockingQueue<>(numThreads * 100);

		// Create work-producer thread
		new Thread() {
			public void run() {
				try {
					for (final I input : inputs)
						workQueue.put(threadPool.submit(callableFactory.newInstance(input)));

					// poisonPill was null until now, and null can never be put in the queue, so
					// the barrier can't be triggered. Now set poisonPill to another value that it
					// can only attain once, a NOP work unit.
					workQueue.put(poisonPill = threadPool.submit(new Callable<O>() {
						@Override
						public O call() throws Exception {
							return null;
						}
					}));

					// Shut down threads after last callable invoked
					threadPool.shutdown();

				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
			};
		}.start();

	}

	@Override
	public Iterator<Future<O>> iterator() {
		return new Iterator<Future<O>>() {
			Future<O> next = null;
			boolean nextConsumed = true;

			@Override
			public boolean hasNext() {
				if (nextConsumed) {
					try {
						next = workQueue.take();
					} catch (InterruptedException e) {
						throw new RuntimeException(e);
					}
					nextConsumed = false;
				}
				// Check for poison pill
				if (next == poisonPill) {
					try {
						// Get result of poison pill Future so thread exits
						next.get();
					} catch (Exception e) {
						throw new RuntimeException(e);
					}
					return false; // at end
				} else {
					return true;
				}
			}

			@Override
			public Future<O> next() {
				if (nextConsumed)
					throw new RuntimeException("Can't call next() twice without calling hasNext()");
				nextConsumed = true;
				return next;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	/**
	 * Return an Iterable<Integer> that iterates from 0 to n-1 inclusive. Can be
	 * passed into the "inputs" parameter of ParallelWorkQueue.
	 */
	public static Iterable<Integer> makeIntRangeIterable(final int n) {
		return new Iterable<Integer>() {
			private final int max = n;

			@Override
			public Iterator<Integer> iterator() {
				return new Iterator<Integer>() {
					int num = 0;

					@Override
					public boolean hasNext() {
						return num < max;
					}

					@Override
					public Integer next() {
						return num++;
					}

					@Override
					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
			}
		};
	}
}

