N = int(eval(input()))

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

l = [set([]) for k in range(N+1)]

ans = 0

for k in range(N):

    if k+1 in l[a[k]]:

        ans += 1

    l[k+1].add(a[k])

print(ans)

N = int(eval(input()))

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

ans = 0

for k in range(N):

    if a[a[k]-1] == k+1:

        ans += 1

print((ans//2))

N = int(eval(input()))
a = list(map(int, input().split()))
l = [set([]) for k in range(N + 1)]
ans = 0
for k in range(N):
    if k + 1 in l[a[k]]:
        ans += 1
    l[k + 1].add(a[k])
print(ans)

N = int(eval(input()))
a = list(map(int, input().split()))
ans = 0
for k in range(N):
    if a[a[k] - 1] == k + 1:
        ans += 1
print((ans // 2))

-l = [set([]) for k in range(N + 1)] -    if k + 1 in l[a[k]]: +    if a[a[k] - 1] == k + 1: -    l[k + 1].add(a[k]) -print(ans) +print((ans // 2))