QOJ.ac

QOJ

시간 제한: 2 s 메모리 제한: 1024 MB 총점: 100 해킹 가능 ✓

#16897. Making an Intersection

통계

An interval $[l, r]$ is defined as the set of all real numbers $x$ such that $l \le x \le r$.

You are given $N$ intervals. Write a program that answers $Q$ queries of the following type:

  • Given $l$ and $r$, is it possible to select one or more of the given intervals such that their intersection is exactly $[l, r]$? If it is possible, what is the minimum number of intervals that must be selected?

Input

The first line contains the number of intervals $N$ ($1 \le N \le 300\,000$).

The next $N$ lines each contain two space-separated integers $l_i$ and $r_i$, representing the interval $[l_i, r_i]$ ($0 \le l_i < r_i \le 10^6$).

The next line contains the number of queries $Q$ ($1 \le Q \le 300\,000$).

The next $Q$ lines each contain two space-separated integers $l$ and $r$, representing the query interval $[l, r]$ ($0 \le l < r \le 10^6$).

Output

For each query, output the minimum number of intervals required to make their intersection exactly $[l, r]$. If it is impossible, output $-1$.

Examples

Input 1

3
0 10
2 6
4 8
3
4 6
2 8
1 9

Output 1

2
-1
-1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.