#include <bits/stdc++.h>
using namespace std;
template <typename T>
T BigMod(T b, T p, T m) {
  if (p == 0) return 1;
  if (p % 2 == 0) {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
  return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T>
T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return (POW(B, P / 2)) * (POW(B, P / 2));
}
template <typename T>
T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / __gcd(a, b));
}
template <typename T>
T Prime(T n) {
  for (long long i = 2; i < min(50000, n / 2); i++)
    if (n % i == 0) return 0;
  return 1;
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, x, p = 0;
    cin >> n;
    for (long long i = 0; i < n; i++) {
      cin >> x;
      x--;
      if (x != i && p == 0)
        p++;
      else if (x == i && p == 1)
        p++;
      else if (x != i && p == 2)
        p++;
    }
    if (p > 1) p--;
    cout << p << endl;
  }
}
