#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x7fffffff;
const double eps = 1e-8;
const long long mod = 1e9 + 7;
const int MAXN = 200005;
int n, a[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if ((a[i] == i && a[i - 1] != i - 1) || (a[i] != i && i == n)) ans++;
    }
    if (ans == 1)
      cout << 1 << endl;
    else if (ans == 0)
      cout << 0 << endl;
    else
      cout << 2 << endl;
  }
  return 0;
}
