#include <bits/stdc++.h>
long long mod(long long x) {
  if (x < 0) return -x;
  return x;
}
long long expo(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y % 2 == 1) res = (res * x);
    y = y >> 1;
    x = x * x;
  }
  return res;
}
using namespace std;
const int MAXN = 500005;
priority_queue<int, vector<int>, greater<int> > pq;
vector<long long> v;
long long a[1000000], b[1000000];
int main() {
  ios_base::sync_with_stdio(false);
  long long n = 0, t = 0, m = 0, s, k = 0, x = 0, y = 0;
  cin >> t;
  while (t--) {
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
    }
    x = -1;
    y = n + 1;
    k = 0;
    for (long long i = 1; i <= n; i++) {
      if (a[i] != i) {
        x = i;
        break;
      }
    }
    for (long long i = n; i >= 1; i--) {
      if (a[i] != i) {
        y = i;
        break;
      }
    }
    for (long long i = x + 1; i <= y - 1; i++) {
      if (a[i] == i) {
        k = 1;
        break;
      }
    }
    if (x == -1 || y == n + 1)
      cout << "0\n";
    else if (k == 1) {
      cout << '2' << endl;
    } else {
      if (x == -1 || y == n + 1)
        cout << "0\n";
      else {
        cout << "1\n";
      }
    }
  }
  return 0;
}
