n = int(input())
a = list(map(int, input().split()))
x = [0]*100001
for i in range(n):
    x[a[i]] += 1
q = int(input())
ans = sum(a)
for i in range(q):
    b, c = map(int, input().split())
    ans += x[b]*(c-b)
    x[c] += x[b]
    x[b] = 0
    print(ans)