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) OE = Array.new(N + 1) { [] } 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) OE[a] << b OE[b] << a end visited = Array.new(N + 1, false) queue = [1] until queue.empty? v = queue.shift next if visited[v] visited[v] = true OE[v].each do |u| next if visited[u] E[v] << u RE[u] << v queue << u end 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