n = gets.to_i
index = {}
input = gets.strip.split(' ').each{|v|
    index[v] = 0 if index[v].nil?
    index[v] += 1
}

c = gets.to_i
c.times{|i|
    from, to = gets.strip.split(' ')
    froms = index.delete(from)
    unless froms.nil?
        index[to] = 0 if index[to].nil?
        index[to] += froms
    end
    sum = 0
    index.each_pair{|key, val|
        sum += key.to_i * val
    }
    puts sum
}