#P1525B. Permutation Sort

    ID: 2193 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>constructive algorithmsgreedy*900

Permutation Sort

Description

You are given a permutation aa consisting of nn numbers 11, 22, ..., nn (a permutation is an array in which each element from 11 to nn occurs exactly once).

You can perform the following operation: choose some subarray (contiguous subsegment) of aa and rearrange the elements in it in any way you want. But this operation cannot be applied to the whole array.

For example, if a=[2,1,4,5,3]a = [2, 1, 4, 5, 3] and we want to apply the operation to the subarray a[2,4]a[2, 4] (the subarray containing all elements from the 22-nd to the 44-th), then after the operation, the array can become a=[2,5,1,4,3]a = [2, 5, 1, 4, 3] or, for example, a=[2,1,5,4,3]a = [2, 1, 5, 4, 3].

Your task is to calculate the minimum number of operations described above to sort the permutation aa in ascending order.

The first line contains a single integer tt (1t20001 \le t \le 2000) — the number of test cases.

The first line of the test case contains a single integer nn (3n503 \le n \le 50) — the number of elements in the permutation.

The second line of the test case contains nn distinct integers from 11 to nn — the given permutation aa.

For each test case, output a single integer — the minimum number of operations described above to sort the array aa in ascending order.

Input

The first line contains a single integer tt (1t20001 \le t \le 2000) — the number of test cases.

The first line of the test case contains a single integer nn (3n503 \le n \le 50) — the number of elements in the permutation.

The second line of the test case contains nn distinct integers from 11 to nn — the given permutation aa.

Output

For each test case, output a single integer — the minimum number of operations described above to sort the array aa in ascending order.

Sample Input 1

3
4
1 3 2 4
3
1 2 3
5
2 1 4 5 3

Sample Output 1

1
0
2

Note

In the explanations, a[i,j]a[i, j] defines the subarray of aa that starts from the ii-th element and ends with the jj-th element.

In the first test case of the example, you can select the subarray a[2,3]a[2, 3] and swap the elements in it.

In the second test case of the example, the permutation is already sorted, so you don't need to apply any operations.

In the third test case of the example, you can select the subarray a[3,5]a[3, 5] and reorder the elements in it so aa becomes [2,1,3,4,5][2, 1, 3, 4, 5], and then select the subarray a[1,2]a[1, 2] and swap the elements in it, so aa becomes [1,2,3,4,5][1, 2, 3, 4, 5].