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
#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

struct UnionFind {
    vector<int> parent, rank;

    UnionFind(int n) : parent(n), rank(n, 0) {
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
    }
    int find(int x) {
        while (parent[x] != x) {
            x = parent[x] = parent[parent[x]];
        }
        return x;
    }
    bool unite(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) {
            return false;
        }
        if (rank[a] < rank[b]) {
            swap(a, b);
        }
        parent[b] = a;
        if (rank[a] == rank[b]) {
            rank[a]++;
        }
        return true;
    }
};

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

    vector<int> parties(n);
    vector<pair<int, int>> roads(m);

    for (int i = 0; i < n; i++) {
        cin >> parties[i];
    }

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        roads[i] = {a - 1, b - 1};
    }

    UnionFind unionFind(n);

    bool changed = true;
    while (changed) {
        changed = false;

        for (auto [a, b] : roads) {
            int componentA = unionFind.find(a);
            int componentB = unionFind.find(b);
            if (componentA != componentB && parties[componentA] == parties[componentB]) {
                unionFind.unite(componentA, componentB);
                changed = true;
            }
        }

        unordered_map<int, unordered_map<int, int>> zeroFirstNeighbor;
        for (auto [a, b] : roads) {
            int componentA = unionFind.find(a);
            int componentB = unionFind.find(b);
            if (componentA == componentB) {
                continue;
            }

            auto mergeNeighborsOfZero = [&](int zeroComponent, int neighborComponent) {
                int party = parties[neighborComponent];
                auto it = zeroFirstNeighbor[zeroComponent].find(party);
                if (it == zeroFirstNeighbor[zeroComponent].end()) {
                    zeroFirstNeighbor[zeroComponent][party] = neighborComponent;
                } else {
                    int prev = unionFind.find(it->second);
                    int curr = unionFind.find(neighborComponent);
                    if (prev != curr) {
                        unionFind.unite(prev, curr);
                        parties[unionFind.find(prev)] = party;
                        it->second = unionFind.find(prev);
                        changed = true;
                    }
                }
                };
            if (parties[componentA] == 0 && parties[componentB] != 0) {
                mergeNeighborsOfZero(componentA, componentB);
            }
            if (parties[componentB] == 0 && parties[componentA] != 0) {
                mergeNeighborsOfZero(componentB, componentA);
            }
        }

        vector<int> partyCount(k + 1, 0);
        for (int i = 0; i < n; i++) {
            if (unionFind.find(i) == i && parties[i] != 0) {
                partyCount[parties[i]]++;
            }
        }
        for (int i = 0; i < n; i++) {
            if (unionFind.find(i) == i && parties[i] != 0 && partyCount[parties[i]] == 1) {
                parties[i] = 0;
                changed = true;
            }
        }
    }

    int componentsCount = 0;
    bool allZero = true;
    for (int i = 0; i < n; i++) {
        if (unionFind.find(i) == i) {
            componentsCount++;
            if (parties[i] != 0) {
                allZero = false;
            }
        }
    }
    if (componentsCount == 1 || allZero) {
        cout << "TAK" << endl;
    } else {
        cout << "NIE" << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        voivodeship();
    }
}