https://www.acmicpc.net/problem/10451
def dfs(w):
check[w]=True
ans=0
for i in range(1,n+1):
if arr[w]==i and check[i]==False:
dfs(i)
ans=1
elif arr[w]==i and w==i: ans=1
return ans
t=int(input())
for i in range(t):
n=int(input())
count=0
arr=[0 for _ in range(n+1)]
check = [False for _ in range(n + 1)]
st=list(map(int,input().split()))
for i in range(1,n+1):
arr[i]=st[i-1]
for i in range(1,n+1):
if dfs(i)==1: count+=1
print(count)