The "Mystery Incorporated" once went for an investigation. Since Scooby and Shaggy always create havoc during investigation, Velma decided to give them a question so as to keep them busy.
Velma gave them a tree consisting of N nodes, rooted at node 1 with a number in each node, and asked them several queries. Each query was of the form u l and they had to find the sum of values of all nodes situated at a depth l and were in the subtree of u.
Since Scooby and Shaggy are dumb, they need your help in solving this problem.
The first line will contain the number of test cases T
The first line of each test case will contain two integers N
(1 ≤ N, Q
≤ 100,000) - the number of nodes and the number of queries.
The second line of each test case will contain N
integers a1, a2, ..., aN
, where ai
(1 ≤ ai
≤ 10,00,000) is the value stored at the ith
The next N-1
lines of each test case will contain two integers u
, meaning that there is an edge connecting u
The next Q
lines contains the queries each of the form u
For each query in every test case, print the answer to the query on a single line.
- $$ 1 \leq T \leq 5, 1 \leq N, Q \leq 1000, \ 1 \leq a_i \leq 10^6 $$ in 35% of test cases.
- $$ 1 \leq T \leq 10, 1 \leq N, Q \leq 10^5, \ 1 \leq a_i \leq 10^6 $$ in 65% of test cases.
1 2 3 4 5 6
The tree in the test case looks like this.
The depths of nodes are:
- Nodes at depth 1: 1
- Nodes at depth 2: 2, 4
- Nodes at depth 3: 3, 5 and 6
For the 2nd query , there are 3 nodes (3, 5, 6) at level 3 which are also in subtree of 1. So the answer would be 3+5+6 = 14.
For the 3rd query, there is only 1 node (1) which is at level 1, but it is not in the subtree of 2. So the answer would be 0.
for each input file.
Marks are awarded when all the testcases pass.