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

d = [0] * (10**5+1)
for i in a:
    d[i] += 1

q = int(input())

for _ in range(q):
    b, c = map(int, input().split())
    d[c] += d[b]
    d[b] = 0
    ans = 0
    for i, v in enumerate(d):
        ans += i * v
    print(ans)