def sum a_dic
  s = 0
  a_dic.each do |k,v|
    s+= k*v
  end
  s
end
gets
a = gets.strip.split(" ").map(&:to_i).sort
a_dic = {}
a.each do |i|
  a_dic[i] = a_dic[i].nil? ? 1 : a_dic[i]+1
end
q = gets.strip.to_i
q.times do |i|
  b,c = gets.strip.split(" ").map(&:to_i)
  unless a_dic[b].nil? || a_dic[b].eql?(0)
    a_dic[c] =  a_dic[c].nil? || a_dic[c].eql?(0) ? a_dic[b] : a_dic[b]+a_dic[c]
    a_dic[b] = 0
  end
  puts sum(a_dic)
end

