#include <bits/stdc++.h>
using namespace std;
const int nax = 2e5 + 69;
const int M = 1e9 + 7;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    int res = 0;
    int start = 0;
    for (int i = 0; i < n; ++i) {
      start = i;
      int mn = a[i], mx = a[i], cnt = 0;
      while (i < n) {
        if (a[i] == i + 1) cnt++;
        mn = min(a[i], mn);
        mx = max(a[i], mx);
        if (mn == start + 1 && mx == i + 1) break;
        i++;
      }
      if (cnt != i - start + 1) {
        res++;
        if (cnt) res++;
      }
      cnt = 0;
    }
    cout << min(res, 2) << '\n';
  }
  return 0;
}
