Limits 1s, 512 MB

You will be given a tree with nn node numbered 1 to nn with weighted edge.

You will be asked qq queries.

For each query you have to find the largest edge in path from AA to BB.

Input

The first line contains two integers nn and qq (1n,q1051 \le n, q \le 10^5). Each of the next n1n-1 lines contains three integer uiu_i, viv_i, wiw_i, (1ui1 \le u_i, vinv_i \le n and 1wi1051 \le w_i \le 10^5), meaning there is an edge between uiu_i and viv_i with weight wiw_i.

Each of the next qq lines contains two integer AA, BB (1A,Bn1 \le A, B \le n and ABA \ne B) describing the query.

Output

For each query. print the answer in a new line.

Samples

InputOutput
5 5
2 1 40
3 2 21
4 1 23
5 2 50
1 2
3 5
3 1
2 3
2 1

40
50
40
21
40

In query 1, from 1 to 2, there is only one edge and so the answer is 40.

In query 2, from 3 to 5, largest edge in path is 2 -- 5 with weight 50, so the answer is 50.

InputOutput
5 5
2 1 2
3 2 31
4 1 2
5 2 5
1 4
1 5
3 1
2 3
2 1
2
5
31
31
2

Submit

Login to submit.

Statistics

91% Solution Ratio
skmonirEarliest, Feb '18
nusuBotFastest, 0.0s
Tarik.amtolyLightest, 10 MB
sakibalaminShortest, 2324B
Toph uses cookies. By continuing you agree to our Cookie Policy.