#1420. Escape The Maze

Escape The Maze

Alice is currently trapped in a maze, which can be seen as a tree. Each edge in the tree has a weight representing the length of that edge. The leaves of the tree represent the exits, and when Alice reaches a leaf, it means she has successfully escaped from the maze.

A leaf is defined as a node with degree 1 that is not the root.

Each maze has a difficulty level, denoted as LL. When Alice is at a node xx in the tree, she can choose to jump to a node yy in her subtree. Let s be the sum of the edge weights along the path from xx to yy. The energy spent when jumping from xx to yy is (sL)2(s − L)^2.

Alice wants to know the minimum amount of energy required to escape the maze if the tree has pp as the root and she starts from pp. Alice will ask this question a total of QQ times.

The data guarantees that for any given pair of points xx and yy, the absolute value of the sum of edge weights ss along the path between them does not exceed 10910^9.

Input

The input consists of multiple test cases. The first line contains a single integer TT(1T51 \le T \le 5) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn, LL (3n1053 \le n \le 10^5, 105L105−10^5 \le L \le 10^5) — the number of nodes in the tree.

Each of the next n1n − 1 lines contains three integers uu, vv, ww (1u,vn1 \le u, v \le n, uvu \ne v, 105w105−10^5 \le w \le 10^5).

The next line contains a positive integer QQ (1Q101 \le Q \le 10).

Each of the next QQ lines contains one integer pp (1pn1 \le p \le n) asks the minimum amount of energy required to escape the maze if the tree has pp as the root and she starts from pp.

It is guaranteed that the given graph is a tree.

Output

For each test case, output QQ lines. Each line should contain a integer indicating the minimum amount of energy required.

The data guarantees that the answer will not exceed the range that can be represented by a 64-bit signed integer.

Example

1
4 2
1 2 5
1 3 -4
1 4 6
4
1
2
3
4
9
1
0
0

Limitation

Time limit: 8 seconds

Memory limit: 512 megabytes