N = gets.to_i
a = gets.split.map(&:to_i)
Q = gets.to_i
b_c = []
Q.times do
  b_c << gets.split.map(&:to_s)
end

class Array
  def change(a, b)
    new_array = self.join(" ").gsub!(a,b).split.map(&:to_i)
    new_array
  end
end


b_c.each do |i|
  a = a.change(i[0], i[1]) if a.join.match i[0]
  puts a.sum
end
