n = int(eval(input()))

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



st = set()

cnt = 0

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

    pair = min(i, e), max(i, e)

    if pair in st:

        cnt += 1

    else:

        st.add(pair)



print(cnt)

n = int(eval(input()))

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



cnt = 0

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

    if a[e-1] == i:

        cnt += 1



cnt //= 2

print(cnt)

n = int(eval(input()))
a = list(map(int, input().split()))
st = set()
cnt = 0
for i, e in enumerate(a, 1):
    pair = min(i, e), max(i, e)
    if pair in st:
        cnt += 1
    else:
        st.add(pair)
print(cnt)

n = int(eval(input()))
a = list(map(int, input().split()))
cnt = 0
for i, e in enumerate(a, 1):
    if a[e - 1] == i:
        cnt += 1
cnt //= 2
print(cnt)

-st = set() -    pair = min(i, e), max(i, e) -    if pair in st: +    if a[e - 1] == i: -    else: -        st.add(pair) +cnt //= 2