import collections

N = int(input())
A = list(map(int,input().split()))
S = sum(A)
c = collections.Counter(A)

Q = int(input())

for i in range(Q):
    B, C = map(int, input().split())
    ans = S - B * c[B] + C * c[B]
    print(ans)
    S = ans
    c[B] = 0
    c[C] += c[C]