#P1975A. Bazoka and Mocha's Array

    ID: 10556 Type: RemoteJudge 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>brute forcegreedyimplementationsortings*800

Bazoka and Mocha's Array

Description

Mocha likes arrays, so before her departure, Bazoka gave her an array aa consisting of nn positive integers as a gift.

Now Mocha wants to know whether array aa could become sorted in non-decreasing order after performing the following operation some (possibly, zero) times:

  • Split the array into two parts — a prefix and a suffix, then swap these two parts. In other words, let a=x+ya=x+y. Then, we can set a:=y+xa:= y+x. Here ++ denotes the array concatenation operation.

For example, if a=[3,1,4,1,5]a=[3,1,4,1,5], we can choose x=[3,1]x=[3,1] and y=[4,1,5]y=[4,1,5], satisfying a=x+ya=x+y. Then, we can set a:=y+x=[4,1,5,3,1]a:= y + x = [4,1,5,3,1]. We can also choose x=[3,1,4,1,5]x=[3,1,4,1,5] and y=[]y=[\,], satisfying a=x+ya=x+y. Then, we can set a:=y+x=[3,1,4,1,5]a := y+x = [3,1,4,1,5]. Note that we are not allowed to choose x=[3,1,1]x=[3,1,1] and y=[4,5]y=[4,5], neither are we allowed to choose x=[1,3]x=[1,3] and y=[5,1,4]y=[5,1,4], as both these choices do not satisfy a=x+ya=x+y.

Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001\leq t\leq 1000). The description of the test cases follows.

The first line of each test case contains a single integer nn (2n502\leq n\leq 50) — the length of the array aa.

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

For each test case, output "Yes" if aa could become non-decreasing after performing the operation any number of times, and output "No" if not.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001\leq t\leq 1000). The description of the test cases follows.

The first line of each test case contains a single integer nn (2n502\leq n\leq 50) — the length of the array aa.

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

Output

For each test case, output "Yes" if aa could become non-decreasing after performing the operation any number of times, and output "No" if not.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Sample Input 1

3
6
1 1 4 5 1 4
5
7 9 2 2 3
3
1 2 3

Sample Output 1

No
Yes
Yes

Note

In the first test case, it can be proven that aa cannot become non-decreasing after performing the operation any number of times.

In the second test case, we can perform the following operations to make aa sorted in non-decreasing order:

  • Split the array into two parts: x=[7]x=[7] and y=[9,2,2,3]y=[9,2,2,3], then swap these two parts. The array will become y+x=[9,2,2,3,7]y+x = [9,2,2,3,7].
  • Split the array into two parts: x=[9]x=[9] and y=[2,2,3,7]y=[2,2,3,7], then swap these two parts. The array will become y+x=[2,2,3,7,9]y+x=[2,2,3,7,9], which is non-decreasing.