This page is still under construction.

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

Lollobrigida

Time limit1sMemory limit128 MB

Summary
Given a multiset of block heights, decide whether the blocks can be arranged so the sequence alternates up and down at every position.
Level

Medium5 of 10

Topics
Greedy, Sorting, Math, Combinatorics
Solved
No attempts yet

Problem

A testing track in a hovercraft factory is built by joining standard blocks of different heights in a row. A perfectly built track is called a lollobrigida: in it, no two neighbouring blocks have equal height, and no three consecutive blocks have heights that only increase or only decrease.

More formally, let h1,h2,…,hnh_1, h_2, \ldots, h_n be the sequence of block heights along a track. The track is a lollobrigida if for every 1≤i≤n−21 \le i \le n-2 one of the following holds:

  • hi<hi+1h_i < h_{i+1} and hi+1>hi+2h_{i+1} > h_{i+2}, or
  • hi>hi+1h_i > h_{i+1} and hi+1<hi+2h_{i+1} < h_{i+2}.

For example, blocks with heights 3,3,3,5,23, 3, 3, 5, 2 cannot form a lollobrigida: in any arrangement two blocks of height 33 end up side by side, or one of the monotone triples (2,3,5)(2, 3, 5) or (5,3,2)(5, 3, 2) appears, and neither is allowed.

Another set of blocks, however, can form a lollobrigida, for instance (3,2,5,2,3,1)(3, 2, 5, 2, 3, 1); other lollobrigidas can be built from the same set as well.

You are given several sets of blocks. For each set, decide whether its blocks can be rearranged into a lollobrigida.

Input

The first line contains the number of data sets dd (1≤d≤1001 \le d \le 100).

The dd sets follow one after another. The first line of each set contains the number of blocks nn (3≤n≤1,000,0003 \le n \le 1{,}000{,}000). Each of the next nn lines contains one integer hh (1≤h≤1091 \le h \le 10^9), the height of a block.

Output

Print exactly dd lines, one per data set. On the ii-th line print the answer for the ii-th set:

  • TAK (Polish for "yes") if a lollobrigida can be built from that set,
  • NIE (Polish for "no") otherwise.

Examples1

  1. Example 1

    Input
    2
    5
    3
    3
    3
    5
    2
    6
    3
    3
    1
    5
    2
    2
    
    Expected output
    NIE
    TAK