Under construction

Still being built. Things may change

Ural Highway

Time limit2sMemory limit1024 MB

Summary
Given each stratum's horizontal span and the top-to-bottom layer order seen in each borehole, find a depth order of all strata that agrees with every borehole.
Level

Medium7 of 10

Topics
Topological sort, Intervals, Graph
Solved
No attempts yet

Problem

A new highway named Ural is planned. Its durability depends on the rock layers beneath it. A stratum is a geological body made of a single rock.

nn horizontal strata lie under the planned highway. A geological survey found the points along the highway where each stratum begins and ends. The depth order of the strata could not be determined.

Vertical boreholes were drilled at set points along the highway. Each borehole passes through several of the upper strata beneath its point. For each borehole, the order of the drilled strata from the surface downward is known. If a borehole does not reach a stratum that lies beneath its point, that stratum runs below the bottom of the borehole.

Write a program that finds a possible depth order of the strata that agrees with the data.

Input

The first line contains an integer nn, the number of strata. The strata are numbered from 11 to nn in arbitrary order. Each of the next nn lines contains integers lil_i and rir_i (0≤li<ri≤1090 \leq l_i < r_i \leq 10^9), the distances from the start of the highway to the points where the ii-th stratum begins and ends.

The next line contains an integer mm, the number of boreholes. Each of the next mm lines describes one borehole. It starts with two integers xx (0≤x≤1090 \leq x \leq 10^9) and kk (0≤k≤n0 \leq k \leq n), the distance from the start of the highway to the borehole and the number of strata found in it. It is followed by s1,s2,…,sks_1, s_2, \ldots, s_k, the stratum numbers in order from top to bottom. The boreholes are listed in increasing order of xx.

It is guaranteed that a solution exists.

Output

Print nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n, the possible depth order of the strata from top to bottom. Each stratum number must appear exactly once. Stratum pjp_j must never pass above strata p1,…,pj−1p_1, \ldots, p_{j-1} or below strata pj+1,…,pnp_{j+1}, \ldots, p_n.

If several arrangements are possible, print any of them.

Constraints

  • 1≤n,m≤1051 \leq n, m \leq 10^5.
  • The total number of strata found across all boreholes does not exceed 10610^6.

Hints

The figure in the statement matches the example.

For the example, the answer 2 3 1 4 is also correct.

The example test does not belong to subtask 1. To be accepted for judging, a solution must pass the example even if it solves only this subtask.

Examples1

  1. Example 1

    Input
    4
    1 5
    2 7
    7 10
    1 11
    3
    1 1 1
    4 1 2
    7 2 2 3
    
    Expected output
    2 1 3 4