from collections import Counter
input()
A = [int(a) for a in input().split()]
d = Counter(A)
sum_ = sum(A)
for q in range(int(input())):
    B,C = map(int,input().split())
    sum_ += ((C - B) * d[B])
    d[C] += d[B]
    del d[B]
    sum2 = 0
    for k,v in d.items():
        sum2 += k*v
    print(sum2)    