#include <bits/stdc++.h>
using namespace std;
void TC(){
int n; cin >> n;
vector<pair<int, int>> a(n);
for(int i = 0 ; i < n; i++){
int x; cin >> x;
a[i].first = x;
a[i].second = i;
}
sort(a.begin(), a.end());
int ans = 0;
for(int i = 0; i < n; i++){
if(a[i].second != i){
swap(a[i].first, a[a[i].second].first);
swap(a[i].second, a[a[i].second].second);
if(a[i].second !=i) --i;
++ans;
}
}
cout << ans << endl;
}
int main(){
int t; cin >> t;
while(t--){
TC();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBUQygpewoJaW50IG47IGNpbiA+PiBuOwoJdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiBhKG4pOwoJZm9yKGludCBpID0gMCA7IGkgPCBuOyBpKyspewoJCWludCB4OyBjaW4gPj4geDsKCQlhW2ldLmZpcnN0ID0geDsKCQlhW2ldLnNlY29uZCA9IGk7Cgl9Cglzb3J0KGEuYmVnaW4oKSwgYS5lbmQoKSk7CglpbnQgYW5zID0gMDsKCWZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspewoJCWlmKGFbaV0uc2Vjb25kICE9IGkpewoJCQlzd2FwKGFbaV0uZmlyc3QsIGFbYVtpXS5zZWNvbmRdLmZpcnN0KTsKCQkJc3dhcChhW2ldLnNlY29uZCwgYVthW2ldLnNlY29uZF0uc2Vjb25kKTsKCQkJaWYoYVtpXS5zZWNvbmQgIT1pKSAtLWk7CgkJCSsrYW5zOwoJCX0KCX0KCWNvdXQgPDwgYW5zIDw8IGVuZGw7Cn0KCmludCBtYWluKCl7CglpbnQgdDsgY2luID4+IHQ7Cgl3aGlsZSh0LS0pewoJCVRDKCk7Cgl9Cn0=