import numpy as np

n = int(input())
A = np.array(list(map(int, input().split())))
q = int(input())

for i in range(q):
    a, b = map(int, input().split())
    A = np.where(A == a, b, A)
    print(A.sum())

