#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, two = 2;
  cin >> n;
  while (n--) {
    long long ans = 0;
    long long a, b, c;
    cin >> a;
    long long x[a + 1000];
    for (long long i = 1; i <= a; ++i) {
      cin >> x[i];
      if (x[i] == i) ++ans;
    }
    if (ans == a)
      cout << "0\n";
    else {
      ans = 0;
      for (long long i = a; i >= 1; --i) {
        if (x[i] != i) {
          b = i;
          break;
        }
      }
      for (long long i = 1; i <= a; ++i) {
        if (x[i] != i) {
          c = i;
          break;
        }
      }
      for (long long i = c; i <= b; ++i) {
        if (x[i] == i) ++ans;
      }
      cout << min(ans + 1, two) << "\n";
    }
  }
}
