if !ENV['RUBY_THREAD_VM_STACK_SIZE'] exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'}, '/usr/bin/ruby', $0) else N, Q = gets.split.map(&:to_i) E = Hash.new { |h, k| h[k] = [] } (N - 1).times do a, b = gets.split.map(&:to_i) E[a] << b E[b] << a end C = Array.new(N + 1, 0) def dfs(v, visited) cnt = 1 visited[v] = true E[v].each do |u| next if visited[u] cnt += dfs(u, visited) end C[v] = cnt end visited = Array.new(N + 1, false) dfs(1, visited) ans = 0 Q.times do p, x = gets.split.map(&:to_i) ans += C[p] * x puts ans end end