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
#include<bits/stdc++.h>
 
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define ff first
#define ss second
#define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mid ((l+r)/2)
#define midLL ((l+r)/2LL)
#define mide ((lo+hi)/2)
#define mideLL ((lo+hi)/2LL)
#define endl '\n'
#define random_shuffle shandom_ruffle
#define lowbit(x) x&(-x)
#define bits(x) __builtin_popcount(x) 
#define mins(se) (*se.begin())
#define maxs(se) (*--se.end())
#define all(x) x.begin(), x.end()
#define log2_floor(i) (i ? __builtin_clzll(1) - __builtin_clzll(i) : -1)
#define siz(cont) ((int)cont.size())
#define each(it, cont) for(auto &it : cont)
#define ein(cont) for(auto &it : cont) cin >> it;
#define mp(a, b) make_pair(a, b)

using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using pli = std::pair<ll, int>;
using pil = std::pair<int, ll>;
using vll = std::vector<pll>;
using vii = std::vector<pii>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vvl = std::vector<vl>;
using vvi = std::vector<vi>;
using vvii = std::vector<vii>;
using vli = std::vector<pli>;
using vvli = std::vector<vli>;
using vil = std::vector<pil>;
using vvil = std::vector<vil>;
using vc = std::vector<char>;
using vvc = std::vector<std::vector<char>>;
using vb = std::vector<bool>;
using vvb = std::vector<vb>;

#ifdef JDKURWE
void err() { std::cout << "\n"; fflush(stdout); }
template<class T, class... Ts>
void err(T arg, Ts &... args) {
    std::cout << arg << ' ';
    err(args...);
}
#else 
void err() {}
template<class T, class... Ts>
void err(T arg, Ts &... args) {
    err(args...);
}
#endif

using namespace std;

vi uf, roz, kol;
vvi g, kol_els;
vector<unordered_map<int, int>> kol_el;
vi q;

int find(int nf) {
    if (uf[nf] == nf) return nf;
    return uf[nf] = find(uf[nf]);
}

void un(int a, int b) {
    a = find(a); b = find(b);
    if (a == b)
        return;

    if (roz[a] < roz[b])
        swap(a, b);

    uf[b] = a;
    roz[a] += roz[b];

    if (roz[a] == siz(kol_els[kol[a]])) {
        q.pb(a);
    }
}

void merge_off(int a, int b) {
    a = find(a); b = find(b);
    if (a == b)
        return;

    if (siz(kol_el[a]) < siz(kol_el[b]))
        swap(a, b);

    for (auto [kl, el] : kol_el[b]) {
        if (kol_el[a].contains(kl)) {
            un(el, kol_el[a][kl]);
            kol_el[a][kl] = find(el);
        }
        else kol_el[a][kl] = el;
    }
    uf[b] = a;
}

void solve() {
    int n, m, k; cin >> n >> m >> k;
    uf = kol = vi(n+1);
    roz = vi(n+1, 1);
    g = vvi(n+1);
    kol_els = vvi(k+1);
    kol_el = vector<unordered_map<int, int>>(n+1);
    q.clear();

    for (int i = 1; i <= n; i++) {
        cin >> kol[i];
        kol_els[kol[i]].pb(i);
    }

    for (int i = 1; i <= n; i++)
        uf[i] = i;

    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }

    for (int i = 1; i <= n; i++) {
        for (auto it : g[i])
            if (kol[i] == kol[it])
                un(i, it);
        if (siz(kol_els[kol[i]]) == 1)
            q.pb(i);
    }

    //cout << " q : ";
    //for (auto it : q) cout << it << " ";
    //cout << endl;

    while (!q.empty()) {
        int kl_el = q.back();
        q.ppb();
        unordered_map<int, vi> pom;

        for (auto el : kol_els[kol[kl_el]])
            for (auto it : g[el])
                if (kol[find(it)])
                    if (siz(kol_els[kol[it]]) != roz[find(it)])
                        pom[kol[it]].pb(it);

        for (auto &[_, vit] : pom) {
            for (int i = 1; i < siz(vit); i++)
                un(vit[i-1], vit[i]);
            kol_el[kl_el][kol[vit[0]]] = find(vit[0]);
        }

        int old_kol = kol[kl_el];
        kol[kl_el] = 0;
        for (auto el : kol_els[old_kol])
            for (auto it : g[el])
                if (!kol[find(it)])
                    merge_off(it, el);

        //cout << "rem el: " << kl_el << " q: ";
        //for (auto it : q) cout << it << " ";
        //cout << endl;
    }

    for (int i = 1; i <= n; i++)
        if (kol[find(i)]) {
            cout << "NIE\n";
            return;
        }
    cout << "TAK\n";
}

signed main() { 
    turbo;
    int t; cin >> t;
    while (t--)
        solve();
}