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
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { (( cerr << x << ", " ), ...) <<
'\n'; }
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define deb(...) 0
#endif
 
struct DSU{
    int n, k;
    vector<int> p;
    vector<set<pii>> edges;
    vector<int> color; //-1 is special
    vector<int> numberComponents;
    queue<int> starQueue;
    queue<pii> mergeQueue;

    int find(int x){
        if(x != p[x])p[x] = find(p[x]);
        return p[x];
    }

    void uni(int a, int b){
        a = find(a);
        b = find(b);
        deb("uni", a, b);
        if(a == b)return;
        if(sz(edges[a]) < sz(edges[b]))swap(a, b);
        p[b] = a;
        assert(color[a] == color[b]);
        if(color[a] == -1){//special
            //merge two special components
            for(auto [c, id] : edges[b]){
                auto it = edges[a].lower_bound({c, 0});
                if(it != edges[a].end() && it->st == c){//same color, needs merging
                    mergeQueue.push({it->nd, id});
                }else{
                    edges[a].insert({c, id});
                }
            }
            edges[b].clear();
        }else{
            numberComponents[color[a]]--;
            if(numberComponents[color[a]] == 1)starQueue.push(a);
            for(auto x : edges[b])edges[a].insert(x);
            edges[b].clear();
        }
    }

    DSU(int N, int K, vector<pii>& e, vector<int>& color1) : n(N), k(K), p(N), edges(N), 
    color(color1), numberComponents(K, 0){
        rep(i, n)p[i] = i;
        rep(i, n)numberComponents[color[i]]++;
        rep(i, n){
            if(numberComponents[color[i]] == 1)starQueue.push(i);
        }
        for(auto [u, v] : e){
            edges[u].insert({color[v], v});
            edges[v].insert({color[u], u});
        }
        for(auto [u, v] : e){
            if(color[u] == color[v])uni(u, v);
        }
    }

    bool ok(){
        while(!starQueue.empty()){
            int x = starQueue.front(); starQueue.pop();
            deb("star", x);
            x = find(x);
            color[x] = -1;
            int prevCol = -1;
            int prevId = -1;
            for(auto [c, id] : edges[x]){
                c = color[find(id)];
                if(c == -1){
                    mergeQueue.push({x, id});
                }else{
                    if(c == prevCol){
                        mergeQueue.push({prevId, id});
                    }
                    prevCol = c;
                    prevId = id;
                }
            }
            while(!mergeQueue.empty()){
                auto [a, b] = mergeQueue.front(); mergeQueue.pop();
                uni(a, b);
            }
        }
        rep(i, n){
            if(color[find(i)] != -1)return false;
        }
        return true;
    }
};

void solve(){
    int n, m, k; cin >> n >> m >> k;
    vector<pii> edges(m);
    vector<int> color(n);
    rep(i, n){
        cin >> color[i];
        color[i]--;
    }
    rep(i, m){
        int a, b; cin >> a >> b;
        a--; b--;
        edges[i] = {a, b};
    }
    DSU xd(n, k, edges, color);
    bool res = xd.ok();
    if(res){
        cout << "TAK\n";
    }else{
        cout << "NIE\n";
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(10);
    int t; cin >> t;
    while(t--)solve();
    return 0;
}