#include <bits/stdc++.h>
using namespace std;
long long int mod(long long int x, long long int m) { return (x % m + m) % m; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1, r, n, a, d, c, b, x, y, sum, cnt, i, j, k, m, l, q, maxx,
                minn, temp, ans, flag, g, ts;
  long long int mod = 1000000007;
  char ch, chh;
  double dans;
  string str, ttr;
  cin >> t;
  for (ts = 1; ts <= t; ts++) {
    flag = 0;
    sum = 0;
    cnt = 0;
    ans = 0;
    k = 0;
    maxx = 0;
    minn = 0;
    a = 0;
    b = 0;
    temp = 0;
    cin >> n;
    long long int arr[n];
    for (i = 0; i < n; i++) {
      cin >> arr[i];
    }
    for (i = 0; i < n; i++) {
      if (arr[i] != (i + 1)) {
        cnt++;
        while (i < n && arr[i] != (i + 1)) {
          i++;
        }
        i--;
      }
    }
    if (cnt == 0)
      cout << 0;
    else if (cnt == 1)
      cout << 1;
    else
      cout << 2;
    cout << "\n";
  }
  return 0;
}
