import numpy as np
N = int(input())
A = A = np.array(list(map(int, input().split()))).reshape((N, 1))

Q = int(input())
for i in range(Q):
    B,C = map(int, input().split())
    A = np.where(A == B, C , A)
    print(A.sum())