Under construction

Still being built. Things may change

Tournament

Interview

Time limit1sMemory limit1024 MB

Summary
Given a bracket order A and k proposed orders, check whether each proposed order makes every pair of players meet in the same round as A.
Level

Medium5 of 10

Topics
Array, Implementation
Solved
No attempts yet

Problem

nn players (nn is a power of two) play in a single-elimination tournament. Each player receives a distinct number ii with 1≤i≤n1 \le i \le n. The bracket is built as follows. The player numbers are drawn one by one, and the ii-th drawn number is aia_i. In the first round, player a1a_1 plays a2a_2, a3a_3 plays a4a_4, and so on, until n/2n/2 winners remain. In the second round, the first winner plays the second, the third plays the fourth, and so on, until n/4n/4 winners remain. This continues until one tournament champion is left. Some players find certain schedules unsuitable, so they propose replacing the sequence AA with another sequence BjB_j. The remaining players want the tournament to stay identical to the original. That means every pair of players must be able to meet in the same round, provided both keep winning until then.

Input

The first line contains two integers nn and kk, the number of players and the number of proposed sequences. The second line contains the sequence AA, where the ii-th number is aia_i. The next kk lines contain the sequences BjB_j (1≤j≤k1 \le j \le k). On line j+2j+2, the ii-th number bj,ib_{j,i} is the proposed value for aia_i (1≤i≤n1 \le i \le n).

Output

Print kk lines, each containing one word: TAIP if the tournament defined by BjB_j is identical to the tournament defined by AA, or NE otherwise.

Constraints

4≤n≤2164 \le n \le 2^{16}, nn is a power of two, 1≤k≤201 \le k \le 20, 1≤ai,bj,i≤n1 \le a_i, b_{j,i} \le n. For u≠vu \ne v, au≠ava_u \ne a_v and bj,u≠bj,vb_{j,u} \ne b_{j,v}.

Examples2

  1. Example 1

    Input
    4 2
    2 1 4 3
    3 4 2 1
    2 4 1 3
    
    Expected output
    TAIP
    NE
    
  2. Example 2

    Input
    8 1
    1 2 3 4 5 6 7 8
    1 2 5 6 3 4 7 8
    
    Expected output
    NE