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

s = sum(a)

num = [0]*10**5

for i in range(10**5):
    num[i] = a.count(i)

for i in range(q):
    b, c = map(int, input().split())
    d = (c-b) * num[b]
    num[c] += num[b]
    num[b] = 0
    s += d
    print(s)
