#F. Problem F. Escape The Maze

    传统题 8000ms 512MiB

Problem F. Escape The Maze

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

Description

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 L. When Alice is at a node x in the tree, she can choose to jump to a node y in her subtree. Let s be the sum of the edge weights along the path from x to y. The energy spent when jumping from x to y is(sL)2 (s − L) ^2 . Alice wants to know the minimum amount of energy required to escape the maze if the tree has p as the root and she starts from p. Alice will ask this question a total of Q times. The data guarantees that for any given pair of points x and y, the absolute value of the sum of edge weights s along the path between them does not exceed 10910^9 .

Format

Input

The input consists of multiple test cases. The first line contains a single integer T(1 ≤ T ≤ 5) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers n, L (3n105,105L1053 ≤ n ≤ 10^5 , −10^5 ≤ L ≤ 10^5 )— the number of nodes in the tree. Each of the next n − 1 lines contains three integers u, v, w (1u,vn,uv,105w1051 ≤ u, v ≤ n, u \neq v, −10^5 ≤ w ≤ 10^5 ) . The next line contains a positive integer Q (1 ≤ Q ≤ 10). Each of the next QQ lines contains one integer p (1 ≤ p ≤ n) asks the minimum amount of energy required to escape the maze if the tree has p as the root and she starts from p. It is guaranteed that the given graph is a tree.

Output

For each test case, output Q 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.

Samples

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

Limitation

8s, 512mb for each test case.

杭电“钉耙编程”挑战(纯英纯硬,持续更新ing)

未参加
状态
已结束
规则
ACM/ICPC
题目
7
开始于
2023-8-5 12:00
结束于
2023-9-24 12:00
持续时间
1200 小时
主持人
参赛人数
28