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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <queue>
#include <vector>
#include <utility>
#include <map>


int64_t fu_find(size_t p, std::vector<int64_t> &fu_parent) {
    if (fu_parent[p] == p) {
        return p;
    }
    fu_parent[p] = fu_find(fu_parent[p], fu_parent);
    return fu_parent[p];
}

int64_t fu_union(size_t u, size_t v, std::vector<int64_t> &fu_parent, std::vector<int64_t> &fu_size) {
    u = fu_find(u, fu_parent);
    v = fu_find(v, fu_parent);
    if (u == v) {
        return u;
    }
    if (fu_size[u] < fu_size[v]) {
        std::swap(u, v);
    }
    fu_size[u] += fu_size[v];
    fu_parent[v] = u;
    return u;
}

void handle_case() {
    int64_t n,m,k;
    std::cin >> n >> m >> k;
    std::vector<int64_t> who_won(n+1);
    
    std::vector<std::map<int64_t, std::vector<int64_t>>> edges(k+1);
    std::queue<std::pair<size_t, size_t>> to_merge;
    std::queue<std::pair<int64_t, int64_t>> to_merge_clipped;
    std::queue<int64_t> to_clip;
    std::vector<int64_t> number_of_unconnected_components(k+1);
    std::vector<int64_t> party_representant(k+1);
    std::vector<int64_t> clipped_fu_parent(k+1);
    std::vector<bool> party_handled(k+1, false);

    std::vector<int64_t> fu_parent(n);
    std::vector<int64_t> fu_size(n);
    for (size_t i = 0; i < clipped_fu_parent.size(); i++) {
        clipped_fu_parent[i] = i;
    }
    const auto get_clipped_party_representant = [&](int64_t v) {
        return fu_find(v, clipped_fu_parent);
    };

    for (size_t i = 0; i < fu_parent.size(); i++) {
        fu_parent[i] = i;
        fu_size[i] = 1;
    }

    const auto get_representant = [&](size_t p) {
        return fu_find(p, fu_parent);
    };

    const auto merge_points = [&](int64_t u, int64_t v) {
        return fu_union(u, v, fu_parent, fu_size);
    };
    // u -> v
    const auto add_edge = [&](int64_t u, int64_t v) {
        const auto u_party_id = who_won[u];
        const auto v_party_id = who_won[v];
        if (!edges[u_party_id].contains(v_party_id)) {
            edges[u_party_id][v_party_id] = std::vector<int64_t>();
        }
        edges[u_party_id][v_party_id].push_back(v);
    };

    for (size_t i = 0; i < n; i++) {
        std::cin >> who_won[i];
        number_of_unconnected_components[who_won[i]]++;
        party_representant[who_won[i]] = i;
    }
    for (size_t i = 0; i < m; i++) {
        int64_t u, v;
        std::cin >> u >> v;
        u--;
        v--;
        add_edge(u,v);
        add_edge(v,u);
        if (who_won[u] == who_won[v]) {
            to_merge.emplace(u,v);
        }
    }
    for (size_t i = 1; i <= k; i++) {
        if (number_of_unconnected_components[i] == 1) {
            to_clip.push(party_representant[i]);
        }
    }

    while (!to_clip.empty() || !to_merge.empty() || !to_merge_clipped.empty()) {
        while (!to_merge.empty()) {
            const auto [u, v] = to_merge.front(); // u and v are guaranteed to be of the same party
            to_merge.pop();
            const auto u_representant = get_representant(u);
            const auto v_representant = get_representant(v);
            if (u_representant == v_representant) {
                continue;
            }
            const auto party_id = who_won[u];
            number_of_unconnected_components[party_id]--;
            party_representant[party_id] = merge_points(u_representant, v_representant);
            if (number_of_unconnected_components[party_id] == 1) {
                to_clip.push(party_representant[party_id]);
            }
        }
        while (!to_clip.empty()) {
            const auto u = to_clip.front();
            to_clip.pop();
            const auto party_id = who_won[u];
            if (party_handled[party_id]) {
                continue;
            }
            party_handled[party_id] = true;
            
            for (const auto &[other_party_id, vertices] : edges[party_id]) {
                if (other_party_id == party_id) {
                    continue;
                }
                if (party_handled[other_party_id]) {
                    to_merge_clipped.emplace(party_id, other_party_id);
                    continue;
                }
                // vertices are guaranteed to be of size at least 1
                for (size_t i = 1; i < vertices.size(); i++) {
                    to_merge.emplace(vertices[0], vertices[i]);
                }
            }
        }

        while (!to_merge_clipped.empty()) {
            const auto [u_party_id, v_party_id] = to_merge_clipped.front();
            to_merge_clipped.pop();
            auto u_party_representant = get_clipped_party_representant(u_party_id);
            auto v_party_representant = get_clipped_party_representant(v_party_id);
            if (u_party_representant == v_party_representant) {
                continue;
            }

            if (edges[u_party_representant].size() < edges[v_party_representant].size()) {
                std::swap(u_party_representant, v_party_representant);
            }
            clipped_fu_parent[v_party_representant] = u_party_representant;
            for (const auto &[other_party_id, vertices] : edges[v_party_representant]) {
                if (edges[u_party_representant].contains(other_party_id)) {
                    to_merge.emplace(vertices[0], edges[u_party_representant][other_party_id][0]);
                } else {
                    std::vector<int64_t> vv;
                    vv.push_back(vertices[0]);
                    edges[u_party_representant][other_party_id] = vv;
                }
            }
            edges[v_party_representant].clear();
        }
    }
    bool is_good = true;
    for (size_t i = 0; i <= k; i++) {
        if (number_of_unconnected_components[i] > 1) {
            is_good = false;
            break;
        }
    }
    if (is_good) {
        std::cout << "TAK\n";
    } else {
        std::cout << "NIE\n";
    }
}

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    size_t t;
    std::cin >> t;
    for (size_t i = 0; i < t; i++) {
        handle_case();
    }
    return 0;
}