import collections
N = int(input())
A = [int(i) for i in input().split()]

Cnt = collections.Counter(A)
S = sum(A)

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