n = int(input())
list_A = input().split()
q = int(input())
for i in range(q):
  SUM = 0
  b, c = input().split()
  if b in list_A:
    list_A = [c if x == b else x for x in list_A]
  else:
    pass
  SUM += sum(map(int, list_A))
  print(SUM)