def my_input():
    i = list(map(int, input().split()))
    if len(i) == 1:
        return i[0]
    return i


n = my_input()
a = [0]+my_input()
a.sort()
d = {}
for i in range(1, n+1):
    if a[i] != a[i-1]:
        d[a[i]] = 0
    d[a[i]] += 1
s = sum(a)
q = my_input()
for i in range(q):
    b, c = my_input()
    if b in d:
        b_count = d.pop(b)
        s += (c-b)*b_count
        if c in d:
            d[c] += b_count
        else:
            d[c] = b_count
    print(s)