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].
  //  Copyright © 2021 Mihir Chawla. All rights reserved.
#include <bits/stdc++.h>
#include <string>
#include <map>
#include <set>
#include <sstream> 
#include <queue>
#include <stack>
#include <deque>
#include <vector>
 
#define loop(i,a,b) for (int i = (a); i <= (b); i++)
#define loopr(i, a, b) for(int i = (a); i >= (b); i--)
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define pf 				push_front
#define mp              make_pair
#define mt              make_tuple
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(x) (int)x.size()
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define mod 1000000007
#define mod1 998244353
using namespace std;
 
int fast_pow(int a, int p) {
  int res = 1;
  while (p) {
    if (p % 2 == 0) {
      a = a * 1LL * a % mod1;
      p /= 2;
    } else {
      res = res * 1LL * a % mod1;
      p--;
    }
  }
  return res;
}
 
int fact(int n) {
  int res = 1;
  for (int i = 1; i <= n; i++) {
    res = res * 1LL * i % mod;
  }
  return res;
}
 
int C(int n, int k) {
  return fact(n) * 1LL * fast_pow(fact(k), mod - 2) % mod * 1LL * fast_pow(fact(n - k), mod - 2) % mod;
}
 
vector<int> sieveOfEratosthenes(int n) {
	vector<int> primes;
	vector<bool> prime(n+1,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;
		}
	}
	for (int p = 2; p <= n; p++)
		if (prime[p])
			primes.push_back(p);
	return primes;
}
 
 
bool isPrime(int n) 
{ 
    // Corner cases 
    if (n <= 1)  return false; 
    if (n <= 3)  return true; 
  
    // This is checked so that we can skip  
    // middle five numbers in below loop 
    if (n%2 == 0 || n%3 == 0) return false; 
  
    for (int i=5; i*i<=n; i=i+6) 
        if (n%i == 0 || n%(i+2) == 0) 
           return false; 
  
    return true; 
} 

bool isPerfectSquare(long double x)
{
    // Find floating point value of
    // square root of x.
    if (x >= 0) {
 
        long long sr = sqrt(x);
         
        // if product of square root 
        //is equal, then
        // return T/F
        return (sr * sr == x);
    }
    // else return false if n<0
    return false;
} 

int sum(int n)
{
	int res = 0;
	while(n>0)
	{
		res+=n%10;
		n = n/10;
	}
	return res;
}

vector<int> adj[10001];
vector<int> vis(10001);
vector<int> col(10001);
void dfs(int v)
{
    vis[v] = 1;
    for(auto ch : adj[v])
    {
        if(vis[ch]==0)
        {
            dfs(ch);
        }
    }
}

bool bptdfs(int v,int c)
{
	vis[v] = 1;
	col[v] = c;
	for(auto ch : adj[v])
	{
		if(vis[ch]==0)
		{
			if(bptdfs(ch,c^1)==false)
			{
				return false;
			}
		}
		else
		{
			if(col[v]==col[ch])
			{
				return false;
			}
		}
	}
	return true;
}

void SieveOfEratosthenes(int n, bool prime[],
                         bool primesquare[], int a[])
{
    // Create a boolean array "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    for (int i = 2; i <= n; i++)
        prime[i] = true;
 
    // Create a boolean array "primesquare[0..n*n+1]"
    // and initialize all entries it as false. A value
    // in squareprime[i] will finally be true if i is
    // square of prime, else false.
    for (int i = 0; i <= (n * n + 1); i++)
        primesquare[i] = false;
 
    // 1 is not a prime number
    prime[1] = false;
 
    for (int p = 2; p * p <= n; p++) {
        // If prime[p] is not changed, then
        // it is a prime
        if (prime[p] == true) {
            // Update all multiples of p
            for (int i = p * 2; i <= n; i += p)
                prime[i] = false;
        }
    }
 
    int j = 0;
    for (int p = 2; p <= n; p++) {
        if (prime[p]) {
            // Storing primes in an array
            a[j] = p;
 
            // Update value in primesquare[p*p],
            // if p is prime.
            primesquare[p * p] = true;
            j++;
        }
    }
}
 
// Function to count divisors
int countDivisors(int n)
{
    // If number is 1, then it will have only 1
    // as a factor. So, total factors will be 1.
    if (n == 1)
        return 1;
 
    bool prime[n + 1], primesquare[n * n + 1];
 
    int a[n]; // for storing primes upto n
 
    // Calling SieveOfEratosthenes to store prime
    // factors of n and to store square of prime
    // factors of n
    SieveOfEratosthenes(n, prime, primesquare, a);
 
    // ans will contain total number of distinct
    // divisors
    int ans = 1;
 
    for (int i = 0;; i++) {
        if (a[i] * a[i] * a[i] > n)
            break;
 
        int cnt = 1; // cnt is power of prime a[i] in n.
        while (n % a[i] == 0) // if a[i] is a factor of n
        {
            n = n / a[i];
            cnt = cnt + 1; // incrementing power
        }
 
        ans = ans * cnt;
    }
 
    if (prime[n])
        ans = ans * 2;
 
    else if (primesquare[n])
        ans = ans * 3;
    else if (n != 1)
        ans = ans * 4;
 
    return ans; 
}


int32_t main(){
    int t = 1;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vi a(n);
        loop(i,0,n-1)
        {
            cin>>a[i];
        }
        int ans = 1;
        if(is_sorted(a.begin(),a.end()))
        {
            cout<<"0"<<endl;
            continue;
        }
        if(a[0]==1 or a[n-1]==n)
        {
            cout<<"1"<<endl;
        }
        else if(a[0]==n and a[n-1]==1)
        {
            cout<<"3"<<endl;
        }
        else
        {
            cout<<"2"<<endl;
        }
    }
}