Folding Sheets

Given a number X or a position P, determine its counterpart in the sequence produced by folding an N x N sheet in half alternating bottom-over-top and right-over-left until 1 x 1, then reading the resulting column bottom to top. N is 2^K, K up to 31, and there are up to 10000 queries.

Medium7RecursionDivide and conquerImplementationMathNo attempts yetTime limit1sMemory limit32 MB

Problem

You have a sheet of grid paper of size N×NN \times N squares. NN is a power of two.

The squares are numbered from 11 to N2N^2, the top row first and left to right inside each row. Each square holds only its own number.

The sheet is folded in half over and over. The bottom half is folded over the top half first, then the right half over the left half, and the two moves alternate in that order until the folded sheet is 1×11 \times 1.

The squares then form a column of size 1×1×N21 \times 1 \times N^2. Reading that column from its bottom square to its top square gives the sequence S=S1,S2,,SP,,SN2S = \langle S_1, S_2, \ldots, S_P, \ldots, S_{N^2} \rangle, where PP is the position in the sequence.

A developer sitting through a dull meeting folds sheets into such columns and wants answers to two questions.

  • Question type 1. A number XX is given. At which position PP of the sequence SS does it sit?
  • Question type 2. A position PP of the sequence SS is given. Which number XX sits there?

Write a program that computes the number XX or the position PP, depending on the type of the question.

Input

The first line contains the number of questions QQ. Each of the next QQ lines contains three integers TT, KK, VV, separated by a single space.

  • TT is the type of the question. T=1T = 1 or T=2T = 2.
  • KK is the exponent that fixes the size of the sheet. The sheet is N=2KN = 2^K squares wide.
  • VV is the number XX when T=1T = 1, and the position PP when T=2T = 2.

Output

Print QQ lines, one per question. Each line holds a single integer, the position PP or the number XX, depending on the type of the question.

Constraints

  • 1Q100001 \le Q \le 10000
  • 1T21 \le T \le 2
  • 0K310 \le K \le 31
  • 1X,PN21 \le X, P \le N^2, where N=2KN = 2^K

Notes

For N=2N = 2, reading the column from bottom to top gives S=1,3,4,2S = \langle 1, 3, 4, 2 \rangle.

For N=4N = 4, S=1,13,16,4,8,12,9,5,6,10,11,7,3,15,14,2S = \langle 1, 13, 16, 4, 8, 12, 9, 5, 6, 10, 11, 7, 3, 15, 14, 2 \rangle.