Under construction

Still being built. Things may change

Meet In The Middle

Time limit1sMemory limit1024 MB

Summary
Given a weighted tree and K queries each naming two villages, find the village where the two distances are equal, breaking ties by the smallest total distance, or -1.
Level

Hard8 of 10

Topics
Tree, Graph, Shortest path, Math
Solved
No attempts yet

Problem

The kingdom of Sinchon has NN villages numbered 11 through NN, connected by N−1N-1 roads, each joining two distinct villages. Every village can be reached from every other village by roads, and travel within a village takes no distance.

Sinchon is now in turmoil from a pandemic of "symmetry sickness." A person with symmetry sickness feels uneasy in body and mind unless everything is symmetric, to the point of being unable to carry on daily life. The aftermath is so severe that even meetings between people have become difficult. Consider two people in Sinchon who want to meet. If the meeting place is closer to one person's village than the other's, they become very uncomfortable. So the meeting place must be chosen at a spot whose distance to each person's village is exactly the same. In other words, Meet in the middle. They must meet in the middle!

Given the road information of Sinchon and the villages of the two people who want to meet, find a suitable location for the meeting place.

Input

The first line gives the number of villages NN and the number of meetings KK. (1≤N,K≤100 000)(1 \le N, K \le 100\,000)

The next N−1N-1 lines give three integers uu, vv, ww describing a road. This means there is a road of length ww between village uu and village vv. (1≤u,v≤N(1 \le u, v \le N, u≠vu \neq v, 1≤w≤109)1 \le w \le 10^9)

The next KK lines give the village numbers uu, vv of the two people who want to meet. (1≤u,v≤N)(1 \le u, v \le N)

Output

For each of the KK lines, print the number of a village where the two people can meet. If no such village exists, print -1. If there are several possible answers, print the one with the smallest sum of distances to the two people's villages.

Examples1

  1. Example 1

    Input
    7 4
    1 2 2
    1 3 3
    1 6 1
    2 4 1
    2 5 1
    3 7 1
    3 4
    4 5
    1 7
    6 6
    
    Expected output
    1
    2
    -1
    6