https://www.acmicpc.net/problem/10451
import java.util.*;
var arr=Array<ArrayList<Int>>(1001) {ArrayList()}
var check=Array<Boolean>(1001) {false}
fun bfs(v:Int):Boolean{
if(check[v]) return false
var e=true
check[v]=true
var f=v
while(e){
e=false
var t=arr[f].get(0)
if(check[t]==false){
e=true
check[t]=true
f=t
}
}
return true
}
fun main(args: Array<String>) {
val s=Scanner(System.`in`)
var t=s.nextInt()
while(t-->0){
for(i in 0..1000) arr[i].clear()
Arrays.fill(check,false)
val n=s.nextInt()
for(i in 1..n) arr[i].add(s.nextInt())
var count=0
for(i in 1..n) if(bfs(i)) count++
println(count)
}
}