This page is still under construction.

Parts of this page are still being built. What you see may change.

Mosaic

Time limit1sMemory limit1024 MB

Summary
Given up to 100000 rectangles in a fixed order, answer range queries asking for any two pieces in the range that share no matching side length.
Level

Hard8 of 10

Topics
Array, Sorting, Divide and conquer, Prefix sum
Solved
No attempts yet

Problem

All pieces of the ABBYY magnetic mosaic are rectangular. Two pieces can be joined only if at least one of their dimensions matches: length, width, or both. Magnetic pieces cannot be rotated or flipped. A pair of mosaic pieces that cannot be joined is called inharmonious. For example, the pair 1×21 \times 2 and 2×32 \times 3 is inharmonious, while the pairs 2×32 \times 3 and 1×31 \times 3, and 2×32 \times 3 and 2×32 \times 3, are harmonious.

The designers at ABBYY laid out all mosaic pieces in a row without joining them. A set is several consecutive pieces in this row. They chose several sets of pieces that they want to keep for an installation. For each such set they need to determine whether it contains an inharmonious pair of pieces.

Write a program that, for various sets of consecutive mosaic pieces, determines the indices of the pieces forming an inharmonious pair, or reports that no such pair exists.

Input

The first line of the input file contains a single number NN, the number of pieces in the mosaic (2≤N≤100 0002 \le N \le 100\,000). The next NN lines contain two integers AiA_i and BiB_i each, the length and width of the ii-th mosaic piece (1≤Ai,Bi≤1091 \le A_i, B_i \le 10^9, 1≤i≤N1 \le i \le N).

The (N+2)(N+2)-th line contains a single integer KK, the number of sets in which the indices of two inharmonious pieces must be determined (1≤K≤100 0001 \le K \le 100\,000). The next KK lines contain pairs of integers N1N_1 and N2N_2, the indices of the first and last pieces of a set in which two inharmonious mosaic pieces must be found (1≤N1<N2≤N1 \le N_1 < N_2 \le N).

Output

The output file must contain KK lines, each with two space-separated numbers: the indices of the mosaic pieces forming an inharmonious pair in the corresponding set. If there are several answers, output any of them. If a set has no inharmonious pair, output 0 0 on the corresponding line.

Examples1

  1. Example 1

    Input
    4
    2 2
    1 2
    1 3
    2 3
    2
    2 3
    2 4
    
    Expected output
    0 0
    4 2