Under construction

Still being built. Things may change

Intersection Construction

Interview

Time limit2sMemory limit1024 MB

Summary
Given N intervals and Q queries [l, r], find the fewest intervals whose intersection is exactly [l, r], or print -1 if no selection works.
Level

Medium4 of 10

Topics
Array, Hash map, Implementation
Solved
No attempts yet

Problem

An interval [l,r][l,r] is the set of all real numbers that are at least ll and at most rr.

NN intervals are given. Write a program that answers QQ queries of the following form.

  • For the given ll and rr, can you choose one or more of the intervals so that their intersection is exactly [l,r][l,r]? If so, what is the minimum number of intervals you must choose?

Input

The first line contains the number of intervals NN. (1≤N≤3000001 \leq N \leq 300000)

Each of the next NN lines contains two integers lil_i and rir_i, separated by a space, which describe the interval [li,ri][l_i,r_i]. (0≤li<ri≤1060 \leq l_i < r_i \leq 10^6)

The next line contains the number of queries QQ. (1≤Q≤3000001 \leq Q \leq 300000)

Each of the next QQ lines contains two integers ll and rr, separated by a space. (0≤l<r≤1060 \leq l < r \leq 10^6)

Output

For each query, print one line. If no selection of intervals has intersection exactly [l,r][l,r], print −1-1. Otherwise, print the minimum number of intervals that must be selected.

Examples1

  1. Example 1

    Input
    3
    0 10
    2 6
    4 8
    3
    4 6
    2 8
    1 9
    
    Expected output
    2
    -1
    -1