n = int(input())
a = list(map(int, input().split()))
q = int(input())
operation = [list(map(int, input().split())) for _ in range(q)]

from  collections import Counter

dict_a = Counter(a)

for i in range(q):
  dict_a[operation[i][1]] += dict_a[operation[i][0]]
  dict_a[operation[i][0]] = 0
  s = 0
  for (k, v) in dict_a.items():
    s += k*v
  print(s)
