문제
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 slingshots (), where the th slingshot is described by three integers , , and , specifying that this slingshot can shoot manure from position to position in only total units of time.
FJ has piles of manure to transport (). The th such pile needs to be moved from position to position . Hauling manure with the tractor for a distance of takes 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 manure piles, please help FJ determine the minimum possible transportation time, given that FJ can use up to one slingshot during the process.
입력
The first line of input contains and . The next lines each describe a single slingshot in terms of integers , , and (). The final lines describe piles of manure that need to be moved, in terms of integers and .
출력
Print lines of output, one for each manure pile, indicating the minimum time needed to transport it.
예제 입력 1
2 3
0 10 1
13 8 2
1 12
5 2
20 7
예제 출력 1
4
3
10
코드를 제출하려면 로그인이 필요합니다.
로그인