n = int(eval(input()))

a = list(map(int, input().split()))



d = {}

ans = 0

for i in range(n):

  if a[i] in d and d[a[i]] == i+1: ans += 1

  else: d[i+1] = a[i]

print(ans)
n = int(eval(input()))

a = list(map(int, input().split()))



d = {}

ans = 0

for num,i in enumerate(a,1):

  if i not in d: d[num] = i

  else:

    if d[i] == num: ans += 1

print(ans)
n = int(eval(input()))
a = list(map(int, input().split()))
d = {}
ans = 0
for i in range(n):
    if a[i] in d and d[a[i]] == i + 1:
        ans += 1
    else:
        d[i + 1] = a[i]
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
d = {}
ans = 0
for num, i in enumerate(a, 1):
    if i not in d:
        d[num] = i
    else:
        if d[i] == num:
            ans += 1
print(ans)

-for i in range(n): -    if a[i] in d and d[a[i]] == i + 1: -        ans += 1 +for num, i in enumerate(a, 1): +    if i not in d: +        d[num] = i -        d[i + 1] = a[i] +        if d[i] == num: +            ans += 1