#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int t;
  cin >> t;
  for (long long int i = 0; i < t; i++) {
    long long int n;
    cin >> n;
    long long int cnt = 0;
    long long int arr[n + 1];
    vector<long long int> v;
    for (long long int i = 1; i <= n; i++) cin >> arr[i];
    for (long long int i = 1; i <= n; i++) {
      if (arr[i] != i) {
        cnt++;
        v.push_back(i);
      }
    }
    if (cnt == 0)
      cout << 0 << endl;
    else {
      long long int start = v[0];
      long long int p = v.size();
      long long int end = v[p - 1];
      long long int num = end - start + 1;
      if (num == p)
        cout << 1 << endl;
      else
        cout << 2 << endl;
    }
  }
}
