This page is still under construction.

Parts of this page are still being built. What you see may change.

Dictionary Survey

Time limit3sMemory limit256 MB

Summary
Find how many leading pages of the integers from A to B in lexicographic order pin down both A and B.
Level

Hard9 of 10

Topics
Trie, Math, String
Solved
No attempts yet

Problem

Jihak wrote every natural number from AA to BB into a dictionary, one number per page, so the dictionary has B−A+1B - A + 1 pages. The numbers are not ordered by value, they are ordered lexicographically, which means their decimal spellings are compared character by character. For A=4A = 4 and B=12B = 12 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 AA and BB. 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 AA and BB, find the smallest number of pages Suchan has to read before both AA and BB are certain.

Input

The first line contains the number of test cases TT (1≤T≤1051 \le T \le 10^5). Each of the next TT lines holds one test case: two integers AA and BB (1≤A≤B≤10181 \le A \le B \le 10^{18}) separated by a space. This means Jihak built the dictionary from the natural numbers between AA and BB inclusive.

Output

For each test case, print the minimum number of pages Suchan has to read on its own line.

Hint

For A=1A = 1 and B=11B = 11 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 BB of 12 or more would have put 12 right after 11, so B=11B = 11 is settled.

For A=5A = 5 and B=13B = 13 the order is 10, 11, 12, 13, 5, 6, 7, 8, 9.

For A=3A = 3 and B=5B = 5 the order is 3, 4, 5. Only after the third page, when the cover appears, does Suchan learn that 6 is absent.

Examples2

  1. Example 1

    Input
    3
    1 11
    5 13
    3 5
    
    Expected output
    4
    5
    3
    
  2. Example 2

    Input
    4
    1 1
    7 7
    10 10
    1000000000000000000 1000000000000000000
    
    Expected output
    1
    1
    1
    1