Hide

Problem E
Pork Barrel

Winning the election was simpler than you expected: it was enough to promise to finally build a good quality, country-wide road infrastructure, of course without crippling the budget…Your happiness did not last long, however: it seems, that the citizens have found a way to actually hold you accountable for your promise!

There are $n$ major cities in your country. The Ministry of Transport has prepared a detailed map, outlining $m$ possible highway connections, together with their costs. The Quality Assurance Committee will not let you build a highway cheaper than $l$, and the National Spendings Regulatory Committee will not let you build a highway more expensive than $h$. To claim a “country-wide” network, you have to connect (possibly indirectly) as many pairs of cities, as it is possible within these two constraints. You have to find the cheapest way to do it, and you have to find it quickly! Of all networks that meet the constraints and connect the most pairs of cities, compute the cost of the cheapest one.

To make things worse, both committees are heavily influenced by your angry competitors: each time you publish your hard-prepared plan, they immediately change their rulings $l$ and $h$, and you are forced to start from scratch.

Input

The first line of input contains the number of test cases $T$ ($1 \leq T \leq 10$). The descriptions of the test cases follow:

The first line of each test case contains integers n and m ($1 \leq n \leq 1000$; $0 \leq m \leq 100\, 000$)—the number of cities in the country, and of possible direct connections, respectively.

Each of the following $m$ lines contains three integers $x$, $y$, $w$ ($1 \leq x \neq y \leq n$; $1 \leq w \leq 1\, 000\, 000$), denoting that the cities $x$ and $y$ can be connected by a bidirectional highway at cost $w$. There might be many ways to connect a single pair of cities.

The following line contains an integer $q$ ($1 \leq q \leq 1\, 000\, 000$)—the number of rulings of the committees. Each of the following $q$ lines contains two integers. The first of the lines contains the initial rulings $l_1$, $h_1$, given directly. The rest of the rulings are encoded. The numbers in the $j$-th of the lines for $j > 1$ are $l_ j + c_{j-1}$ and $h_ j + c_{j-1}$, where $l_ j$ and $h_ j$ are the actual rulings and $c_{j-1}$ is the correct answer for the preceding rulings $l_{j-1}$ and $h_{j-1}$.

All rulings satisfy $1 \leq l_ j \leq h_ j \leq 1\, 000\, 000$.

The total number of lines in all test cases is at most $1\, 100\, 003$.

Output

For each test case, output $q$ lines, one for each ruling. In the $j$-th of them, output the minimal cost $c_ j$ of building a highway network which adheres to the committees’ constraints, and creates the maximum number of connected pairs of cities.

Sample Input 1 Sample Output 1
1
5 7
1 2 2
2 3 4
3 4 3
4 5 1
5 1 3
2 5 4
1 4 5
5
1 2
4 7
11 12
11 13
18 19
3
9
8
14
13

Please log in to submit a solution to this problem

Log in