#include <bits/stdc++.h>
using namespace std;
inline long long int sbt(long long int x) { return __builtin_popcount(x); }
inline long long int mul(long long int a, long long int b,
                         long long int m = 1000000007) {
  return (a * b) % m;
}
inline long long int add(long long int a, long long int b,
                         long long int m = 1000000007) {
  return (a + b) % m;
}
inline long long int sub(long long int a, long long int b,
                         long long int m = 1000000007) {
  return (a - b + m) % m;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<long long int> v(n);
    for (long long int(i) = (0); (i) < (n); (i)++) {
      cin >> v[i];
    }
    long long int start = 0, end = n - 1;
    while (start < n) {
      if (v[start] != start + 1) {
        break;
      }
      start++;
    }
    while (end >= 0) {
      if (v[end] != end + 1) {
        break;
      }
      end--;
    }
    if (start == n) {
      cout << 0 << "\n";
    } else {
      long long int gg = 1;
      while (start < end) {
        if (v[start] == start + 1) {
          gg = 2;
          break;
        }
        start++;
      }
      cout << gg << "\n";
    }
  }
  return 0;
}
