n = int(input())
a = list(map(int, input().split()))
d = [0]*(10**5+1)
t = 0
for i in range(n):
    t += a[i]
    d[a[i]] += 1
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    t += (c-b)*d[b]
    d[c] += d[b]
    d[b] = 0
    print(t)