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.util.*;

public class Asd {

    static PrintWriter w = new PrintWriter(System.out);
    static FastScanner s = new FastScanner();
    
    public static void main(String[] args) {

       { int t = s.nextInt();
            //  int t=1;
        while (t-- > 0) {
            solve();
        }
        w.close();
       }
       
    }

    
    

    

    

    

   

   

   

    public static class Student {

        public long i1;
        public long i;public long j;public long x;public long y;
        public int value;

        // A parameterized student constructor
        public Student(long i,long j,long i1, long i2) {

            this.i = i; this.j = j;this.x=i1;
            this.y = i2;
        }

        public long geti() {
            return i;
        }
       public long getj() {
            return j;
        }
        public long getx() {
            return x;
        } public long gety() {
            return y;
        }
        
        public int getValue() {
            return value;
        }
    }

    static class StudentComparator implements Comparator<Student> {

            // on polling you will get smallest number
        @Override
        public int compare(Student s1, Student s2) {
            if (s1.i1 < s2.i1) {
                return -1;
            } else if (s1.i1 > s2.i1) {
                return 1;
            }
            return 0;
        }
    }

    /* Function to print all the permutations of the string
     static String swap(String str, int i, int j)
     {
     char ch;
     char[] array = str.toCharArray();
     ch = array[i];
     array[i] = array[j];
     array[j] = ch;
     return String.valueOf(array);
     }
    
     static void permute(String str,int low,int high)
     {
     if(low   == high)
     list.add(Long.parseLong(str));
 
     int i;
     for(i = low; i<=high; i++){
     str = swap(str,low,i);
     permute(str, low+1,high);
     str = swap(str,low,i);
     }
     }
     use  permute(str2,0,str2.length()-1); to perform combinations
     */
    
    public static void solve() {
       
        int n=s.nextInt();
       int arr[]=new int[n];
       for(int i=0;i<n;i++)arr[i]=s.nextInt();
      // long arr[]=s.readArray(n);
      int min=100;int max=-1;
      int pos1=-1;int pos2=-1;int y=-1;
       for(int i=01;i<n;i++)
       {
           if(arr[i-1]<arr[i]){}
           else
               y=2;
               
       }
       if(y==-1)
       {
           w.println("0");return;
       }
      for(int i=0;i<n;i++)
      {
          if(arr[i]==n)
          {
             
              pos2=i+1;
              
          }
          else if(arr[i]==1)
          {
             
              pos1=i+1;
              
          }
      }
      if(pos1==1||pos2==n)
      {
          w.println(1);
      }
      else if(pos1==n&&pos2==1)
      {
          w.println("3");
      }
      else
          w.println(2);
       
    }

    static void call(ArrayList<Integer> t, ArrayList<Integer> m) {
        if (t.size() == 0 && m.size() == 0) {
           
            return;
        }
        t.remove(0);
        t.remove(t.size() - 1);
        call(t, new ArrayList<Integer>(m.subList(0, m.size() - 1)));
        call(t, new ArrayList<Integer>(m.subList(1, m.size())));
    }

    static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    static int noofdivisors(int n) {
        //it counts no of divisors of every number upto number n

        int arr[] = new int[n + 1];
        for (int i = 1; i <= (n); i++) {
            for (int j = i; j <= (n); j = j + i) {
                arr[j]++;
            }
        }
        return arr[0];
    }

    static char[] reverse(char arr[]) {
        char[] b = new char[arr.length];
        int j = arr.length;
        for (int i = 0; i < arr.length; i++) {
            b[j - 1] = arr[i];
            j = j - 1;
        }
        return b;
    }

    static long factorial(int n) {
        if (n == 0) {
            return 1;
        }
        long su = 1;
        for (int i = 1; i <= n; i++) {
            su *= (long) i;
        }
        return su;
    }

    static class FastScanner {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        public String next() {
            while (!st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        long[] readArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextLong();
            }
            return a;
        }

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

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

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

}
