eolymp
bolt
Try our new interface for solving problems
Problems

Slingshot

Slingshot

One of the farming chores Farmer John dislikes the most is hauling around lots of cow manure. In order to streamline this process, he comes up with an intriguing idea: instead of hauling manure between two points in a cart behind his tractor, why not shoot it through the air with a giant manure slingshot? (indeed, what could possibly go wrong...)

Farmer John's farm is built along a single long straight road, so any location on his farm can be described simply using its position along this road (effectively a point on the number line). FJ builds n (1n105) slingshots, where the i-th slingshot is described by three integers xi, yi and ti, specifying that this slingshot can shoot manure from position xi to position yi in only ti total units of time.

FJ has m (1m105) piles of manure to transport. The j-th such pile needs to be moved from position aj to position bj. Hauling manure with the tractor for a distance of d takes d units of time. FJ is hoping to reduce this by allowing up to one use of any slingshot for transporting each pile of manure. Time FJ spends moving his tractor without manure in it does not count.

For each of the m manure piles, please help FJ determine the minimum possible transportation time, given that FJ can use up to one slingshot during the process.

Input

The first line contains n and m. The next n lines each describe a single slingshot in terms of integers xi, yi, and ti (0xi, yi, ti109). The final m lines describe piles of manure that need to be moved, in terms of integers aj and bj.

Output

Print m lines, one for each manure pile, indicating the minimum time needed to transport it.

Example

Here, the first pile of manure needs to move from position 1 to position 12. Without using an slingshot, this would take 11 units of time. However, using the first slingshot, it takes 1 unit of time to move to position 0 (the slingshot source), 1 unit of time to fling the manure through the air to land at position 10 (the slingshot destination), and then 2 units of time to move the manure to position 12. The second pile of manure is best moved without any slingshot, and the third pile of manure should be moved using the second slingshot.

Time limit 2 seconds
Memory limit 128 MiB
Input example #1
2 3
0 10 1
13 8 2
1 12
5 2
20 7
Output example #1
4
3
10
Source 2018 USACO February, Platinum