from collections import Counter

N = int(input())
A = list(map(int, input().split()))
di = Counter(A)
S = sum(A)
for q in range(int(input())):
    b, c = map(int, input().split())
    di[c] += di[b]
    S += (c-b) * di[b]
    di[b] = 0
    print(S)