You are given a permutation a consisting of n numbers 1, 2, ..., n (a permutation is an array in which each element from 1 to n occurs exactly once).

You can perform the following operation: choose some subarray (contiguous subsegment) of a 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] and we want to apply the operation to the subarray a[2, 4] (the subarray containing all elements from the 2-nd to the 4-th), then after the operation, the array can become a = [2, 5, 1, 4, 3] or, for example, a = [2, 1, 5, 4, 3].

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

Input

The first line contains a single integer t (1 ≤ t ≤ 2000) — the number of test cases.

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

The second line of the test case contains n distinct integers from 1 to n — the given permutation a.

Output

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

Example

Input


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


Output


1
0
2

Note

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

In the first test case of the example, you can select the subarray 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] and reorder the elements in it so a becomes [2, 1, 3, 4, 5], and then select the subarray a[1, 2] and swap the elements in it, so a becomes [1, 2, 3, 4, 5].
import java.io.*;
import java.lang.reflect.Array;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;

public class gcd {
    public static void printAllPathsUtil(Integer u, Integer d,
                                   boolean[] isVisited,
                                   List<Integer> localPathList,ArrayList<ArrayList<Integer>> arr)
    {

        if (u.equals(d)) {
            System.out.println(localPathList);
            // if match found then no need to traverse more till depth
            return;
        }

        // Mark the current node
        isVisited[u] = true;

        // Recur for all the vertices
        // adjacent to current vertex
        for (Integer i : arr.get(u)) {
            if (!isVisited[i]) {
                // store current node
                // in path[]
                localPathList.add(i);
                printAllPathsUtil(i, d, isVisited, localPathList,arr);

                // remove current node
                // in path[]
                localPathList.remove(i);
            }
        }

        // Mark the current node
        isVisited[u] = false;
    }


    static HashSet<Integer> paths=new HashSet<>();
    static boolean found=false;
    static boolean answer=false;
    public static boolean paths(ArrayList<ArrayList<Integer>> arr,int x,boolean[] visited,int n)
    {

        System.out.println(x);
        System.out.println(paths);
        visited[x]=true;
        boolean sta=false;
        for(int i:arr.get(x))
        {
            //System.out.println("hello");
            if(i==n || paths.contains(i))
            {
                found=true;
                sta=true;
                paths.add(x);

            }
            else if(!visited[i])
            {
                boolean sta1=paths(arr,i,visited,n);
                if(sta1)
                    sta=true;
            }
        }

        if(sta)
        {
            paths.add(x);
            return true;
        }
        else
            return false;

    }
    public static boolean isCyclicUtil(ArrayList<ArrayList<Integer>> arr,int i, boolean[] visited, boolean[] recStack)
    {

        // Mark the current node as visited and
        // part of recursion stack
        //System.out.println("hello");
        if (recStack[i])
            return true;

        if (visited[i])
            return false;

        visited[i] = true;

        recStack[i] = true;
        boolean sta=false;
        for(int x:arr.get(i))
        {
            boolean sta1=isCyclicUtil(arr,x,visited,recStack);
            if(sta1)
                sta=true;
        }

        if(sta)
        {
            if(paths.contains(i) && i!=0)
                answer=true;

            return true;
        }
        else

        {
            recStack[i] = false;

            return false;
        }
    }




        public static int fun(ArrayList<ArrayList<Integer>> arr, ArrayList<ArrayList<Integer>> length,ArrayList<ArrayList<Integer>> auto, int s , int d , int n,int[][] dp)
        {

            if(n<0){
                return Integer.MAX_VALUE ;
            }
            if(dp[s][n] != -1){
                return dp[s][n] ;
            }
            int ans = Integer.MAX_VALUE ;
            for(int i=0;i<arr.get(s).size();i++){
                if(arr.get(s).get(i) == d && auto.get(s).get(i)<=n){
                    ans = Math.min(ans ,length.get(s).get(i)) ;
                }
                else{
                    int y = fun(arr,length,auto , arr.get(s).get(i) , d , n-auto.get(s).get(i),dp) ;
                    if(y == Integer.MAX_VALUE){
                        continue ;
                    }
                    ans = Math.min(ans , length.get(s).get(i)+ y);
                }
            }

            return dp[s][n] = ans ;
        }

