#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
        int T, n, a1, an,sorted, x0,x1;
        cin >> T;
        while(T--)
        {
            cin >> n;
            sorted = 1;
            x0 = 0;
            for(int i = 0; i < n; i++)
            {
                cin >> x1;
                if(i == 0)
                    a1 = x1;
                else if(i == n-1)
                    an = x1;
                if (x1 < x0)
                    sorted = 0;
                x0 = x1;
            }   
            if (sorted)
            {
                printf("0\n");
            }
            else if(a1 == 1 || an == n)
            {
                printf("1\n");
            }
            else if(a1 != n || an != 1)
            {
                printf("2\n");
            }
            else
            {
                printf("3\n");
            }
        }
        return 0;
}