#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
using namespace std;
const long long maxn = 2e5 + 20;
const long long inf = (long long)2e9;
const long double pi = asin(1) * 2;
const long double eps = 1e-8;
const long long mod = (long long)1e9 + 7;
const long long ns = 97;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    vector<long long> goo(n);
    for (auto& x : goo) cin >> x;
    long long cnt = 0;
    for (long long i = 0; i < n; ++i) cnt += (i + 1 == goo[i]);
    long long cnt1 = 0, cnt2 = 0;
    long long i = 0;
    while (i < n && goo[i] == i + 1) ++i, ++cnt1;
    i = n - 1;
    while (i >= 0 && goo[i] == i + 1) --i, ++cnt1;
    if (cnt == n)
      cout << 0;
    else if (!cnt || cnt1 == cnt)
      cout << 1;
    else
      cout << 2;
    cout << '\n';
  }
}
