from collections import Counter
n = int(input())
a = list(map(int, input().split()))
count = Counter(a)
q = int(input())
b_c = [list(map(int, input().split())) for i in range(q)]
for i in b_c:
  count[i[1]] += count[i[0]]
  count[i[0]] = 0
  ans = 0
  for k, v in count.items():
    ans += k * v
  print(ans)