You live in a city where there are initially points. Each tram connects two points in the city , and its schedule is described by three numbers: t_1, t_2, and .
Every tram operates on a periodic schedule with a fixed period (it is the same for all trams).
From to , the tram departs at times t_2 + k \cdot T, where is a non-negative integer. The journey from to takes t_1 minutes.
From to , the tram departs at times t_2 + t_1 + k \cdot T, and the journey back takes T-t_1 minutes.
No two trams operate between the same pair of points. Initially, there are exactly n-1 trams and points in the city. There are no cycles. So the given graph forms a tree.
The following types of operations can be performed:
Query: Given a starting time , determine the minimum time required to travel from point to point ;
Update Schedule: Modify the parameters t_1, t_2 for a specific tram;
Remove Point: Remove a point that has exactly one tram connected to it;
Add tram: Introduce a new point and a tram that connects it to an existing point. The new tram is defined by its parameters t_1, t_2.
Your task is to efficiently handle these operations while maintaining the network's dynamic state and ensuring accurate results for travel time queries.
In the first line are T, n, q, g (1\le n, q\le 2\cdot10^5, 2 \le T \le 10^9, 0 \le g \le 1) — the period of each tram, the number of points at the beginning, and the number of queries. is a number which used to calculate final vertices of the query. Read about it at the end of this section.
In the next n-1 lines are four numbers x, y, t_1, t_2 (1\le x, y\le n, 1\le t_1, t_2 \le10^9, t_1+t_2<T) — two points between which tram move and two numbers which describe schedule.
In the next lines, each query starts with a number (1 \leq m \leq 4) indicating the type of operation:
If m = 1, the query contains three numbers x, y, t (1 \leq t \leq 10^9, 1 \le x, y \le n + q), where and are two points between which people travel, starting at time . Both and already exist in the city. is starting point.
If m = 2, the query contains four numbers x, y, t_1, t_2 (t_1+t_2<T, 1 \le x, y \le n + q), where and are two points between which a tram moves. The numbers t_1, t_2 describe the tram's schedule. Both and already exist in the city.
If m = 3, the query contains one number , representing a point that already exists and has exactly one tram connected to it. This point must be deleted.
If m = 4, the query contains four numbers x, y, t_1, t_2 (t_1+t_2<T, 1 \le x, y \le n + q), where and are two points between which a tram moves. The numbers t_1, t_2 describe the tram's schedule. Here, already exists in the city, and is the first yet unused point in the city's numbering.
Let be the answer to the last query of type (ans=0 if there was no query of type yet), then the real points of the current query are x'=((x-1+ans \cdot g) \mod (n + q))+ 1, y'=((y-1+ans \cdot g) \mod (n + q)) + 1.
For each query of the type 1 print one number minimum amount of the minutes their trip will take.
Example 1
Example 2
In the sample, first, at the first query of type 1 they have to move in this order 5 3 1 2 4.In the node 5 they wait till tram arrive at moment 4, than they will be in node 3 at moment 8, next moment tram from 1 to 3 will be in node 3 is 10, so they will arrive to node 1 at moment 13. At node 1 tram will be at moment 17 and they will arrive to node 2 at moment 22, then tram will be in node 2 at moment 24 and finally they will arrive to node 4 at moment 29. So the answer will be 27.
( points): n, q\le 10^3, g=0;
( points): m=1, g=0;
( points): m\le2, g=0;
( points): points with trams form a bamboo, g=0;
( points): g=0;
( points): g=1;