#include <bits/stdc++.h>
using namespace std;
const long long int mxn = 0;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int i, j, k, n, m = -1, m1 = -1;
    cin >> n;
    vector<long long int> a(n);
    for (i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] != i + 1 && m == -1)
        m = i, m1 = i;
      else if (a[i] != i + 1)
        m1 = i;
    }
    long long int ans = 0;
    if (m != -1) {
      long long int f = 0;
      for (i = m; i <= m1; i++)
        if (a[i] == i + 1) {
          f = 1;
          break;
        }
      if (f)
        ans = 2;
      else
        ans = 1;
    }
    cout << ans << "\n";
  }
  return 0;
}