        public static void spiral(ArrayList<Integer> arr,boolean status,int cur_row,int cur_column,int counter,boolean[][] visited,int n,int m,int[][] matrix)
        {

            int imp=-1;
            //System.out.println(cur_row+" "+cur_column+" "+counter);
            if(status)
            {
                if(counter%2== 0)
                {
                    for(int i=cur_column;i<m;i++)
                    {

                            //System.out.println(i+" "+matrix[c1][i]);
                            if(!visited[cur_row][i])
                            {
                                arr.add(matrix[cur_row][i]);
                                visited[cur_row][i] = true;
                                imp=i;
                            }

                    }
                    if(imp!=-1)
                    spiral(arr,false,cur_row+1,imp,counter,visited,n,m,matrix);
                    else
                        return;
                }

                else
                {

                    for(int i=cur_column;i>=0;i--)
                    {

                            //System.out.println(i+" "+matrix[c1][i]);
                            if(!visited[cur_row][i])
                            {
                                //System.out.println(i);
                                arr.add(matrix[cur_row][i]);
                                visited[cur_row][i] = true;

                                imp=i;
                            }

                    }

                    if(imp!=-1)
                        spiral(arr,false,cur_row-1,imp,counter,visited,n,m,matrix);
                    else
                        return;
                }


            }

            else
            {
                if(counter%2== 0)
                {
                    for(int i=cur_row;i<n;i++)
                    {

                            //System.out.println(i+" "+matrix[c1][i]);
                            if(!visited[i][cur_column])
                            {
                                arr.add(matrix[i][cur_column]);
                                visited[i][cur_column] = true;

                                imp=i;
                            }

                    }
                    if(imp!=-1)
                    {
                        ++counter;
                        spiral(arr,true,imp,cur_column-1,counter,visited,n,m,matrix);
                    }
                    else
                        return;
                }

                else
                {
                    for(int i=cur_row;i>=0;i--)
                    {

                            //System.out.println(i+" "+matrix[c1][i]);
                            if(!visited[i][cur_column])
                            {
                                arr.add(matrix[i][cur_column]);
                                visited[i][cur_column] = true;

                                imp=i;
                            }

                    }
                    if(imp!=-1)
                    {
                        ++counter;
                        spiral(arr,true,imp,cur_column+1,counter,visited,n,m,matrix);
                    }
                    else
                        return;
                }
            }
        }
    public static int days(long[] sum,int M,long b,long time)
    {
        if(M*b+time>sum[M])
            return 0;
        int l=1;
        int r=M;
        while(l<=r)
        {
            int mid=(l+r)/2;
            //System.out.println(mid);
            long check=(long)mid*b+time;
            if(check<=sum[mid])
            {
                //System.out.println("hello "+sum[mid-2]);
                if(mid>1 && (((long)mid-1)*b+time)<=sum[mid-1])
                    r=mid-1;
                else
                    return mid;
            }
            else if(check>sum[mid])
                l=mid+1;
        }
        return 0;

    }



    public static void main(String[] args)throws IOException
    {
        Reader.init(System.in);
        BufferedWriter output=new BufferedWriter(new OutputStreamWriter(System.out));
        int T=Reader.nextInt();
        for(int k=1;k<=T;k++)
        {
            int N=Reader.nextInt();
            int[] arr=new int[N];
            int[] answer=new int[N];
            for(int j=0;j<N;j++)
            {
                arr[j]=Reader.nextInt();
                answer[j]=arr[j];
            }
            Arrays.sort(answer);
            boolean status=true;
            for(int i=0;i<N;i++)
            {
                if(answer[i]!=arr[i])
                    status=false;
            }
            if(status)
            {
                output.write(0+"\n");
            }
            else
            {
                if(arr[0]==answer[0] || arr[N-1]==answer[N-1])
                    output.write(1+"\n");
                else if(arr[0]==answer[N-1] && arr[N-1]==answer[0])
                    output.write(3+"\n");
                else
                    output.write(2+"\n");

            }








        }


        output.flush();
    }





}

class level
{
    long height;
    int index;
    level(long h,int i)
    {
        height=h;
        index=i;
    }
}


class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}






