Planning Excursions

Time limit: 1s Memory limit: 256MB Input: Output:

Task

Adrian is starting a new business as a travel counsellor, helping tourists to plan their trip along the Amazon River!

Along the river, there are NN boat excursions available, the ii-th of which starting at LiL_i meters from the river source and ending at RiR_i meters from the river source.
All excursions follow the water flow, in order to keep the engines turned off and ensure a full immersion in the natural environment.

Tourists are often puzzled by the huge number of available options. So, they call Adrian for suggestions! There are QQ requests from tourists. The ii-th request comes from
a tourist standing at XiX_{i} meters from the river source. That tourist wants to plan a series of trips that brings him to the farthest possible distance YiY_i from the river source,
using a tourist pass that allows to do at most ViV_{i} excursions. Furthermore, the tourist wants to do at least UiU_i excursions,
and does not want to repeat an excursion.
The chosen excursions must be perfectly consecutive: each of them must start where the previous one has ended, with the first one starting at XiX_i and the last one ending in YiY_i.
In case it is impossible to plan a series of at least UiU_i excursions, we conventionally set YiY_i to βˆ’1-1. Help Adrian answer the tourists' requests!

Input data

The input file consists of:

  • a line containing integer NN.
  • NN lines, the ii-th of which consisting of 64-bit integers LiL_{i}, RiR_{i}.
  • a line containing integer QQ.
  • QQ lines, the ii-th of which consisting of 64-bit integer XiX_{i} and integers UiU_{i}, ViV_{i}.

Output data

The output file must contain a single line consisting of the QQ integers Y0, …, YQβˆ’1Y_{0}, \, \ldots, \, Y_{Q-1}.

Constraints and clarifications

  • 1≀N≀1Β 0001 \le N \le 1 \ 000.
  • 1≀Q≀100Β 0001 \le Q \le 100 \ 000.
  • 0≀Li≀Ri≀10180 \le L_i \le R_i \le 10^{18}.
  • 0≀Xi≀10180 \le X_i \le 10^{18}.
  • 0≀Ui≀Vi≀N0 \le U_i \le V_i \le N.
# Score Constraints
1 0 Examples.
2 20 N,Q≀20N,Q \le 20, Ri,Xi≀100R_i, X_i \le 100.
3 20 Li≠LjL_i \ne L_j for each i≠ji \ne j.
4 20 Ui=ViU_i = V_i for each ii.
5 20 Ri,Xi≀1000R_i, X_i \le 1000.
6 20 No additional limitations.

Example 1

stdin

5
0 2
0 4
2 5
4 10
5 6
3
0 0 3
0 3 3
2 1 5

stdout

10 6 6

Explanation

In the first sample case, the available excursions are as follows:

The first tourist stands at X0=0X_0 = 0, and wants to perform a number of excursions from 00 to 33. The farthest he can reach is through the two excursions 0β†’4β†’100 \rightarrow 4 \rightarrow 10, thus Y0=10Y_0 = 10.
The second tourist also stands at 00, but wants to perform exactly 33 excursions: his only option is the trip 0β†’2β†’5β†’60 \rightarrow 2 \rightarrow 5 \rightarrow 6.
The last tourist stands at 22, and wants to perform a number of excursions between 11 and 55. His best option is the trip 2β†’5β†’62 \rightarrow 5 \rightarrow 6.

Example 2

stdin

5
4 4
2 4
4 4
4 7
2 4
4
2 4 4
2 5 5
1 0 2
1 1 5

stdout

7 -1 1 -1

Explanation

In the second sample case, the available excursions are as follows:

The first tourist stands at 22, and wants to perform exactly 44 excursions: his only option is the trip 2β†’4β†’4β†’4β†’72 \rightarrow 4 \rightarrow 4 \rightarrow 4 \rightarrow 7.
The second tourist also stands at 22, but wants to perform exactly 55 excursions: this is not possible, so the answer is Y1=βˆ’1Y_1 = -1.
The third tourist stands at 11, and wants to perform a number of excursions from 00 to 22. His only possibility is to perform 00 excursions, staying at Y2=1Y_2 = 1, since there are no excursions starting from 11.
The last tourist also stands at 11, but wants to perform at least one excursion, which is not possible, so the answer is Y3=βˆ’1Y_3 = -1.

Log in or sign up to be able to send submissions!