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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<class T>
using vec = vector<T>;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef KoRoVa
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif

const int max_n = 1e5 + 11, inf = 1000111222;



struct dsu {
public:
    int n;
    vector <int> p, cnt;

    inline void make_set (int v) {
        p[v] = v;
    }

    dsu (int n) : n(n) {
        p.resize(n);
        cnt.assign(n, 1);
        for (int i = 0; i < n; i++) {
            make_set(i);
        }
    }

    inline int get (int v) {
        if (p[v] == v) return v;
        return p[v] = get(p[v]); /// compressing path
    }

    inline bool unite (int a, int b) {
        a = get(a);
        b = get(b);
        if (a == b) return false;
        if (cnt[a] > cnt[b]) {
            swap(a, b);
        }
        p[a] = b;
        cnt[b] += cnt[a];
        return true;
    }
};

int a[max_n];





void test_case() {
    int n, m, k;
    cin >> n >> m >> k;
    vector <int> cnt(k);
    vector <vector <int> > have(k);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        --a[i];
        ++cnt[a[i]];
        have[a[i]].pb(i);

    }
    vector <vector <int> > edge(n);
    for (int i = 0, u, v; i < m; i++) {
        cin >> u >> v;
        --u, --v;
        edge[u].pb(v);
        edge[v].pb(u);
    }
    dsu t(n);
    queue <int> q;
    auto check = [&] (int u, int v) {
        if (t.unite(u, v)) {
            --cnt[a[v]];
            if (cnt[a[v]] == 1) {
                q.push(a[v]);
            }
        }
    };
    for (int i = 0; i < k; i++) {
        if (cnt[i] <= 1) {
            q.push(i);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int to : edge[i]) {
            if (a[i] == a[to]) {
                check(i, to);
            }
        }
    }
    int gg = 0;
    vector <int> alive(n, 1);
    vector <map <int, int> > cmp(n);
    vector <int> p(n);
    for (int i = 0; i < n; i++) {
        p[i] = i;
    }
    auto get_p = [&] (int v) {
        int V = v;
        while (p[v] != v) {
            v = p[v];
        }
        while (p[V] != V) {
            int to = p[V];
            p[V] = v;
            V = to;
        }
        return v;
    };
    auto add = [&] (int v, int to) {
        v = get_p(v);
        if (!cmp[v].count(a[to])) {
            cmp[v][a[to]] = to;
        }
        else {
            check(cmp[v][a[to]], to);
        }
    };
    auto _merge = [&] (int a, int b) {
        a = get_p(a);
        b = get_p(b);
        if (a == b) {
            return;
        }
        if (len(cmp[a]) < len(cmp[b])) {
            swap(a, b);
        }
        p[b] = a;
        for (auto &x : cmp[b]) {
            add(a, x.second);
        }
        cmp[b].clear();
    };
    while (!q.empty()) {
        int color = q.front();
        ++gg;
        q.pop();
        for (int v : have[color]) {
            alive[v] = 0;
            for (int to : edge[v]) {
                if (alive[to]) {
                    add(v, to);
                }
                else {
                    _merge(v, to);
                }
            }
        }
    }

    if (gg == k) {
        cout << "TAK\n";
    }
    else {
        cout << "NIE\n";
    }
}



int main() {
//    freopen("test.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);


    int t = 1;

     cin >> t;

    while (t--) test_case();

    return 0;
}

/*
KoRoVa!
*/