n = input()
a = list(map(int, input().split()))
q =int(input())
rest = 0
for i in range(q):
  b,c = map(int, input().split())
  numb = a.count(b)
  if numb > 0:
    for i in range(numb):
        a.remove(b)
        a.append(c)
  s = sum(a)
  print(s)