#Replacing
import numpy as np
N = int(input())
A = np.array(sorted(map(int, input().split())))
Q = int(input())
# u, count = np.unique(A,return_counts=True)
# asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    A = np.where(A==B,C,A)
    print(A.sum())