require 'tsort' class Hash include TSort alias tsort_each_node each_key def tsort_each_child(node, &block) fetch(node).each(&block) end end N, Q = gets.split.map(&:to_i) E = Hash.new RE = Hash.new { |h, k| h[k] = [] } 1.upto(N) do |v| E[v] = [] end (N - 1).times do a, b = gets.split.map(&:to_i).sort E[a] << b RE[b] << a end C = Array.new(N + 1, 0) E.tsort.each do |v| C[v] += 1 RE[v].each do |u| C[u] += C[v] end end ans = 0 Q.times do p, x = gets.split.map(&:to_i) ans += C[p] * x puts ans end