Under construction

Still being built. Things may change

Flaaffy

Time limit15sMemory limit512 MB

Summary
Given a range [L, R], find the fewest digit changes and comparisons needed to identify a hidden integer on a five-digit display.
Level

Medium7 of 10

Topics
Dynamic programming, Binary search
Solved
No attempts yet

Problem

So you want to see how Pokémon play games, eh? It is a good day for you. Flaaffy, a sheep-like electric Pokémon, just found an electronic Number Guessing Board™ and it wants to have fun with it!

The board is a five-digit electronic display that can show all integers from 0 to 99999. When Flaaffy turned it on, all five digits were initially set to 0. On its startup, the board chose a secret integer xx in the interval [L,R][L, R]. Flaaffy wants to guess this number. It can use electric shocks to operate the board in the following two ways:

  • Change a single digit on the display.
  • Ask the board whether xx is smaller, equal to, or larger than the number shown on the display.

The game ends if Flaaffy can correctly determine the hidden number.

However, each operation depletes the electricity stored by Flaaffy. Therefore, it wants to determine the hidden number with the minimum possible number of shocks. Flaaffy already figured out the optimal strategy. Can you?

Input

The first line contains a single integer tt (1≤t≤501 \le t \le 50), the number of independent testcases. Each of the following tt lines describes one testcase and contains two integers LL and RR (1≤L<R≤999991 \le L < R \le 99999).

Output

For each testcase, output a single number: the minimum number of shocks Flaaffy needs to produce in order to correctly guess the hidden number.

Hint

Here is the decision tree for the first testcase:

Each edge means either changing one digit or comparing xx with the number currently on the display. In the leaves of the tree, Flaaffy is already sure what the hidden number is.

Examples1

  1. Example 1

    Input
    3
    97 107
    12043 12045
    61 69
    
    Expected output
    6
    5
    7