N = int(input())
A = list(map(int, input().split()))
S = sum(A)
count = [0 for _ in range(int(1e5))]
for a in A:
    count[a] += 1    

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