from collections import Counter
#input = raw_input
n = int(input())
As = list(map(int, input().split(" ")))

counter = Counter(As)
cur_sum = sum(As)


q = int(input())
bs = []
cs = []
for i in range(q):
    b, c = map(int, input().split(" "))
    bs.append(b)
    cs.append(c)

set_bc = set(bs) | set(cs)
sa = set_bc - set(list(counter.keys()))
#print("sa", sa)

for key in sa:
    counter[key] = 0
#print(counter)

for i in range(q):
    b = bs[i]
    c = cs[i]

    num1 = counter[b]
    counter[c] += num1

    diff = (c - b) * num1

    cur_sum += diff
    print(cur_sum)