N = gets.to_i
A = gets.split.map(&:to_i)
Q = gets.to_i
BC = Q.times.map{ gets.split.map(&:to_i) }

h = A.each_with_object(Hash.new(0)){|i,h| h[i] += 1}
BC.each do |b,c|
  h[c] += h[b]
  h[b] = 0
  puts h.sum{|k,v| k * v}
end