#include<bits/stdc++.h>
using namespace std;
int arr[100005],temp;
inline void swap(int *a,int *b)
{
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int t,n,i,count=0;
bool flag;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&arr[i]);
flag=0;
count=0;
i=1;
while(i<=n)
{
if(i==arr[i]) { /* do nothing */ }
else if(i==arr[arr[i]])
{
count++;
swap(&arr[i],&arr[arr[i]]);
}
else if( arr[arr[arr[i]]]==i )
{
count+=2;
swap(&arr[i],&arr[arr[i]]);
swap(&arr[i],&arr[arr[i]]);
}
else
{
flag=1;
printf("Too chaotic\n");
break;
}
i++;
}
if(flag==0) printf("%d\n",count);
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGFyclsxMDAwMDVdLHRlbXA7CgppbmxpbmUgdm9pZCBzd2FwKGludCAqYSxpbnQgKmIpCnsKICAgIHRlbXA9KmE7CiAgICAqYT0qYjsKICAgICpiPXRlbXA7Cn0KCmludCBtYWluKCkKewogICAgaW50IHQsbixpLGNvdW50PTA7CiAgICBib29sIGZsYWc7CiAgICBzY2FuZigiJWQiLCZ0KTsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgICBzY2FuZigiJWQiLCZuKTsKICAgICAgICBmb3IoaT0xO2k8PW47aSsrKSBzY2FuZigiJWQiLCZhcnJbaV0pOwogICAgICAgICAgIAogICAgICAgIAogICAgICAgIGZsYWc9MDsKICAgICAgICBjb3VudD0wOwogICAgICAgIGk9MTsKICAgICAgICB3aGlsZShpPD1uKQogICAgICAgIHsKICAgICAgICAgICAgIGlmKGk9PWFycltpXSkgeyAvKiBkbyBub3RoaW5nICovIH0KICAgICAgICAgICAgIGVsc2UgaWYoaT09YXJyW2FycltpXV0pCiAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgY291bnQrKzsKICAgICAgICAgICAgICAgICAgICBzd2FwKCZhcnJbaV0sJmFyclthcnJbaV1dKTsKICAgICAgICAgICAgIH0KICAgICAgICAgICAgIGVsc2UgaWYoIGFyclthcnJbYXJyW2ldXV09PWkgKQogICAgICAgICAgICAgewogICAgICAgICAgICAgICAgIGNvdW50Kz0yOwogICAgICAgICAgICAgICAgIHN3YXAoJmFycltpXSwmYXJyW2FycltpXV0pOwogICAgICAgICAgICAgICAgIHN3YXAoJmFycltpXSwmYXJyW2FycltpXV0pOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgZmxhZz0xOwogICAgICAgICAgICAgICAgICAgcHJpbnRmKCJUb28gY2hhb3RpY1xuIik7CiAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgCiAgICAgICAgICAgICBpKys7CiAgICAgICAgICAgIAogICAgICAgICB9CiAgICAgICAgCiAgICAgICAgaWYoZmxhZz09MCkgcHJpbnRmKCIlZFxuIixjb3VudCk7CiAgICAgICAgCiAgICB9CiAgICAgICAgCn0=