This page is still under construction.

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

The Fibonacci Game

Time limit1sMemory limit128 MB

Summary
Determine whether the first player wins a game that erases Fibonacci words only from the right end of a given a/b string.
Level

Hard8 of 10

Topics
String matching, Dynamic programming, Game theory, String
Solved
No attempts yet

Problem

Define the Fibonacci words fif_i as follows: f1=af_1 = a, f2=bf_2 = b, and for i≥3i \ge 3, fi=fi−2fi−1f_i = f_{i-2} f_{i-1}. In other words, the Fibonacci word with index ii is obtained by concatenating the words with indices i−2i-2 and i−1i-1, in that order. For example, f3=abf_3 = ab, f4=babf_4 = bab, and f5=abbabf_5 = abbab.

The Fibonacci game is played by two players on a single word made only of the letters a and b (this word is called the board). The players alternate moves; a single move consists of erasing any one Fibonacci word from the right end of the board. A player who cannot make a move loses. For a given word, determine whether the player who moves first can always win, assuming both players play optimally.

Write a program that reads the number of test cases and, for each one, reads the board on which the game is played from standard input, determines whether the first player can always win, and prints the answer to standard output.

Input

The first line contains one integer tt (1≤t≤101 \le t \le 10), the number of test cases. Each of the next tt lines describes one test case. Each line contains a positive integer nn (1≤n≤100 0001 \le n \le 100\,000), followed by a single space and then a string of the letters a and b of length nn (with no spaces between the letters). This string is the board on which the game is played.

Output

Print tt lines. Each line contains the answer for one test case, in the same order as the input. Print TAK if the first player can always win, and NIE otherwise.

Examples3

  1. Example 1

    Input
    2
    5 aaaaa
    10 abbababbaa
    
    Expected output
    TAK
    NIE
    
  2. Example 2

    Input
    3
    1 a
    1 b
    2 ab
    
    Expected output
    TAK
    TAK
    TAK
    
  3. Example 3

    Input
    6
    5 aaaaa
    6 aaaaaa
    5 bbbbb
    6 bbbbbb
    1 a
    2 aa
    
    Expected output
    TAK
    NIE
    TAK
    NIE
    TAK
    NIE