Take what you can, Give Nothing Back

0

0 votes
Problem

Captain Jack Sparrow has stolen the center portion of the Navigational Charts and gained knowledge about locations of various islands around Tortuga, all of which have treasures. By doing a few calculations, he has figured out the distances of the islands from Tortuga and the resources required to reach each island.

Jack favours an island A over another island B if the distance of A is less than or equal to B and the resources spent in going to A are less than or equal to B. An island cannot be preferred over itself.

Consider a set S of islands. An island A is termed "Extra-Ordinary S" if there is no island in set S that is favourable over A. "Exceptional S" is the maximal subset of S such that every school in "Exceptional S" is "Extra-Ordinary S".

Jack only wants to travel a maximum distance maxDist to collect treasures. He also does not want to spend more than maxRes resources in the journey to the island. Since he has a reputation to maintain among the pirates, he has to spend atleast minRes resources on any journey. If he spends less than minRes resources, he may lose his status among the pirates.

For multiple queries, calculate the number of islands that are considered in "Exceptional S" considering the above three conditions. Also calculate the amount of resources that are spent in travelling to these islands.

Input

The first line contains an integer N, the number of islands discovered by Jack from the Navigational Charts.

The next N lines contain two integers D and R, denoting the distance of the island and the resources spent in travelling to that island.

The next line contains an integer Q, the number of queries.

The next Q lines contain three integers, the values of maxDist, minRes and maxRes.

Output

Output Q lines containing the results for all the queries. In each line, output two integers - the number of islands in "Exceptional S" and the resources spent in the journey to those islands.

Constraints

1 <= N <= 100000

1 <= Q <= 100000

1 <= D, R, maxDist, minRes, maxRes <= 1000000000

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?