/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

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

#define ll long long

int main()
{
    int t; cin >> t;
    
    while (t--){
        int n; cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        int check = 1;
        for (int i = 0; i < n - 1; i++) if (a[i] > a[i + 1]) check = 0;
        if (check) cout << 0 << endl;
        else if (a[0] == 1 || a[n - 1] == n) cout << 1 << endl;
        else if (a[0] == n && a[n - 1] == 1) cout << 3 << endl;
        else cout << 2 << endl;
    }

    return 0;
}
