s=0;n=int(eval(input()));*a,=list(map(int,input().split()))

for i in range(~-n):s+=(-~i<a[i])*(a[~-a[i]]==-~i)

print(s)
n=int(eval(input()));*a,=list(map(int,input().split()))

print((sum([a[~-a[i]]==-~i for i in range(n)])>>1))
s = 0
n = int(eval(input()))
(*a,) = list(map(int, input().split()))
for i in range(~-n):
    s += (-~i < a[i]) * (a[~-a[i]] == -~i)
print(s)

n = int(eval(input()))
(*a,) = list(map(int, input().split()))
print((sum([a[~-a[i]] == -~i for i in range(n)]) >> 1))

-s = 0 -for i in range(~-n): -    s += (-~i < a[i]) * (a[~-a[i]] == -~i) -print(s) +print((sum([a[~-a[i]] == -~i for i in range(n)]) >> 1))