n = gets.to_i
al = Hash.new(0).merge(gets.split.map(&:to_i).tally)
q = gets.to_i
bcl = q.times.map { gets.split.map(&:to_i) }

bcl.each do |b, c|
  al[c] += al[b]
  al[b] = 0
  puts al.sum { |k, v| k*v }
end