#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  cin >> T;
  while (T--) {
    int N, region = 0;
    cin >> N;
    long long int a[N];
    string s;
    for (int(i) = 0; (i) < (N); (i)++) {
      cin >> a[i];
      if (a[i] == i + 1)
        s += "T";
      else
        s += "F";
    }
    for (int(i) = 0; (i) < (s.size()); (i)++) {
      if (s[i] == 'F' && s[i + 1] == 'T') region++;
      if (s[i] == 'F' && i == (s.size() - 1)) region++;
    }
    cout << min(region, 2) << endl;
  }
  return 0;
}
