#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
using namespace std;
inline long long add(long long a, long long b) {
  return ((a + b) < 1000000007 ? (a + b) : (a + b - 1000000007));
}
int prime[1000005];
void seive() {
  prime[0] = prime[1] = 1;
  for (int i = 2; i <= sqrt(1000005); i++) {
    if (prime[i]) continue;
    for (int j = 2 * i; j <= 1000005; j += i) {
      prime[j] = 1;
    }
  }
}
inline long long sub(long long a, long long b) {
  return ((a - b) < 0 ? a - b + 1000000007 : a - b);
}
long long power(long long a, long long b) {
  if (b == 0) return 1;
  long long y = power(a, b / 2);
  y = (y * y) % 1000000007;
  if (b & 1) y = (a * y) % 1000000007;
  return y;
}
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n + 1];
    map<long long, long long> m;
    long long c = -1;
    long long b[n + 1];
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
    }
    for (long long i = 1; i <= n; i++) {
      b[i] = i - a[i];
      if (b[i] < 0) b[i] = b[i] + n;
      m[b[i]]++;
      if (m[b[i]] == n) c = b[i];
    }
    long long lst = -1, str = -1, cnt = 0;
    for (long long i = 1; i <= n; i++) {
      if (b[i] != 0 && str == -1) {
        str = i;
        cnt++;
      } else if (b[i] != 0) {
        lst = i;
        cnt++;
      }
    }
    long long sol = 2;
    if (c == 0)
      sol = 0;
    else if (c != -1)
      sol = 1;
    else {
      sol = 2;
    }
    if (str != -1 && lst - str + 1 == cnt) sol = 1;
    cout << sol << endl;
  }
}
