#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cassert>
#include <deque>
#include <map>
#include <queue>
#include <climits>
#include <set>
#include <stack>
#include <cmath>
#include <array>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define ll long long
#define MAX 100000
#define MAX1 10000
#define MAX2 100000
#define inf 1000000000
#define eps 1e-12
#define MOD 1000000007

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    int t;
    cin >> t;
    while (t-- > 00) {
        int n;
        cin >> n;
        int a[n + 5], i;
        bool is = true;
        for (i = 1; i <= n; ++i) {
            cin >> a[i];
            is &= a[i] == i;
        }
        if (is) {
            cout << 0 << "\n";
            continue;
        }
        cout << (a[1] == 1 or a[n] == n ? 1 : (a[1] == n and a[n] == 1) ? 3 : 2) << "\n";
    }

    return 0;
}