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].

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update>
// find_by_order returns iterator to  element present at that index...
// order_of_key returns no. of elements having value strictly less than that given in the order_of_key();
#warning !!!check the size of arrayss!!!
// NUMERIC DEFINITIONS :
 
#define MOD  1000000007
#define newMOD  998244353
#define pi 3.1415926535897932384626433832795
#define ld long double
#define ll long long
#define int long long
#define INF 1000000000000000008
#define Need_for_speed(activated) ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define satisfy ll t; cin>> t; while(t--)
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define endl "\n"
#define x first
#define y second

#define pii pair<ll,ll>
#define input freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout)
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
using namespace std;
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll power(int x, unsigned int y, unsigned int m){ if (y == 0) return 1;int p = power(x, y/2, m) % m;  p = (p * p) % m;return (y%2 == 0)? p : (x * p) % m;}
ll modInverse(int a, int m){{ return power(a, m-2, m);}}
const ll maxn = 500004;
//const ll lim = ;
//ll fact[lim] = {0};
//ll inv[lim];
//void facto(ll modd) {
//
//    fact[0] = 1; fact[1] = 1;
//
//    for(int i = 2; i < lim; i++){fact[i] = (fact[i-1]*i)%modd;}
//    inv[lim-1] = modInverse(fact[lim-1],modd);
//    for(ll i=lim-2;i>=0;i--)
//        inv[i]=(inv[i+1]*(i+1))%modd;
//
//}


//ll ncr(ll n, ll r,ll modd)
//{
//
//    if(r>n)return 0;
//    ll res = 1;
//    res = fact[n];
//    res*=inv[r];
//    res%=modd;
//    res*=inv[n-r];
//    res%=modd;
////    res*=modInverse(fact[r],modd);
////    res%=modd
////    res*=modInverse(fact[n-r],modd);
//    return res;
//}
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
 
template <typename T, typename V>
void __print(const pair<T, V> &x)
{
    cerr << '{';
    __print(x.fi);
    cerr << ',';
    __print(x.se);
    cerr << '}';
}
template <typename T>
void __print(const T &x)
{
    int f = 0;
    cerr << '{';
    for (auto &i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define see(x...)                 \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define see(x...)
#endif


 //-----------------Just be aware what are u doing...!!!!!!!!-------------------------



void solve()
{
    ll n;cin>>n;
    ll a[n+1];
    ll b[n+1];
    vi v;
    for(ll i=1;i<=n;i++){cin>>a[i];b[i]=a[i];v.pb(a[i]);}
    sort(b+1,b+n+1);
    bool ok=0;
    
    for(ll i=1;i<=n;i++)
    {
        if(a[i]!=b[i])ok=1;
    }
    
    
    if(ok==0)
    {
        cout<<0<<endl;return;
    }
    
    if(a[1]==1 || a[n]==n)
    {
        cout<<1<<endl;return;
    }
    sort(v.rbegin(),v.rend());

    if(a[1]==n && a[n]==1)
    {
        cout<<3<<endl;return;
    }
    
    
    cout<<2<<endl;
    
    
}
signed main()
{
    Need_for_speed(activated);
    
    //No standings....Focus on question
    //Clear the global data strucrures!!!
    //binary lifting,make whole UP 0, dont use terminating as -1,use it as 0
    // while traversing dp,focus on for loops arrangement
    //read question statement carefully...
    // Focus on yourself->remember...
 

        satisfy
        { solve();
        }
    
   
    
    
    return 0;
}




