This page is still under construction.

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

Brunch Book

Time limit1sMemory limit1024 MB

Summary
Given N and K, find the K-th string in the natural (numeric-aware, case-folded) ordering of all N-character hex strings.
Level

Medium7 of 10

Topics
String, Math, Combinatorics, Implementation
Solved
No attempts yet

Problem

You can make anything by writing. - C.S.Lewis

Good writing influences many people, and its value remains intact even when read again years later. "Brunch", operated by Kakao, is a service made for everyone who wants to write well.

Brunch lets you write, edit, and read on every device, so you can record ideas anytime, anywhere, and read beautifully anywhere. You can group the pieces you write by topic or together with other writers who share your interests into a "magazine" and serialize them. By carefully polishing the ideas laid out in a magazine into a "Brunch Book", you can create your own original work, and the Brunch team supports the process of that work being reborn as a book, a lecture, or a new derivative work.

Jongseo, a poet who captivates everyone with both strong programming skills and writing that moves the heart, decided to post his poems on Brunch in line with the latest trend. After reading the writing, Heewon thought it would be good for Jongseo to publish a poetry collection as a Brunch Book, and selected TT poems. While working on the Brunch Book, Jongseo felt that Heewon had chosen unexpected poems, so they talked it over and found that the title sorting method Heewon's environment used after downloading the poems differed from Jongseo's.

For example, suppose the bb-th poem of the aa-th book has a title of the form BookaaPoembb. The 1010-th poem of the 22-nd book gets the title Book2Poem10, and the 11-st poem of the 1010-th book gets the title Book10Poem1. The ordinary string comparison used in programming, which Jongseo used, compares from the front and, when differing characters appear, decides the comparison by those characters. Here, at the 55-th character, where the first difference appears, 2 is greater than 1, so Book2Poem10 is greater than Book10Poem1. But if you know that each poem belongs to the 22-nd and 1010-th books, it would be natural to compare so that the poem of the 1010-th book is greater.

So Heewon sorted the book titles using the natural string comparison widely used elsewhere. In that string comparison, first all uppercase letters are changed to lowercase. Then consecutive digits are grouped and treated as a number for comparison. Scanning characters from the front, if both sides are digits, the numbers containing those digits are compared; otherwise the two characters are compared. For numbers starting with 0, if the two numbers have the same value, the string with more 0s is smaller. The following is an example of comparing titles using that string comparison: 005a << 05ab << 5abc << 5bcd << 006a << 6abc << 10ab << abcd.

On day XX, Jongseo wrote 16X16^X poems, and each poem's title used, without repetition, all XX-character strings whose characters each come from 0123456789abcdef. Heewon said that among the TT poems, the ii-th poem she liked was the KiK_i-th poem written on day NiN_i. What she meant was the KiK_i-th poem when all 16Ni16^{N_i} titles from day NiN_i are sorted by natural string comparison, so there were cases where this pointed to a different book than the ordinary string comparison Jongseo used in programming. Jongseo would normally have written a program himself to find the poem titles, but since he wanted to finish the Brunch Book quickly, he asked you to tell him the title of each poem Heewon selected.

Input

The input is given as follows.

TT

N1N_1 K1K_1

…\dots

NTN_T KTK_T

  • TT is the number of poems Heewon selected. (1≤T≤1 0001 \le T \le 1\,000)
  • NiN_i and KiK_i describe the poem Heewon selected. They mean that the ii-th selected poem is the KiK_i-th poem when all 16Ni16^{N_i} poems from day NiN_i are sorted by natural string comparison. (1≤Ni≤151 \le N_i \le 15, 1≤Ki≤16Ni1 \le K_i \le 16^{N_i})
  • All numbers in the input are integers.

Output

Output NN lines in total. On the ii-th line, output the title of the ii-th poem Heewon selected.

Examples1

  1. Example 1

    Input
    9
    1 1
    10 9809476352
    11 178182069613
    11 13935560805565
    15 768614336404564651
    15 910344248188228555
    15 1003434262550842233
    15 1003468668299564026
    15 1152921504606846976
    
    Expected output
    0
    0be5e9ee5e
    0c7e7add1c7
    caca0c0ffee
    aaaaaaaaaaaaaaa
    ca5cadeda7aba5e
    decea5ed0ffbea7
    ded1ca7ed9ad9e7
    fffffffffffffff