For Gamers. By Gamers.
Contest: Educational Codeforces Round 125 (Rated for Div. 2)
Difficulty rating: 2000
Tags: binary search, brute force, greedy, math, sortings

Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.

Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.

There are $$$n$$$ types of units. Every unit type has three parameters:

- $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type;
- $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second;
- $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type.

Monocarp has to face $$$m$$$ monsters. Every monster has two parameters:

- $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second;
- $$$H_j$$$ — the amount of health the $$$j$$$-th monster has.

Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.

For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster.

Input format:
The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle.

The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$).

The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face.

The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$).

Output format:
Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$.

Constraints:
Time limit: 4.0 s
Memory limit: 256.0 MB

Note:
Consider the first monster of the first example.

Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.

Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.

Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.

Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type.

Example input:
3 10
3 4 6
5 5 5
10 3 4
3
8 3
5 4
10 15

Example output:
5 3 -1

Example input:
5 15
14 10 3
9 2 2
10 4 3
7 3 5
4 3 1
6
11 2
1 1
4 7
2 1
1 14
3 3

Example output:
14 4 14 4 7 7

Editorial:
Imagine you are fighting the $$$j$$$-th monster, and you fixed the type of units $$$i$$$ and their amount $$$x$$$.
What's the win condition? $$$\frac{H_j}{d_i \cdot x} < \frac{h_i}{D_j}$$$. Rewrite it as $$$H_j \cdot D_j < d_i \cdot x \cdot h_i$$$. Notice how we only care about $$$d \cdot h$$$ for both the units and the monster, but not about $$$d$$$ and $$$h$$$ on their own.
Let's call $$$d \cdot h \cdot x$$$ and $$$D \cdot H$$$ the power of the squad and the monster.
You can see that for each cost $$$c$$$ we can only leave one unit type of that price that has the largest value of $$$d \cdot h$$$. Let's call it $$$\mathit{bst}_c$$$.
Now let's learn to determine the maximum power we can obtain for cost exactly $$$c$$$. We can iterate over the cost $$$c$$$ of one unit and the count $$$x$$$ of units in the squad. Since $$$c \cdot x$$$ should not exceed $$$C$$$, that will take $$$\frac{C}{1} + \frac{C}{2} + \dots + \frac{C}{C} = O(C \log C)$$$. Propagate $$$\mathit{bst}_c$$$ to be the maximum power for cost exactly $$$c$$$.
We have the knowledge about cost exactly $$$c$$$, but we actually want no more than $$$c$$$. Calculate prefix maximums over $$$\mathit{bst}$$$ — that will be the maximum power we can obtain with no more than $$$c$$$ coins.
For each monster, we just have to find the smallest $$$c$$$ such that $$$\mathit{bst}_c > D \cdot H$$$. Since the array is monotone, we can use binary search.
Overall complexity: $$$O(n + (C + m) \log C)$$$.