loj#P3570. 「COCI 2021.11」嫌疑人

「COCI 2021.11」嫌疑人

Description

Problem from COCI 2021/2022 Contest #2 T5「Suspects」

In a police investigation, nn suspects were identified and now it's up to the witnesses to try to find the perpetrator. The height of every suspect ii was measured, but due to the unreliability of measurement, it is known only that their height is a real number from the interval from lil_i to rir_i (inclusive). At most one of the suspects is the perpetrator, and it could be the case that none of them are.

A single lineup consists of choosing two positive integers aa and bb (1abn)(1 \le a \le b \le n), then taking the suspects a,a+1,,ba, a + 1, \ldots, b to a separate room so that the witnesses could try to identify the perpetrator. As the witnesses could be confused if two of the suspects have the same height, a lineup is allowed only if it is possible to guarantee that no two suspects will have the same height. During a lineup, the witnesses will always be able to identify the perpetrator if he is among the chosen suspects, or they will be able to tell that he is not among them.

The lead investigator is now interested in answering questions of the following form: "If I were certain that the label of the perpetrator could only be between pp and qq (pq)(p \le q), what is the minimum number of lineups needed in the worst case so that the witnesses are able to find the perpetrator, or report that he is not among the suspects?" Help the lead investigator answer qq of such questions.

Input

The first line contains a positive integer nn, the number of suspects.

The following nn lines contain two positive integers lil_i and rir_i (1liri109)(1 \le l_i \le r_i \le 10^9) which represent the possible height range of the suspect with label ii.

The next line contains a positive integer qq, the number of questions.

The following qq lines contain two positive integers pip_i and qiq_i (1piqin)(1 \le p_i \le q_i \le n) which determine a question.

Output

In qq lines print the answers to the corresponding questions: the minimum required number of lineups.

2
1 1
1 1
3
1 1
2 2
1 2

1
1
2

3
1 1
2 2
3 3
3
1 1
2 3
1 3

1
1
1

5
1 3
3 3
4 6
2 3
1 1
3
1 4
3 5
1 5

3
1
3

Scoring

In every subtask, it holds that 1n,q200 0001 \le n, q \le 200\ 000.

Subtask Points Constraints
11 1010 q=1,p1=1,q1=nq=1,p_1=1,q_1=n
22 1n5000,1q50001\le n\le 5000,1\le q\le 5000
33 2020 1n5000,1q200 0001\le n\le 5000,1\le q\le 200\ 000
44 1n200 000,1q1001\le n\le 200\ 000,1\le q\le 100
55 5050 No additional constraints.