n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

from collections import Counter
counter = Counter(a)

for b,c in bc:
    if b in counter:
        cnt = counter.pop(b)
        counter[c] += cnt
    print(sum([k*v for k,v in counter.items()]))