Under construction

Still being built. Things may change

Desert

Time limit2sMemory limit512 MB

Summary
Assign levels from 1 to 10^9 to n points so that the listed points of each measurement are lower than the other points in its range, or report a contradiction.
Level

Medium7 of 10

Topics
Graph, Topological sort, Union-find, Intervals
Solved
No attempts yet

Problem

The route from Bythad to Bytara crosses the sands of the Great Byteotian Desert. The trip is hard, especially because there are only ss wells along the route. Knowing that the prosperity of his country depends on its communication routes, the ruler of Byteotia has decided to have new wells dug along this route. The distance from Bythad to Bytara is n+1n+1 bytemiles. At each integer multiple of a bytemile from Bythad, there is already a well, or a new one can be dug. The lower the water level is, the harder and more costly it is to dig a well.

The royal geologist Byteasar surveys the options. He has mm measurements from the satellite network. The satellite data do not give the water levels directly. Each measurement covers a contiguous fragment of the route and says only that, at certain points of that fragment, the water level is lower than at the remaining points. The water level at every point is between 1 and 10910^9 bytemeters below the surface. Help Byteasar find a possible water level at every point along the route. The satellite data may be contradictory.

Input

The first line contains three integers nn, ss, and mm separated by single spaces (1≤s≤n≤100 0001 \le s \le n \le 100\,000, 1≤m≤200 0001 \le m \le 200\,000), where nn is the number of wells, ss is the number of wells already dug, and mm is the number of satellite measurements.

Each of the next ss lines contains two integers pip_i and did_i (1≤pi≤n1 \le p_i \le n, 1≤di≤1091 \le d_i \le 10^9). Well ii is pip_i bytemiles from Bythad, and the water level in it is did_i bytemeters below the surface. The wells are given in increasing order of pip_i.

Each of the next mm lines contains integers lil_i, rir_i, kik_i, followed by kik_i integers x1,…,xkix_1, \ldots, x_{k_i} (1≤li<ri≤n1 \le l_i < r_i \le n, 1≤ki≤ri−li1 \le k_i \le r_i - l_i, li≤x1<⋯<xki≤ril_i \le x_1 < \cdots < x_{k_i} \le r_i). The measurement says that the water level at each of the points x1,…,xkix_1, \ldots, x_{k_i} is strictly lower than the water level at each of the remaining integer points in {li,…,ri}∖{x1,…,xki}\{l_i, \ldots, r_i\} \setminus \{x_1, \ldots, x_{k_i}\}. The sum of all kik_i does not exceed 200 000200\,000.

Tests worth 60% of the score satisfy n,m≤1000n, m \le 1000. Tests worth 30% of the score have a sum of kik_i at most 10001000.

Output

If the measurements are contradictory, print NIE on the first line. Otherwise, print TAK on the first line and, on the second line, print nn integers from 1 to 10910^9: the water levels at the successive points along the route from Bythad. If several solutions exist, print any one of them.

Examples3

  1. Example 1

    Input
    5 2 2
    2 7
    5 3
    1 4 2 2 3
    4 5 1 4
    
    Expected output
    TAK
    6 7 1000000000 6 3
    
  2. Example 2

    Input
    3 2 1
    2 3
    3 5
    1 3 1 2
    
    Expected output
    NIE
    
  3. Example 3

    Input
    2 1 1
    1 1000000000
    1 2 1 2
    
    Expected output
    NIE