Recursive Pattern (Szlaczek)

Interview

Time limit1sMemory limit128 MB

Summary
Given a starting sequence that is repeatedly extended by appending its own reversal, report the value at position M.
Level

Medium4 of 10

Topics
Recursion, Array, Math
Solved
No attempts yet

Problem

Kornelia likes to draw “recursive patterns” (szlaczki) made of natural numbers. A pattern starts from an arbitrary sequence of natural numbers, for example:

1 2 1 1 1

She then appends, right after the pattern written so far, a copy of that whole pattern written backwards. Each such step doubles the length of the pattern. For the example above, after one step the pattern becomes (the appended part is the original pattern reversed):

1 2 1 1 1 1 1 1 2 1

After one more step it becomes:

1 2 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 2 1

The pattern can be extended forever in this way.

Given the sequence Kornelia started from, determine which number ends up at a given position of the pattern.

Input

The first line contains the number of test cases ZZ (1≤Z≤101 \le Z \le 10). The test cases follow.

The first line of each test case contains two natural numbers NN and MM (1≤N≤1061 \le N \le 10^6, 0≤M≤1090 \le M \le 10^9): the length of the starting sequence and the position in the resulting pattern.

The second line contains the NN natural numbers c1,c2,…,cNc_1, c_2, \dots, c_N (1≤ci≤1061 \le c_i \le 10^6) of the starting sequence, separated by spaces.

Output

For each test case, print on its own line the number located at position MM of the pattern. Positions are numbered starting from 00.

Examples1

  1. Example 1

    Input
    3
    5 0
    1 2 1 1 1
    5 1
    1 2 1 1 1
    5 8
    1 2 1 1 1
    
    Expected output
    1
    2
    2