Non-monotonicity

Time limit10sMemory limit128 MB

Summary
Given a permutation of 1 to n, find the longest subsequence whose elements alternate down, up, down, starting with a decrease.
Level

Medium7 of 10

Topics
Dynamic programming, Array, Binary search, Greedy
Solved
No attempts yet

Problem

There is a sequence AA of length nn made up of the nn distinct integers from 11 to nn. Among the subsequences of AA, we want to find the length of the longest sequence BB that satisfies the following condition.

B0>B1<B2>B3<⋯B_0 > B_1 < B_2 > B_3 < \cdots

That is, reading from left to right the elements of BB must alternately satisfy 'greater than, less than, greater than, less than', and the very first comparison must be 'greater than' (B0>B1B_0 > B_1). A subsequence is what remains after deleting zero or more elements from the original sequence, keeping the order of the remaining elements unchanged. A sequence consisting of a single element is also considered to satisfy the condition.

Given AA, write a program that prints the maximum possible length of BB.

Input

The first line contains the number of test cases TT (1≤T≤501 \le T \le 50).

Each test case is given on a single line in the following format.

n A[0] A[1] A[2] ... A[n-1]

Here nn is the length of the sequence (1≤n≤300001 \le n \le 30000), followed by the nn elements of AA separated by spaces. AA is a permutation containing each integer from 11 to nn exactly once.

Output

For each test case, print the maximum length of BB on its own line.

Examples1

  1. Example 1

    Input
    4
    5 1 2 3 4 5
    5 5 4 3 2 1
    5 5 1 4 2 3
    5 2 4 1 3 5
    
    Expected output
    1
    2
    5
    3