import numpy as np
n = int(input())
a = np.array([int(a) for a in input().split()])
q = int(input())
bc = np.array([[int(a) for a in input().split()] for j in range(q)])

u, c = np.unique(a, return_counts=True)
dica = dict(zip(u, c))
#print(dica)

keys = dica.keys()

for i in range(q):
  t1, t2 = bc[i]
  if not t1 in keys:
    dica[t1] = 0
    
  if not t2 in keys:
    dica[t2] = 0


for i in range(q):
  t1, t2 = bc[i]
  dica[t2] = dica[t1] + dica[t2]
  dica[t1] = 0
  print(sum([ k*dica[k] for k in dica.keys()]))




