from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
S = sum(A)
C = Counter(A)
Q = int(input())
for i in range(Q):
    Bi, Ci = map(int, input().split())
    S += (Ci - Bi) * C[Bi]
    C[Ci] += C[Bi]
    C[Bi] = 0
    print(S)