#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;


int main()
{
    int t = 0;

    cin>>t;
    for(int i = 0; i < t; i++)
    {
        int n = 0;
        cin>>n;
        int v[100] = {0};
        for(int j = 0; j < n; j++) cin>>v[j];

        if(v[0] == n && v[n-1] == 1)
        {
            cout<<3<<'\n';

        }
        else if(v[0] != 1 && v[n-1] != n)
        {
            cout<<2<<'\n';
        }
        else if(v[0] == 1 && v[n-1] != n)
        {
            cout<<1<<'\n';
        }
        else if(v[0] != 1 && v[n-1] == n)
        {
            cout<<1<<'\n';
        }
        else
        {
            bool d = false;
            for(int j = 0; j < n; j++)
            {
                if(v[j] != j+1)
                {
                    d = true;
                    break;
                }
            }
            if(!d) cout<<0<<' ';
            else cout<<1<<' ';
        }
    }

    return 0;
}
