1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <bits/stdc++.h>
#define ssize(x) int(x.size())
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define V vector
using namespace std;
typedef long long ll;
int mod = 119<<23|1;
ll infll = 2e18;
int add(int a, int b) { return a+b >= mod ? a+b-mod : a+b; }
int sub(int a, int b) { return add(mod-b, a); }
int mul(int a, int b) { return int(a*ll(b)%mod); }
int fpow(int a, int b = mod-2) {
    int res = 1;
    while(b) {
        if (b & 1) res = mul(res, a);
        b >>= 1, a = mul(a, a);
    }
    return res;
}
int divd(int a, int b) {
    assert(b != 0);
    return mul(a, fpow(b));
}

template<typename... Args>
void read(Args&... args) {
    auto read_one = [&](auto &a) {
        a = 0; int c = getchar_unlocked();
        while (c < '0' || '9' < c) c = getchar_unlocked();
        while ('0' <= c && c <= '9') a = a*10+c-'0', c = getchar_unlocked();
    };
    return (read_one(args), ...);
}

void answer() {
    V<int> rep, sz;
    V<unordered_map<int, int>> neigh;
    V<int> col, used_col;
    V<V<int>> col_vec;
    queue<int> col_q;
    auto Find = [&](auto &&me, int x) -> int {
        if (rep[x] != x) rep[x] = me(me, rep[x]);
        return rep[x];
    };
    auto Union = [&](auto &&me, int a, int b) -> int {
        // printf("%d %d: %d %d\n", a, b, col[a], col[b]);
        a = Find(Find, a), b = Find(Find, b);
        if (a == b) return -1;
        //assert(col[a] == col[b]); // wywalić to potem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! tak jak wszystkie inne asserty

        if (col[a]) {
            if (sz[a] < sz[b]) swap(a, b);
            rep[b] = a, sz[a] += sz[b];
            // printf("ret: %d\n", sz[a]);
            return sz[a];
        }

        if (ssize(neigh[a]) < ssize(neigh[b])) swap(a, b);
        for (auto [c, x] : neigh[b]) if (!used_col[c]) {
            if (neigh[a][c]) {
                if (me(me, neigh[a][c], x) == ssize(col_vec[c]))
                    col_q.emplace(c);//, assert(!used_col[c]); 
            }
            else neigh[a][c] = x;
        }
        neigh[b] = unordered_map<int, int>();
        rep[b] = a, sz[a] += sz[b];
        return -1;
    };

    int n, m, k; read(n, m, k);
    rep.resize(n+1), iota(all(rep), 0);
    neigh.resize(n+1), sz.resize(n+1, 1);
    col.resize(n+1, 0); col_vec.resize(k+1);
    used_col.resize(k+1, 0); used_col[0] = 1;

    for (int i = 1; i <= n; ++i)
        read(col[i]), col_vec[col[i]].emplace_back(i);

    V<V<int>> g(n+1);
    for (int i = 0; i < m; ++i) {
        int a, b; read(a, b);
        g[a].emplace_back(b), g[b].emplace_back(a);
        if (col[a] != col[b]) continue;
        Union(Union, a, b);
    }

    for (int i = 1; i <= k; ++i)
        if (ssize(col_vec[i]) && ssize(col_vec[i]) == sz[Find(Find, col_vec[i].back())]) col_q.emplace(i);


    auto get_elems = [&] (queue<int> q) {
        vector<int> t;
        while (ssize(q)) t.emplace_back(q.front()), q.pop();
        return t;
    };

    // printf("%d\n", ssize(col_q));
    // for (int u : get_elems(col_q)) printf("%d ", u);
    // printf("\n");
    
    while (ssize(col_q)) {
        int c = col_q.front(); col_q.pop();
        // printf("c: %d\n", c);
        // assert(c != 0);
        // assert(!used_col[c]);
        used_col[c] = 1;
        for (int x : col_vec[c]) {
            // printf("x: %d\n", x);
            rep[x] = x, col[x] = 0;
            for (int u : g[x])
                if (col[u] == 0) Union(Union, x, u);
            
            int rep_x = Find(Find, x);
            for (int u : g[x]) if (!used_col[col[u]]) {
                int cc = col[u];
                if (neigh[rep_x][cc]) {
                    if (Union(Union, neigh[rep_x][cc], u) == ssize(col_vec[cc]))
                        col_q.emplace(cc);
                }
                else neigh[rep_x][cc] = u;
            }
        }
    }

    bool all_used = 1;
    for (int i = 1; i <= k; ++i)
        if (ssize(col_vec[i]) && !used_col[i]) all_used = 0;
    
    printf(all_used ? "TAK\n" : "NIE\n");
}
int main() {
    int T = 1; scanf("%d", &T);
    for (++T; --T; ) answer();
    return 0;
}