https://www.acmicpc.net/problem/10451
using System;
using System.Collections;
class Program
{
static int[] arr;
static bool[] check;
static int n;
static int dfs(int v){
check[v]=true;
int ans=0;
for(int i=1;i<=n;i++){
if(arr[v]==i && check[i]==false){
dfs(i);
ans=1;
}
else if(arr[v]==i && v==i) ans=1;
}
return ans;
}
static void Main() {
string s=Console.ReadLine();
int t=int.Parse(s);
while(t-->0){
string s1=Console.ReadLine();
n=int.Parse(s1);
int count=0;
arr=new int[n+1];
check=new bool[n+1];
string[] s2=Console.ReadLine().Split(' ');
for(int i=1;i<=n;i++){
arr[i]=int.Parse(s2[i-1]);
}
for(int i=1;i<=n;i++){
if(dfs(i)==1) count++;
}
Console.WriteLine(count);
}
}
}