# -*- coding: utf-8 -*-
#数を取得
count = int(input())
seq = input().split()
repeat = int(input())
#置換する
for i in range(0, repeat):
  replace = input().split()
  seq = [replace[1] if l == replace[0] else l for l in seq]
  seq2 = [int(l) for l in seq]
  print(sum(seq2))
