
n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
hash = Hash.new(0)
aa = a.size
#(0..10001).each do |i|
    #hash[*0..100] = 0
#end
(0..aa-1).each do |i|
    hash[a[i]] += 1
end
#p hash
q = gets.chomp.to_i
q.times do 
    all = []
    b,c = gets.chomp.split(" ").map(&:to_i)
    
    hash[c] += hash[b]
    hash[b] = 0
    #p hash
    hash.each {|i,j| all <<  i*j }
    puts all.inject(:+)
end