Under construction

Still being built. Things may change

Decorator Cubelover

Time limit1sMemory limit512 MB

Summary
Arrange n decorations on an n^3-cycle so each window of three labels is unique and the positional-value sum is minimal; report the nth digit of p-1.
Level

Hard9 of 10

Topics
Combinatorics, Math, Greedy, Number theory
Solved
No attempts yet

Problem

Kipa loves perfect cubes more than anything. Mori, who knew this well, gave Kipa a large round cake cut into n3n^3 identical pieces as a birthday present. Every piece tastes different. There is one problem: the cake is so large that to eat it you have to climb on top of the pieces!

Standing on a cake piece, Kipa has poor eyesight and can see only the current piece and the pieces on either side. Kipa first wants to taste every piece in clockwise order and place one of nn decorations, numbered 1 through nn, so that no matter where Kipa stands, the current piece can be identified. Counting exactly n3n^3 pieces by eye is hard.

Kipa starts standing on a particular piece with fatigue 0 and tentative fatigue 1. Each time Kipa skips one piece clockwise, the tentative fatigue is multiplied by nn. Also, because each piece is so wide, a piece holds only one decoration, decoration ii, and placing it adds ii times the tentative fatigue to the fatigue.

Write a program that helps Kipa: when the decorations are arranged so that the fatigue is minimized after placing all decorations and walking one full lap back to the starting position, output which decoration is placed on the pp-th piece clockwise from the starting position.

Input

The first line gives the number of test cases TT. TT is a positive integer at most 10510^5.

The next TT lines each give a positive integer nn at most 10610^6 and a positive integer pp at most n3n^3, separated by a space.

Output

For each of the TT lines, output the number of the decoration placed on the pp-th piece clockwise from the starting position.

Hint

There are 2 decorations in total, and there are 16 ways to place them so that every piece is distinguishable.

Among them, placing them in the order 2, 2, 2, 1, 2, 1, 1, 1 gives the smallest final fatigue, 278.

Examples1

  1. Example 1

    Input
    3
    2 1
    2 4
    2 6
    
    Expected output
    2
    1
    1