#include <bits/stdc++.h>
using namespace std;
bool compareInterval(pair<int, int> i1, pair<int, int> i2) {
  return (i1.first > i2.first);
}
long long int max(long long int a, long long int b) {
  if (a < b) return b;
  return a;
}
void solve() {
  int n;
  cin >> n;
  long long int a[n + 1];
  for (int i = 1; i <= n; ++i) cin >> a[i];
  int si = 1, ei = n;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != i) break;
    si++;
  }
  for (int i = n; i >= 1; i--) {
    if (a[i] != i) break;
    ei--;
  }
  if (si == n || ei == 0) {
    cout << 0 << endl;
    return;
  }
  for (int i = si; i <= ei; ++i) {
    if (a[i] == i) {
      cout << 2 << endl;
      return;
    }
  }
  cout << 1 << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
