n = gets.to_i
hash = {}
gets.split.map(&:to_i).each do |num|
  hash[num] ||= 0
  hash[num] += 1
end

q = gets.to_i
q.times do
  b, c = gets.split.map(&:to_i)
  hash[c] ||= 0
  hash[c] += hash[b].to_i
  hash[b] = 0

  sum = 0
  hash.each do |num, count|
    sum += num * count
  end
  puts sum
end
