﻿//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector> 
#include <set>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <iomanip>
#include <queue>
using namespace std;
typedef long long ll;
typedef int itn;
//min({a,b,c,d});
int a[100], b[100];
int main()
{
    ll t;
    cin >> t;
    int n, mx, nm;
    for (int _ = 0; _ < t; _++)
    {
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            b[i] = a[i];
            if (a[i] == 1)
                nm = i;
            if (a[i] == n)
                mx = i;
        }
        if (nm == 0 && mx == n - 1)
        {
            sort(b, b + n);
            bool x = true;
            for (int i = 0; i < n && x; i++)
            {
                if (a[i] != b[i])
                    x = false;
            }
            if (x)
            {
                cout << 0 << endl;
            }
            else
            {
                cout << 1 << endl;
            }
        }
        else if (nm == 0 || mx == n - 1)
        {
            cout << 1 << endl;
        }
        else if (nm == n - 1 && mx == 0)
        {
            cout << 3 << endl;
        }
        else
        {
            cout << 2 << endl;
        }
    }
    return 0;
}