#P7637. [2018年杭电多校]Rikka with Line Graph
[2018年杭电多校]Rikka with Line Graph
Rikka with Line Graph
Problem Description
Line Graph can be considered as an operator on an undirected graph just like Complementary Graph and Dual Graph. Rikka generalizes Line Graph to edge-weighted undirected graphs. For a graph , is still an edge-weighted undirected graph which is constructed in the following way:
-
has vertices and the th vertex corresponds to the th edge in .
-
There is an edge between in if and only if edge and have at least one common vertices in . And the edge weight is equal to the sum of the weights of edge and in . For example, in the following picture, the right graph is the line graph of the left one. Vertex in correspond to edge in . And if all edges in the left graph have weight , the edges in the right graph will have weight .
Now, Rikka has an edge-weighted tree with vertices. And she constructs a graph . It is clear that is connected. Let be the length of the shortest path between vertex in (the length of each edge is equal to the weight), be the number of vertices in , Rikka wants you to calculate .
Input
The first line contains a single number , the number of the testcases. For each testcase, the first line contains one single integer . Then lines follow, each line contains three integers $u_i,v_i,w_i(1 \leq u_i,v_i \leq 10^5, 1 \leq w_i \leq 10^9)$, describe an edge with weight between and . The input guarantees that has at least one vertices and there are at most testcases with .
Output
For each testcase, output a single line with a single number, the answer modulo .
Sample Input
3
4
1 2 1
1 3 2
1 4 3
5
1 2 1
2 3 10
2 5 7
3 4 2
10
1 2 1
1 3 1
2 4 1
2 5 1
2 6 1
3 7 1
7 8 1
5 9 1
6 10 1
Sample Output
24
166
420
Source
2018 Multi-University Training Contest 9