CF1979A.Guess the Maximum

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

Guess the Maximum

Alice and Bob came up with a rather strange game. They have an array of integers a1,a2,,ana_1, a_2,\ldots, a_n. Alice chooses a certain integer kk and tells it to Bob, then the following happens:

  • Bob chooses two integers ii and jj (1i<jn1 \le i \lt j \le n), and then finds the maximum among the integers ai,ai+1,,aja_i, a_{i + 1},\ldots, a_j;
  • If the obtained maximum is strictly greater than kk, Alice wins, otherwise Bob wins.

Help Alice find the maximum kk at which she is guaranteed to win.

Input

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

The first line of each test case contains a single integer nn (2n51042 \le n \le 5 \cdot 10^4) — the number of elements in the array.

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

It is guaranteed that the sum of nn over all test cases does not exceed 51045 \cdot 10^4.

Output

For each test case, output one integer — the maximum integer kk at which Alice is guaranteed to win.

Note

In the first test case, all possible subsegments that Bob can choose look as follows: $[2, 4], [2, 4, 1], [2, 4, 1, 7], [4, 1], [4, 1, 7], [1, 7]$. The maximums on the subsegments are respectively equal to 4,4,7,4,7,74, 4, 7, 4, 7, 7. It can be shown that 33 is the largest integer such that any of the maximums will be strictly greater than it.

In the third test case, the only segment that Bob can choose is [1,1][1, 1]. So the answer is 00.

Samples

6
4
2 4 1 7
5
1 2 3 4 5
2
1 1
3
37 8 16
5
10 10 10 10 9
10
3 12 9 5 2 3 2 9 8 2
3
1
0
15
9
2

在线编程 IDE

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