from collections import Counter
import copy

N = int(input())

target_list = map(int, input().split())
trans_list = {}

for i in range(int(input())):
    trans_list[i] = list(map(int, input().split()))

check = []
check.append(Counter(target_list))
answer = []

for index, trans in trans_list.items():
    if trans[0] in check[index].keys():
        result = copy.deepcopy(check[index])
        if trans[1] in result:
            result[trans[1]] = result[trans[1]] + result.pop(trans[0])
        else:
            result[trans[1]] = result.pop(trans[0])
    else:
        result = copy.deepcopy(check[index])
    check.append(Counter(result))

for r in check:
    print(sum([x * y for x, y in r.items()]))
