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

    public class Main {

	static FastScanner scn = new FastScanner();
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] HastaLaVistaLa) {
		// Running Number Of TestCases (t)
		int t = scn.nextInt();
		while(t-- > 0)
			solve();
		out.close();
	}

	static long MOD = 998244353L;
	static long[][] dp;
	static long ans;
	static boolean[][] vis;
	public static void solve() {
		// We're Friends
		int n = scn.nextInt();
		int[] arr = scn.readIntArray(n);
		int idxO = -1, idxN = -1;
		boolean check = false;
		for(int i = 0; i < n; i++) {
			if(arr[i] == 1) idxO = i;
			if(arr[i] == n) idxN = i;
			if(arr[i] != (i + 1)) check = true;
		}
		int v = n;
		boolean flag = true;
		if(arr[0] == n && arr[n - 1] == 1) flag = false;
		if(!flag) out.println(3);
		else if(!check) out.println(0);
		else if(idxO == 0 || idxN == n - 1) out.println(1);
		else out.println(2);
	} 

	public static HashMap<Integer, List<Integer>> indexes(char[] ch, int n) {

		HashMap<Integer, List<Integer>> map = new HashMap<>();
		for(int i = 0; i < n; i++) {
			int c = ch[i] - 'a';
			if(map.containsKey(c)) {
				List<Integer> temp = map.get(c);
				temp.add(i);
				map.put(c, temp);
			}else {	
				List<Integer> list = new ArrayList<>();
				list.add(i);
				map.put(c, list);
			}
		}
		return map;
	}

	public static boolean isPrime(int p) {
		int l = (int) Math.sqrt(p);
		for(int i = 2; i <= l; i++) {
			if(p % i == 0) return false;
		}
		return true;
	}
 
	public static long binpow(long n, long m, long mod) {
	        long res = 1;
	        while (m > 0) {
	            if (m % 2 == 1) {
	                res *= n;
	                res %= mod;
	                m--;
	            }
	            n *= n;
	            n %= mod;
	            m /= 2;
	        }
	        return res;
    	}

    	// To calculate the Factorial in Combinatorics Problems
	static long[] fact;
    	public static void factorial(int n) {
    		fact = new long[n];
		Arrays.fill(fact, 1);
		for(int i = 1; i < n; i++) fact[i] *= ( ((long) i * (fact[i-1])) % MOD);
    	}

	public static HashMap<Character, Integer> CountCharFrequencies(char[] arr) {
		HashMap<Character, Integer> map = new HashMap<>();
		for (char i : arr) if (map.containsKey(i)) map.put(i, map.get(i) + 1); else map.put(i, 1);
		return map;
	}

	public static HashMap<Integer, Integer> CountIntFrequencies(int[] arr) {
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i : arr) if (map.containsKey(i)) map.put(i, map.get(i) + 1); else map.put(i, 1);
		return map;
	}

	// Number of Divisor for Range [l, r]
	public static int[] NumberOFDivisors(int l, int r) {
		int[] arr = new int[l - r + 1];
		for(int i = l; i <= r; i++) for(int j = i; j <= r; j += i) arr[j]++;
		return arr;
	}

	public static int LowerBound(int a[], int x) { // x is the target value or key

		  int l = -1,r = a.length;
		  while(l + 1 < r) {
		    int m = (l + r) >>> 1;
		    if(a[m] >= x) r = m;
		    else l = m;

		  }
		  return r;
	}

	static boolean[] prime;
	public static void sieveOfEratosthenes(int n) {

        	prime = new boolean[n+1];
	        for(int i=0;i<n;i++)
	            prime[i] = true;
	        for(int p = 2; p*p <=n; p++) {
	            if(prime[p] == true) {
	                for(int i = p*p; i <= n; i += p)
	                    prime[i] = false;
	            }
	        }
    	}

	public static int UpperBound(int[] a, int x) {// x is the key or target value

		    int l = -1,r = a.length;
		    while(l + 1 < r) {
		       int m = (l + r) >>> 1;
		       if(a[m] <= x) l = m;
		       else r = m;
		    }
		    return l + 1;
	}

    	public static long lcm(long a, long b) {
		return (a * b) / gcd(a, b);
	}

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

	public static void sort(int[] arr) {
		Random rand = new Random();
		int n = arr.length;
		for(int i = 0; i<n; i++) {
			swap(arr, i, rand.nextInt(n));
		}
		Arrays.sort(arr);
	}

	public static void swap(int[] arr, int i, int j) {
		if(i!=j) {
			arr[i] ^= arr[j];
			arr[j] ^= arr[i];
			arr[i] ^= arr[j];
		}
	}
     
	public static void sortbyColumn(int[][] arr, int col) {
		Arrays.sort(arr, new Comparator<int[]>() {
			public int compare(final int[] entry1, final int[] entry2) {
				if (entry1[col] > entry2[col])
					return 1;
				else
					return -1;
			}
		});
	}

	public static void ArraySort2D(double[][] arr, int xy) {
		// xy == 0, for sorting wrt X-Axis
		// xy == 1, for sorting wrt Y-Axis
		Arrays.sort(arr, Comparator.comparingDouble(o -> o[xy]));
	}
     
	public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readIntArray(int n) {
        	int[] a = new int[n];
        	for(int i = 0; i < n; i++) a[i] = scn.nextInt();
        	return a;
        }
        long[] readLongArray(int n) {
        	long[] a = new long[n];
        	for(int i = 0; i < n; i++) a[i] = scn.nextLong();
        	return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}