There is a graph with vertices and undirected edges. Graph is a connected graph, and the vertices are numbered . Each edge has a length and a weight .
There are queries. Each query consists of an origin and a threshold . You need to travel from vertex back to vertex . There are two ways of traveling:
- Travel by car: there is a car at origin , but you can only travel by car along an edge if the weight of edge is strictly greater than the threshold .
- Walk: You can get off the car at any time and walk towards vertex 1, but the car will be left at the vertex you get off, and the car cannot be used again for the journey.
For each query, output the minimum total length of edges that you need to walk to get back home. Notice that the queries are independent, which means you cannot pick up the car left at an earlier query and use the car to continue traveling.
For some test cases, you must implement an online algorithm. This is enforced by the parameter : when , you can only decode the actual query with the knowledge of the result to the last query. When , you will get the actual query.
Input Specification
The first line of the input consists of a non-negative integer denoting the number of test cases. For each test case:
The first line consists of two non-negative integers denoting the number of vertices and the number of edges of graph .
In the following lines, each line consists of four positive integers denoting that there is an undirected edge between and . The edge has length and weight . It is guaranteed that .
In the following line, there are three non-negative integers denoting the number of queries, the coefficient , and the maximum possible threshold .
In the following lines, each line consists of two integers specifying a query:
- The actual is .
- The actual threshold is .
Here, represents the answer to the last query, and for the first query, since there are no queries before it, we initialize to . It is guaranteed that and .
Output Specification
For each query, output a line with an integer denoting the minimum length you have to walk.
Sample Input 1
1
4 3
1 2 50 1
2 3 100 2
3 4 50 1
5 0 2
3 0
2 1
4 1
3 1
3 2
Sample Output 1
0
50
200
50
150
Sample Input 2
1
5 5
1 2 1 2
2 3 1 2
4 3 1 2
5 3 1 2
1 5 2 1
4 1 3
5 1
5 2
2 0
4 0
Sample Output 2
0
2
3
1
Constraints
For all test cases, .
For all edges, .
The graph is connected.
UPDATE: there are no parallel edges or self loops.
Scoring
The subtasks are given below:
- Subtask 1 (1 point): .
- Subtask 2 (20 points): , for all edges. Depends on Subtask 1.
- Subtask 3 (9 points): , and for all edges. Depends on Subtask 1,2.
- Subtask 4 (15 points): , . For all edges, , so the graph forms a chain.
- Subtask 5 (5 points): , , and the graph is a tree. Depends on Subtask 4.
- Subtask 6 (5 points): , , and the graph is a tree.
- Subtask 7 (15 points): . Depends on Subtask 1,2,3,4,5.
- Subtask 8 (10 points): .
- Subtask 9 (10 points): . Depends on Subtask 6.
- Subtask 10 (10 points): . Depends on Subtask 6,9.
Here is a summary of what you can expect:
- Subtasks with : Subtask 1,2,3,4,5,7 satisfy , which means we don't require you to implement an online algorithm for these subtasks.
- Subtasks with for all edges: Subtask 1,2,3 satisfy for all edges.
- Subtasks where the graph is a chain: Subtask 4.
- Subtasks where the graph is a tree: Subtask 5,6.
- Subtasks where and : Subtask 1,2,4,8.
Comments