Hazel is starting a tree plantation. She likes to plan ahead and has prepared a planting schedule for trees.
The trees will be grown elsewhere before being moved to Hazel’s plantation. Each tree will be planted there in a specific year , when it will already be meters tall. Once planted, it remains in the plantation and grows exactly one meter per year. Thus, in any year , the tree is meters tall.
To plan her future sales, Hazel considers potential purchase orders for trees. Each order requests trees, each at least meters tall. An order can be fulfilled in a given year if the plantation has at least planted trees that are each at least meters tall.
The orders are independent: no trees are actually removed from the plantation, and each order is considered as if it were the only one. For each order, determine the earliest year in which it could be fulfilled.
The first line contains an integer () representing the number of trees in the planting schedule.
Each of the next lines describes a tree with two integers and (), indicating respectively its planting year and its height in meters when planted.
The next line contains an integer () representing the number of purchase orders.
Each of the next lines describes a purchase order with two integers () and (), indicating respectively the number of trees requested and their minimum height in meters.
For each purchase order in the input, output a line with an integer indicating the earliest year in which it could be fulfilled. It can be shown that an answer exists for each purchase order.
3 1 5 6 10 3 2 3 2 4 1 11 3 1
5 7 6
For the first order (K = 2, V = 4), the first tree is already tall enough when it is planted in year 1. The third tree reaches a height of 4 meters in year 5, while the second tree has not yet been planted. Therefore, the earliest year in which the order could be fulfilled is 5.
For the second order (K = 1, V = 11), the first two trees reach a height of 11 meters in year 7, while the third tree reaches that height in year 12. Therefore, the answer is 7.
For the third order (K = 3, V = 1), every tree is tall enough from the year it is planted. However, the second tree is not in the plantation before year 6. Therefore, the answer is 6.
2 5 8 5 8 3 2 8 1 10 1 8
5 7 5
Both trees are planted in year 5, when they are already 8 meters tall. They are separate trees, so the first order (K = 2, V = 8) could be fulfilled in year 5, but not earlier.
Both trees reach a height of 10 meters in year 7, the answer to the second order (K = 1, V = 10).
The third order (K = 1, V = 8) could be fulfilled in year 5: orders are considered independently, so neither of the previous orders removes any trees.