#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long numofqueries;
  cin >> numofqueries;
  for (; numofqueries--;) {
    long long n;
    cin >> n;
    long long a[n + 1], correct = 0, cons = 1, f = 0, b = 0;
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
      if (a[i] == i) correct++;
    }
    for (long long i = 1; i <= n; i++) {
      if (a[i] == i)
        f++;
      else
        break;
    }
    for (long long i = n; i >= 1; i--) {
      if (a[i] == i)
        b++;
      else
        break;
    }
    if (correct == n)
      cout << 0;
    else if (f + b == correct)
      cout << 1;
    else
      cout << 2;
    cout << "\n";
  }
  return (0);
}
