#include <bits/stdc++.h>
using namespace std;
void testcase() { return; }
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long u = 0; u < t; u++) {
    long long n;
    cin >> n;
    long long num, c = 0, t = 1;
    for (long long i = 1; i <= n; i++) {
      cin >> num;
      if (num != i && t == 1) c += 1;
      t = (num == i);
    }
    if (c > 2) c = 2;
    cout << c << "\n";
  }
  return 0;
}
