OI XXVI - rem

// https://szkopul.edu.pl/problemset/problem/xNjwUvwdHQoQTFBrmyG8vD1O/site/?key=statement

#include <bits/stdc++.h>

// using namespace std;

// #define GARY_DBG
#define GARY_LIB

// #define int long long

constexpr int sizik = 150 * 1001;

#define ar std::array
#define pr std::pair

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(const std::pair<int, int>& p) const {
        static const uint64_t FIXED_RANDOM = std::chrono::high_resolution_clock::now().time_since_epoch().count();
        uint64_t combined = (static_cast<uint64_t>(static_cast<uint32_t>(p.first)) << 32) | static_cast<uint32_t>(p.second);
        return splitmix64(combined + FIXED_RANDOM);
    }
};

namespace two_sat {

using std::pair;
using std::vector;

inline int lit_to_node(int lit) {
    int v = abs(lit) - 1;
    return 2 * v + (lit < 0 ? 1 : 0);
}

inline int neg_node(int node) {
    return node ^ 1;
}

pair<bool, vector<char>> solve(const vector<pair<int, int>>& cond, int N) {
    const int nodes = 2 * N;
    vector<vector<int>> g(nodes), gr(nodes);

    g.reserve(nodes);
    gr.reserve(nodes);

    for (auto& cl : cond) {
        int a = cl.first, b = cl.second;
        int na = lit_to_node(a), nb = lit_to_node(b);
        int n_na = neg_node(na), n_nb = neg_node(nb);

        g[n_na].push_back(nb);
        gr[nb].push_back(n_na);

        g[n_nb].push_back(na);
        gr[na].push_back(n_nb);
    }

    vector<char> used(nodes, 0);
    vector<int> order;
    order.reserve(nodes);

    vector<pair<int, int>> stack;
    for (int s = 0; s < nodes; ++s) {
        if (used[s]) continue;
        stack.clear();
        stack.emplace_back(s, 0);
        used[s] = 1;
        while (!stack.empty()) {
            int v = stack.back().first;
            int& it = stack.back().second;
            if (it < (int)g[v].size()) {
                int to = g[v][it++];
                if (!used[to]) {
                    used[to] = 1;
                    stack.emplace_back(to, 0);
                }
            } else {
                order.push_back(v);
                stack.pop_back();
            }
        }
    }

    vector<int> comp(nodes, -1);
    int cid = 0;
    vector<int> st2;
    st2.reserve(nodes);
    for (int i = (int)order.size() - 1; i >= 0; --i) {
        int v = order[i];
        if (comp[v] != -1) continue;
        st2.clear();
        st2.push_back(v);
        comp[v] = cid;
        while (!st2.empty()) {
            int u = st2.back();
            st2.pop_back();
            for (int to : gr[u]) {
                if (comp[to] == -1) {
                    comp[to] = cid;
                    st2.push_back(to);
                }
            }
        }
        ++cid;
    }

    for (int i = 0; i < N; ++i) {
        if (comp[2 * i] == comp[2 * i ^ 1]) {
            return {false, {}};
        }
    }

    vector<char> assign(N, 0);
    for (int i = 0; i < N; ++i) {
        assign[i] = (comp[2 * i] > comp[2 * i ^ 1]) ? 1 : 0;
    }
    return {true, assign};
}
} // namespace two_sat

int a[sizik];

std::unordered_map<std::pair<int, int>, std::vector<int>, custom_hash> mp;

void solve() {
    int n, k;
    std::cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
    }

    mp.clear();

    for (int i = 1; i <= n - 1; i++) {
        mp[{a[i], a[i + 1]}].push_back(i);
    }

    std::vector<std::pair<int, int>> clauses;

    int ziomeczki = n - 1;

    clauses.push_back({1, 1});
    for (int i = 2; i < n; i++) {
        clauses.push_back({i - 1, i});
    }
    clauses.push_back({n - 1, n - 1});

    for (const auto& [_, vec] : mp) {
        if (vec.size() <= 1) continue;
        if (vec.size() <= 2) {
            clauses.push_back({-vec[0], -vec[1]});
            continue;
        }

        int w = (int)vec.size();

        clauses.push_back({-vec[0], ziomeczki + 1});

        for (int i = 2; i <= w - 1; i++) {
            clauses.push_back({-vec[i - 1], ziomeczki + i});
            clauses.push_back({-(ziomeczki + i - 1), ziomeczki + i});
        }

        for (int i = 2; i <= w; i++) {
            clauses.push_back({-vec[i - 1], -(ziomeczki + i - 1)});
        }

        ziomeczki += w - 1;
    }

    auto res = two_sat::solve(clauses, ziomeczki);
    if (!res.first) {
        std::cout << "NIE\n";
    } else {
        std::cout << "TAK\n";
    }
}

int32_t main() {
#ifndef GARY_DBG
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
#endif

    int t = 1;
    std::cin >> t;

    for (; t > 0; t--) {
        solve();
    }

    return 0;
}