This page is still under construction.

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

Jumpers

Time limit1sMemory limit128 MB

Summary
Decide whether a set of distinct white start cells and closed jump sequences can cover every white cell without landing on any black cell.
Level

Hard8 of 10

Topics
Graph, Number theory, Math, Greedy
Solved
No attempts yet

Problem

A cyclic tape has nn cells. Some are painted black and some white. Your task is to paint every cell black. To paint them you use jumpers: hopping robots dipped in black paint. When a jumper lands on a white cell, it repaints that cell black. A jumper may never land on a black cell. You may use any number of jumpers, and for each jumper you pick any white cell as its start; the chosen starting cells must be pairwise distinct.

Every jumper must make at least one jump and finish back on the cell it started from. A jumper paints its own starting cell only on its final jump. All jumpers are identical: they share one set of possible jumps S={s1,…,sm}S = \{s_1, \dots, s_m\}. From its current cell a jumper may perform any one of the mm moves, as long as that jump obeys the rules above. The ii-th move sends the jumper sis_i cells clockwise.

For a given tape and jumper specification, decide whether it is possible to repaint the whole tape black while respecting all of the rules above.

Input

The first line contains an integer tt (1≤t≤201 \le t \le 20), the number of test sets. The test sets follow.

Each test set begins with a line holding two integers nn and mm (1≤n≤5001 \le n \le 500, 1≤m≤n1 \le m \le n): nn is the tape length and mm is the number of possible jump lengths. The next line is a string of nn characters B and C describing the cyclic tape (B is a white cell, C is a black cell). Each of the following mm lines holds one integer sis_i (1≤si≤n1 \le s_i \le n), one of the jumper's possible jump lengths.

Output

For each test set output one line with a single word: TAK if the tape can be painted entirely black, or NIE otherwise.

Examples4

  1. Example 1

    Input
    2
    4 2
    BCBB
    1
    2
    4 1
    BCBB
    2
    
    Expected output
    TAK
    NIE
    
  2. Example 2

    Input
    1
    3 1
    CCC
    2
    
    Expected output
    TAK
    
  3. Example 3

    Input
    1
    4 1
    BCBC
    2
    
    Expected output
    TAK
    
  4. Example 4

    Input
    1
    9 1
    BBCBBCBBC
    3
    
    Expected output
    TAK