CF2003B.Turtle and Piggy Are Playing a Game 2

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Turtle and Piggy Are Playing a Game 2

Turtle and Piggy are playing a game on a sequence. They are given a sequence a1,a2,,ana_1, a_2, \ldots, a_n, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.).

The game goes as follows:

  • Let the current length of the sequence be mm. If m=1m = 1, the game ends.
  • If the game does not end and it's Turtle's turn, then Turtle must choose an integer ii such that 1im11 \le i \le m - 1, set aia_i to max(ai,ai+1)\max(a_i, a_{i + 1}), and remove ai+1a_{i + 1}.
  • If the game does not end and it's Piggy's turn, then Piggy must choose an integer ii such that 1im11 \le i \le m - 1, set aia_i to min(ai,ai+1)\min(a_i, a_{i + 1}), and remove ai+1a_{i + 1}.

Turtle wants to maximize the value of a1a_1 in the end, while Piggy wants to minimize the value of a1a_1 in the end. Find the value of a1a_1 in the end if both players play optimally.

You can refer to notes for further clarification.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (2n1052 \le n \le 10^5) — the length of the sequence.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1051 \le a_i \le 10^5) — the elements of the sequence aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, output a single integer — the value of a1a_1 in the end if both players play optimally.

Note

In the first test case, initially a=[1,2]a = [1, 2]. Turtle can only choose i=1i = 1. Then he will set a1a_1 to max(a1,a2)=2\max(a_1, a_2) = 2 and remove a2a_2. The sequence aa becomes [2][2]. Then the length of the sequence becomes 11, and the game will end. The value of a1a_1 is 22, so you should output 22.

In the second test case, one of the possible game processes is as follows:

  • Initially a=[1,1,2]a = [1, 1, 2].
  • Turtle can choose i=2i = 2. Then he will set a2a_2 to max(a2,a3)=2\max(a_2, a_3) = 2 and remove a3a_3. The sequence aa will become [1,2][1, 2].
  • Piggy can choose i=1i = 1. Then he will set a1a_1 to min(a1,a2)=1\min(a_1, a_2) = 1 and remove a2a_2. The sequence aa will become [1][1].
  • The length of the sequence becomes 11, so the game will end. The value of a1a_1 will be 11 in the end.

In the fourth test case, one of the possible game processes is as follows:

  • Initially a=[3,1,2,2,3]a = [3, 1, 2, 2, 3].
  • Turtle can choose i=4i = 4. Then he will set a4a_4 to max(a4,a5)=3\max(a_4, a_5) = 3 and remove a5a_5. The sequence aa will become [3,1,2,3][3, 1, 2, 3].
  • Piggy can choose i=3i = 3. Then he will set a3a_3 to min(a3,a4)=2\min(a_3, a_4) = 2 and remove a4a_4. The sequence aa will become [3,1,2][3, 1, 2].
  • Turtle can choose i=2i = 2. Then he will set a2a_2 to max(a2,a3)=2\max(a_2, a_3) = 2 and remove a3a_3. The sequence aa will become [3,2][3, 2].
  • Piggy can choose i=1i = 1. Then he will set a1a_1 to min(a1,a2)=2\min(a_1, a_2) = 2 and remove a2a_2. The sequence aa will become [2][2].
  • The length of the sequence becomes 11, so the game will end. The value of a1a_1 will be 22 in the end.

Samples

5
2
1 2
3
1 1 2
3
1 2 3
5
3 1 2 2 3
10
10 2 5 2 7 9 2 5 10 7
2
1
2
2
7

在线编程 IDE

建议全屏模式获得最佳体验