Pork barrel

No attempts yetTime limit30sMemory limit256 MB

Problem

Winning the election was easier than you expected. Promising to finally build a good country-wide highway network, of course without wrecking the budget, was enough. The happiness did not last: the citizens found a way to hold you to that promise.

Your country has nn major cities. The Ministry of Transport prepared a detailed map of mm possible highway connections together with their costs. The Quality Assurance Committee does not let you build a highway cheaper than ll, and the National Spendings Regulatory Committee does not let you build a highway more expensive than hh. To claim a country-wide network, you have to connect, directly or indirectly, as many pairs of cities as these two constraints permit. Among those networks you have to find the cheapest one, 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.

It gets worse. Both committees answer to your competitors, so every time you publish a plan they change the rulings ll and hh, and you start from scratch.

Input

The first line contains the number of test cases TT. The test cases follow, each in this format.

The first line of a test case contains the number of cities nn and the number of possible direct connections mm. (1n10001 \le n \le 1\,000, 0m1000000 \le m \le 100\,000)

Each of the next mm lines contains three integers xx, yy, ww. (1x,yn1 \le x, y \le n, xyx \ne y, 1w10000001 \le w \le 1\,000\,000) The cities xx and yy can be connected by a bidirectional highway at cost ww. There might be many ways to connect a single pair of cities.

The next line contains the number of rulings of the committees qq. (1q10000001 \le q \le 1\,000\,000) Each of the next qq lines contains two integers. The first of these lines gives the initial rulings l1l_1, h1h_1 directly. The rest of the rulings are encoded. The numbers in the jj-th line for j>1j > 1 are lj+cj1l_j + c_{j-1} and hj+cj1h_j + c_{j-1}, where ljl_j and hjh_j are the actual rulings and cj1c_{j-1} is the correct answer for the preceding rulings lj1l_{j-1}, hj1h_{j-1}.

All rulings satisfy 1ljhj10000001 \le l_j \le h_j \le 1\,000\,000.

Output

For each test case, print qq lines, one per ruling. In the jj-th of them, print the minimal cost cjc_j of a highway network that adheres to the committees' constraints and creates the maximum number of connected pairs of cities.

Hint

In the first example the actual rulings are (1,2)(1, 2), (1,4)(1, 4), (2,3)(2, 3), (3,5)(3, 5) and (4,5)(4, 5). The cheapest highway networks for them consist of the connections {(1,2),(4,5)}\{(1, 2), (4, 5)\}, {(2,1),(1,5),(5,4),(4,3)}\{(2, 1), (1, 5), (5, 4), (4, 3)\}, {(1,2),(1,5),(3,4)}\{(1, 2), (1, 5), (3, 4)\}, {(1,5),(5,2),(2,3),(3,4)}\{(1, 5), (5, 2), (2, 3), (3, 4)\} and {(3,2),(2,5),(1,4)}\{(3, 2), (2, 5), (1, 4)\}, in that order.