#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n, x;
    cin >> n;
    int ans = 0;
    bool already = false;
    bool match = true;
    for (int i = 1; i <= n; i++) {
      cin >> x;
      if (x != i) {
        if (already) ans = 2;
        ans = max(ans, 1);
        match = false;
      }
      if (!already && !match && x == i) {
        already = true;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
