Dictionary Survey
Time limit3sMemory limit256 MB
Find how many leading pages of the integers from A to B in lexicographic order pin down both A and B.
Problem
Jihak wrote every natural number from to into a dictionary, one number per page, so the dictionary has pages. The numbers are not ordered by value, they are ordered lexicographically, which means their decimal spellings are compared character by character. For and the pages carry 10, 11, 12, 4, 5, 6, 7, 8, 9 in that order.
Jihak handed the dictionary to Suchan and told him only that it holds consecutive natural numbers in lexicographic order. Suchan wants to work out which numbers are inside, that is, the values of and . The pages carry no page numbers, so he can only read them one at a time from the front. Once he has read the last page the cover appears, so at that moment he knows the dictionary is over.
Given and , find the smallest number of pages Suchan has to read before both and are certain.
Input
The first line contains the number of test cases (). Each of the next lines holds one test case: two integers and () separated by a space. This means Jihak built the dictionary from the natural numbers between and inclusive.
Output
For each test case, print the minimum number of pages Suchan has to read on its own line.
Hint
For and the pages come in the order 1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9. After four pages Suchan has seen 1, 10, 11, 2, and any of 12 or more would have put 12 right after 11, so is settled.
For and the order is 10, 11, 12, 13, 5, 6, 7, 8, 9.
For and the order is 3, 4, 5. Only after the third page, when the cover appears, does Suchan learn that 6 is absent.