#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()))
    asum += count[u==B].sum() * (C-B)
    u = np.where(u==B,C,u)
    print(asum)