#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  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 f = 0, l = 0, total = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == i + 1)
        f++;
      else {
        break;
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (a[i] == i + 1)
        l++;
      else {
        break;
      }
    }
    for (int i = 0; i < n; i++) {
      if (a[i] == i + 1) total++;
    }
    if (f == n)
      cout << 0 << "\n";
    else if (l + f == total)
      cout << 1 << "\n";
    else {
      cout << 2 << "\n";
    }
  }
}
