Under construction

Still being built. Things may change

Epidemic

Time limit9sMemory limit1024 MB

Summary
Process contacts and tests online; for each query, say whether only quarantined people can be infected, or name a person who still might be.
Level

Medium7 of 10

Topics
Union-find, Implementation, Graph
Solved
No attempts yet

Problem

Byteland has just shut its borders. The reason is a new strain of bytebacterium detected in the neighbouring countries. Research shows that the new strain is highly contagious and does not trigger any response from the Bytelanders' immune systems. Once a person is infected, that person stays infected for life (or at least until an effective cure is discovered) and keeps infecting others.

To contain the spread, the Bytelandish government introduced far-reaching restrictions and launched the National Invigilation System, which monitors all social interactions in the country. The government announced that the restrictions will be lifted only when it is certain that nobody, except people under quarantine, is infected. You are the data science expert in charge of analyzing the system's data and determining when the restrictions can be lifted.

There are nn people in Byteland. At the start, any one of them may be healthy or infected. After the borders close, kk events take place one after another.

A social contact means a group of people meets. If any of them is infected, all of them become infected and stay infected. This is the only way to catch the disease. Touching infected surfaces cannot spread it.

A negative test means a given person takes a test and gets a negative result.

A positive test means a given person takes a test and gets a positive result. That person is immediately put under quarantine and takes part in no social contacts from then on. A later test may still be performed on them. The author has sent an enquiry to the Ministry of Health about why anyone would test a person whose result is already known to be positive, but the ministry's reply has been delayed by three months because of the complex nature of the case.

A query means the health minister asks whether the restrictions can be abolished. In other words, can the information gathered so far prove that nobody except the quarantined people can be infected? If someone might still be infected, you must name such a person, following the guidelines in the Output section.

Your solution must be an online algorithm. You must answer each query right after reading it, without reading any later part of the input.

Input

Correct interpretation of the input depends on the current value of the variable shiftshift. At the beginning of each test case, shiftshift is 0. Its later values depend on your program's answers.

The decoding function is defined as decode(p) = ((p - 1 + shift) mod n) + 1, where 1≤p≤n1 \le p \le n.

The first line contains the number of test cases zz (1≤z≤10001 \le z \le 1000). Each test case starts with two integers nn and kk (1≤n≤5000001 \le n \le 500000, 1≤k≤10000001 \le k \le 1000000), where nn is the number of people and kk is the number of events. People are numbered from 1 to nn.

The next kk lines describe the events in one of the following forms.

K c p_1 ... p_c (2≤c≤n2 \le c \le n) is a social contact of cc people with identifiers decode(p_1), ..., decode(p_c). The pip_i are distinct.

N p means person decode(p) takes a test with a negative result.

P p means person decode(p) takes a test with a positive result. This person is quarantined from that moment on, and any later test on this person gives a positive result.

Q p is the ministry's inquiry with starting value decode(p).

The sums of nn and kk over all test cases do not exceed 500000 and 1000000. The sum of cc over all queries does not exceed 1000000.

Output

For each query (type Q), print one line.

If it can be proven that nobody except the quarantined people can be infected, print TAK. The variable shiftshift then becomes 0.

Otherwise, print NIE followed by one integer. Let ss be the starting value decode(p). The integer is the identifier of the first person in the sequence s,s+1,...,n,1,2,...,s−1s, s+1, ..., n, 1, 2, ..., s-1 who might still be infected and is not quarantined. This number becomes the new value of shiftshift.

The shift does not change on events of type K, N, or P. For each test case, the positive and negative results always describe a consistent scenario, meaning at least one set of initially infected people makes the events free of contradictions.

Examples1

  1. Example 1

    Input
    1
    6 14
    K 3 3 4 5
    K 2 6 5
    N 3
    Q 3
    P 1
    K 2 6 2
    P 6
    Q 4
    P 6
    K 2 1 3
    N 3
    Q 4
    N 2
    Q 1
    
    Expected output
    NIE 5
    NIE 1
    TAK
    TAK