n = gets.to_s.to_i64
a = gets.to_s.split.map(&.to_i64)
q = gets.to_s.to_i64

h = Hash(Int64, Int64).new { |h, k| h[k] = 0_i64 }
a.each { |v| h[v] += 1 }

q.times do
  b, c = gets.to_s.split.map(&.to_i64)
  h[c] += h[b]
  h[b] = 0
  
  ans = 0_i64
  h.each do |k,v|
    ans += k * v
  end
  puts ans
end