QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#14379. The Elder

統計

Once upon a time, in the mystical continent, there is a frog kingdom, ruled by the oldest frog, the Elder. The kingdom consists of $N$ cities, numbered from east to west. The $1^{\text{st}}$ city, which is located to the east of others, is the capital. Each city, except the capital, links none or several cities to the west, and exactly one city to the east.

There are some significant news happening in some cities every day, the Elder want to know them as soon as possible. So, that is the job of journalist frogs, who run faster than any other frog. Once some tremendous news happen in a city, the journalist in that city would take the message and run to the capital. Once it reach another city, it can either continue running, or stop at that city and let another journalist to transport. The journalist frogs are too young and simple to run a long distance efficiently. As a result, it takes $L^2$ time for them to run through a path of length $L$. In addition, passing message requires $P$ time for checking the message carefully, because any mistake in the message would make the Elder become extremely angry.

Now you are excited to receive the task to calculate the maximum time of sending a message from one of these cities to the capital.

Input

The first line of input contains an integer $t$, the number of test cases. $t$ test cases follow.

For each test case:

In the first line there are two integers $N$ ($N \leq 100\,000$) and $P$ ($P \leq 10\,000$).

In the next $N - 1$ lines, the $i$-th line describes the $i$-th road: a line with three integers $u$ $v$ $w$ denotes an edge between the $u$-th city and $v$-th city with length $w$ ($w \leq 100$).

Output

For each case, output the maximum time.

Examples

Input

3
6 10
1 2 4
2 3 5
1 4 3
4 5 3
5 6 3
6 30
1 2 4
2 3 5
1 4 3
4 5 3
5 6 3
6 50
1 2 4
2 3 5
1 4 3
4 5 3
5 6 3

Output

51
75
81

Explanation

In the second case, the best transportation time is:

  • The $2$-th city: $16 = 4^2$
  • The $3$-th city: $72 = 4^2 + 30 + 5^2$
  • The $4$-th city: $ 9 = 3^2$
  • The $5$-th city: $36 = (3 + 3)^2$
  • The $6$-th city: $75 = (3 + 3)^2 + 30 + 3^2$

Consequently, the news in the $6$-th requires most time to reach the capital.