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

#define f first
#define s second

using ll = long long;

const int mxN = 200000;
const int lgN = 18;
const int mod = 1e9 + 7;
const ll inf = 1e15;

void solve() {
        int n; cin >> n;
        int a[n + 1];
        int check = true;
        for (int i = 1; i <= n; i++) {
                cin >> a[i];
                if (i > 1 && a[i] < a[i - 1]) check = false;
        }
        
        if (check) cout << "0";
        else if (a[1] == 1 || a[n] == n) cout << "1";
        else if (a[1] != n || a[n] != 1) cout << "2";
        else cout << "3";
}

int main() {
        /*if (fopen("input.txt", "r")) {
                freopen("input.txt", "r", stdin);
                freopen("output.txt", "w", stdout);
        }*/

        ios_base::sync_with_stdio(0); cin.tie(0);

        int n_test = 1; cin >> n_test;
        for (int test = 1; test <= n_test; test++) {
                //cout << "Case #" << test << ": ";
                solve(); cout << "\n";
        }

        return 0;
}
