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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define LL long long
#define int LL
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define VI vector<int>
#define PII pair<int,int>
#define LD long double

template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream &os, vector<T> V){
  os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
} 

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}


#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

struct FU {
    VI parent;
    vector<map<int,int>> party_count;
    VI& party_total;
    vector<bool>& in_queue;
    VI& ready;

    FU(int n, VI& color, VI& party_total, vector<bool>& in_queue, VI& ready)
        : party_total(party_total), in_queue(in_queue), ready(ready) {
        parent.resize(n);
        iota(ALL(parent), 0);
        party_count.resize(n);
        REP(i, n) party_count[i][color[i]] = 1;
    }

    int fi(int x) {
        while (parent[x] != x) {
            parent[x] = parent[parent[x]];
            x = parent[x];
        }
        return x;
    }

    void un(int a, int b) {
        a = fi(a);
        b = fi(b);
        if (a == b) return;
        if (SZ(party_count[a]) < SZ(party_count[b])) swap(a, b);
        parent[b] = a;
        for (auto& [p, c] : party_count[b]) {
            party_count[a][p] += c;
            if (party_count[a][p] == party_total[p] && !in_queue[p]) {
                in_queue[p] = true;
                ready.PB(p);
            }
        }
        party_count[b].clear();
    }
};

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

    VI color(n);
    REP(i, n) {
        cin >> color[i];
        color[i]--;
    }

    vector<VI> adj(n);
    REP(i, m) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        adj[u].PB(v);
        adj[v].PB(u);
    }

    vector<VI> party_nodes(k);
    VI party_total(k, 0);
    REP(i, n) {
        party_nodes[color[i]].PB(i);
        party_total[color[i]]++;
    }

    VI q;
    vector<bool> in_queue(k, false);

    FU fu(n, color, party_total, in_queue, q);

    REP(u, n) {
        for (int v : adj[u]) {
            if (color[u] == color[v]) {
                fu.un(u, v);
            }
        }
    }

    REP(p, k) {
        if (party_total[p] == 0) {
            in_queue[p] = true;
            continue;
        }
        if (in_queue[p]) continue;
        int root = fu.fi(party_nodes[p][0]);
        if (fu.party_count[root][p] == party_total[p]) {
            in_queue[p] = true;
            q.PB(p);
        }
    }

    REP(qi, SZ(q)) {
        int p = q[qi];
        for (int u : party_nodes[p]) {
            for (int v : adj[u]) {
                fu.un(u, v);
            }
        }
    }

    // Check if all parties were processed
    bool ok = true;
    REP(p, k) {
        if (!in_queue[p]) {
            ok = false;
            break;
        }
    }
    if(ok){
        cout << "TAK\n";
    } else{
        cout << "NIE\n";
    }
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) solve();
}