import numpy as np
N = int(input())
A = np.asarray(list(map(int, input().split())))
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    A = np.where(A == B, C, A)
    print(np.sum(A))

    