N = gets.to_i
AN = gets.split.map(&:to_i)
BC = gets.chomp.to_i.times.map { gets.split.map(&:to_i) }

require "set"

changes = Set.new(BC.map { |bc| bc[0] })

axy = AN.map { |n|
  if changes.include?(n)
    BC.map { |bc|
      b, c = bc
      n = n == b ? c : n
    }
  else
    n * BC.length
  end
}

(0...BC.length).each do |i|
  puts axy.map { |ax| ax[i] }.sum
end
