import collections

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

collect_dict = dict(collections.Counter(a))

now = sum(a)

ans = []
for i in range(q):
    b, c = map(int,input().split())
    now -= collect_dict[b] * b
    tmp = collect_dict[b]
    collect_dict.pop(b)
    collect_dict[c] += tmp
    now += c * tmp
    ans.append(now)

for i in ans:
    print(i)