N = int(input())
A = list(map(int, input().split()))
lis = [0] * (10**5)
Q = int(input())
for x in A:
  lis[x] += 1 * x
for i in range(Q):
  B, C = map(int, input().split())
  try:
    temp = lis[B] // B
    lis[B] = 0
    lis[C] += temp * C
  except:
    pass
  print(sum(lis))