#include <bits/stdc++.h>
using namespace std;
vector<bool> sieve(long long n) {
  vector<bool> prime(n + 1, true);
  prime[0] = prime[1] = false;
  for (long long i = 2; i <= n; ++i) {
    if (prime[i] && (i * i) <= n)
      for (long long j = i * i; j <= n; j += i) prime[j] = false;
  }
  return prime;
}
long long power(long long a, long long b, long long m = 1000000007) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> a(n);
    for (long long i = 0; i < (long long)n; ++i) cin >> a[i];
    long long res = 0;
    for (long long i = 1; i < (long long)n + 1; ++i) {
      long long cur = a[i - 1];
      if (cur != i && res == 0) res = 1;
      if (cur == i && res == 1) res = 2;
      if (cur != i && res == 2) res = 3;
    }
    if (res == 0)
      cout << "0\n";
    else if (res == 3)
      cout << "2\n";
    else
      cout << "1\n";
  }
  return 0;
}
