#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[200000];
    for (long long i = 0; i < n; i++) cin >> a[i];
    bool sorted = true, exceed = false;
    for (long long i = 0; i < n; i++) {
      if (a[i] != i + 1) sorted = false;
    }
    if (sorted) {
      cout << 0 << endl;
      continue;
    }
    long long i = 0;
    while (i < n && a[i] == i + 1) i++;
    while (i < n && a[i] != i + 1) i++;
    while (i < n && a[i] == i + 1) i++;
    if (i == n)
      cout << 1 << endl;
    else
      cout << 2 << endl;
  }
}
