创建于 2026/07/18 · 更新于 2026/07/18
Turing Daughter is Turing Grandma's granddaughter. Like her grandmother, she also loves knitting wool sweaters.
Each sweater she knits is represented by an unweighted tree with vertices numbered . Its edges have pairwise distinct colors numbered .
She first knits a sweater . She then modifies it times in order, producing the sweaters
In the -th modification, she removes the edge of color from and knits that edge back between vertices and , keeping its color . It is guaranteed that the resulting sweater is still represented by a tree.
Therefore, there are exactly sweaters: .
The complexity of a sweater is the diameter of its tree: the greatest number of edges on a simple path between two of its vertices. In particular, a sweater consisting of a single vertex has complexity .
For a sweater , removing its edge of color splits it into two smaller sweaters, whose trees are the two connected components and . Define
There are queries. In each query, Turing Daughter chooses a time interval and removes the edge of color from every sweater . This leaves exactly smaller sweaters. She wants to know the maximum complexity among all of them, that is,
The first line contains two integers and (, ).
The next lines describe . The -th of these lines contains two integers and (), indicating that the edge of color connects vertices and .
The next lines describe the modifications. The -th of these lines contains three integers (, ), describing the modification that transforms into .
The next lines each contain three integers (, ), describing a query.
It is guaranteed that and the graph obtained after every modification are trees.
For each query, output one integer on a separate line.
4 4
1 2
2 3
3 4
1 1 2
2 1 3
3 2 4
0 0 2
0 3 2
1 2 1
2 3 3
1
2
2
2