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
#include <bits/stdc++.h>

using namespace std;

constexpr int MAX_N = 1e5, MAX_K = 1e5;

int t, n, m, k;
int a[MAX_N + 1];
int u, v;

vector<int> V[MAX_N + 1];
bool visited[MAX_N + 1];

int rep[MAX_N + 1];
unordered_map<int, int> cnt[MAX_N + 1];

vector<int> a_inv[MAX_K + 1];
bool used[MAX_K + 1];

queue<int> to_merge;

void reset() {
    for (int i = 1; i <= n; ++i) {
        V[i].clear();
        visited[i] = false;

        rep[i] = i;
        cnt[i].clear();
    }

    for (int i = 1; i <= k; ++i) {
        a_inv[i].clear();
        used[i] = false;
    }

    while (!to_merge.empty()) {
        to_merge.pop();
    }
}

int find_set(int v) {
    if (rep[v] == v) {
        return v;
    }

    return rep[v] = find_set(rep[v]);
}

void union_sets(int u, int v) {
    u = find_set(u);
    v = find_set(v);

    if (u != v) {
        if (cnt[u].size() < cnt[v].size()) {
            swap(u, v);
        }

        rep[v] = u;

        for (auto p : cnt[v]) {
            cnt[u][p.first] += p.second;

            if (cnt[u][p.first] == (int)a_inv[p.first].size() && !used[p.first]) {
                used[p.first] = true;
                to_merge.push(p.first);
            }
        }
    }
}

void dfs(int v = 1) {
    visited[v] = true;

    for (int u : V[v]) {
        if (a[u] == a[v]) {
            union_sets(u, v);
        }

        if (!visited[u]) {
            dfs(u);
        }
    }
}

void merge(int x) {
    for (int v : a_inv[x]) {
        for (int u : V[v]) {
            union_sets(u, v);
        }
    }

    a_inv[x].clear(); // means done
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> t;

    while (t--) {
        cin >> n >> m >> k;

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

            a_inv[a[i]].push_back(i);
            cnt[i][a[i]] = 1;
        }
        
        for (int i = 1; i <= m; ++i) {
            cin >> u >> v;
            
            V[u].push_back(v);
            V[v].push_back(u);
        }

        dfs();

        for (int i = 1; i <= n; ++i) {
            for (auto p : cnt[i]) {
                if (p.second == (int)a_inv[p.first].size() && !used[p.first]) {
                    used[p.first] = true;
                    to_merge.push(p.first);
                }
            }
        }

        while (!to_merge.empty()) {
            merge(to_merge.front());
            to_merge.pop();
        }

        bool possible = true;

        for (int i = 1; i <= k; ++i) {
            if (!a_inv[i].empty()) {
                possible = false;
                break;
            }
        }

        cout << (possible ? "TAK\n" : "NIE\n");
    }
}