#include <bits/stdc++.h>

using namespace std;

#define N 55
#define P 998244353

#define pb push_back
#define mp make_pair

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

int Min(int a, int b)
{
    if(a < b)
        return a;
    return b;
}

int Max(int a, int b)
{
    return a + b - Min(a, b);
}

int L[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, t, i;
    cin >> t;
    while(t > 0)
    {
        cin >> n;
        for(i = 0; i < n; i++)
            cin >> L[i];
        int p = 1;
        for(i = 0; i < n; i++)
        {
            if(L[i] != i + 1)
            {
                p = 0;
                break;
            }
        }
        if(p == 1)
            cout << 0 << endl;
        else
        {
            if(L[0] == 1 || L[n - 1] == n)
                cout << 1 << endl;
            else if(L[0] == n && L[n - 1] == 1)
                cout << 3 << endl;
            else
                cout << 2 << endl;
        }
        t -= 1;
    }

    return 0;
}
