#P6995. Travel on Tree
Travel on Tree
Problem Description
In the world of $\textit{The Three-Body Problem}$, about 200 years later, people will live in huge underground tree buildings. In this problem, we use the tree data structure to describe tree buildings.
There is a tree with $n$ nodes and $n - 1$ edges with length, and $n$ of your friends live in the tree, one at each node. You are planning to visit your friends in the next $m$ days. Each day you choose an interval $[l, r]$ and plan to visit friends living in the nodes numbered from $l$ to $r$. You can choose an arbitrary node $u$ to start the day's visit, then travel on the tree along the edges, and finally go back to $u$. During the travel, you should visit all your friends living in the nodes numbered from $l$ to $r$. You can visit these friends $\textbf{in any order}$ and you can pass a node without visiting the friend. Please calculate the minimum total distance of the travel for each day.
Input
The first line of the input contains an integer $T \ (1 \leq T \leq 2 \times 10^4)$ --- the number of test cases.
The first line of each test case contains two integers $n, m \ (1 \leq n, m \leq 10^5)$ --- the number of nodes and the number of days.
Each of the following $n - 1$ lines of each test case contains three integers $u, v, w \ (1 \leq u, v \leq n, 1 \leq w \leq 10^4)$ --- an edge connecting nodes $u$ and $v$ with length $w$.
Each of the following $m$ lines of each test case contains two integers $l, r \ (1 \leq l \leq r \leq n)$ --- a plan to visit your friends living in the nodes numbered from $l$ to $r$.
It is guaranteed that for all test cases, $\sum n \leq 10^6, \ \sum m \leq 10^6$.
Output
For each day's plan, output the answer in a single line.
2
3 3
1 2 1
2 3 1
1 1
1 2
1 3
5 5
1 2 1
1 3 2
3 4 3
3 5 4
1 3
1 4
1 5
2 5
3 5
0
2
4
6
12
20
20
14