Under construction

Still being built. Things may change

Hidden Digits

Time limit5sMemory limit1024 MB

Summary
Given digits d_0 to d_{n-1}, find the smallest positive x such that x+i contains digit d_i for every index i.
Level

Medium7 of 10

Topics
Implementation, Math, Greedy
Solved
No attempts yet

Statement

You are given a sequence of nn digits d0d_0, d1d_1, \dots, dn−1d_{n-1}. Find the minimum positive integer xx such that for all 0≤i<n0 \le i < n, the decimal representation of x+ix + i contains the digit did_i.

Input

The input contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). The test cases follow. The first line of each test case contains a single integer nn (1≤n≤1061 \le n \le 10^6). The second line contains a string of nn digits d0d1…dn−1d_0 d_1 \ldots d_{n-1} (0≤di≤90 \le d_i \le 9). The sum of nn over all test cases does not exceed 10610^6.

Output

For each test case, print a single integer xx, the smallest positive integer such that the decimal representation of x+ix+i contains the digit did_i for all 0≤i<n0 \le i < n.

Examples1

  1. Example 1

    Input
    6
    5
    12345
    5
    01234
    3
    239
    9
    998244353
    10
    1000000007
    20
    18446744073709551616
    
    Expected output
    1
    10
    92
    45296
    701
    10367486