from collections import defaultdict

n = int(input())

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

dic = defaultdict(int)
for num in nums:
    dic[num] += 1 


answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    dic[c] += dic[b]
    dic[b] = 0

    answer = 0
    for i, v in dic.items():
        answer += i * v

    answers.append(answer)

for answer in answers:
    print(answer)