from collections import Counter
N = int(input())
As = list(map(int, input().split()))

ct = Counter(As)
S = sum(As)

Q = int(input())
for _ in range(Q):
    b, c = map(int, input().split())
    n = ct[b]
    ct[b] = 0
    ct[c] += n
    S += (c-b)*n
    print(S)
