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
#pragma GCC optimize("O3,unroll-loops")
#include <iostream>
#include <vector>
#include <queue>
#include <map>

using namespace std;

const int MAXN = 100005;

int a[MAXN];
int base_parent[MAXN];
int wild_parent[MAXN];
bool is_wildcard[MAXN];
int first_node[MAXN];
bool present[MAXN];
int regions_count[MAXN];
vector<int> base_edges[MAXN];
map<int, int> wild_adj[MAXN];

queue<int> ready_queue;

// DSU dla obszarów w tym samym kolorze
int base_find(int i) {
    if (base_parent[i] == i) return i;
    return base_parent[i] = base_find(base_parent[i]);
}

// DSU dla połączonych wildcardów (odmalowanych terytoriów)
int wild_find(int i) {
    if (wild_parent[i] == i) return i;
    return wild_parent[i] = wild_find(wild_parent[i]);
}

// Łączenie dwóch obszarów tego samego koloru z wykorzystaniem Mniejsze do Większego
void merge_base(int r1, int r2) {
    r1 = base_find(r1);
    r2 = base_find(r2);
    if (r1 == r2) return;
    if (base_edges[r1].size() < base_edges[r2].size()) swap(r1, r2);
    
    base_parent[r2] = r1;
    // Przepinanie krawędzi (Small-To-Large)
    base_edges[r1].insert(base_edges[r1].end(), base_edges[r2].begin(), base_edges[r2].end());
    base_edges[r2].clear();
    base_edges[r2].shrink_to_fit();

    int col = a[r1];
    regions_count[col]--;
    // Jeśli kolor skleił się w jeden spójny kawałek, jest gotowy do ściągnięcia!
    if (regions_count[col] == 1) {
        ready_queue.push(col);
    }
}

// Łączenie dwóch sąsiadujących obszarów wildcardów
void merge_wildcard(int w1, int w2) {
    w1 = wild_find(w1);
    w2 = wild_find(w2);
    if (w1 == w2) return;
    if (wild_adj[w1].size() < wild_adj[w2].size()) swap(w1, w2);
    
    wild_parent[w2] = w1;

    // Przenoszenie mapy sąsiedztw (Small-To-Large)
    for (auto& kv : wild_adj[w2]) {
        int col = kv.first;
        int r2 = base_find(kv.second);
        if (is_wildcard[r2]) continue; // Odfiltrowanie nieaktualnych danych

        if (wild_adj[w1].count(col)) {
            int r1 = base_find(wild_adj[w1][col]);
            if (is_wildcard[r1]) {
                wild_adj[w1][col] = r2;
            } else if (r1 != r2) {
                merge_base(r1, r2); // Zderzenie tego samego koloru - łączymy bazy!
                wild_adj[w1][col] = base_find(r1);
            }
        } else {
            wild_adj[w1][col] = r2;
        }
    }
    wild_adj[w2].clear();
}

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    
    // Inicjalizacja dla przypadku testowego
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        base_parent[i] = i;
        wild_parent[i] = i;
        is_wildcard[i] = false;
        present[i] = false;
        base_edges[i].clear();
        wild_adj[i].clear();
    }
    for (int i = 1; i <= k; i++) {
        present[i] = false;
        regions_count[i] = 0;
    }
    
    for (int i = 1; i <= n; i++) {
        present[a[i]] = true;
        first_node[a[i]] = i;
    }
    
    vector<pair<int, int>> edges(m);
    for (int i = 0; i < m; i++) {
        cin >> edges[i].first >> edges[i].second;
    }
    
    // Krok 1: Wstępne łączenie węzłów tego samego koloru
    for (int i = 0; i < m; i++) {
        int u = edges[i].first;
        int v = edges[i].second;
        if (a[u] == a[v]) {
            int r1 = base_find(u);
            int r2 = base_find(v);
            if (r1 != r2) base_parent[r2] = r1;
        }
    }
    
    // Podliczanie ile spójnych obszarów ma każdy kolor
    for (int i = 1; i <= n; i++) {
        if (base_parent[i] == i) {
            regions_count[a[i]]++;
        }
    }
    
    // Krok 2: Ustawianie krawędzi między RÓŻNYMI kolorami
    for (int i = 0; i < m; i++) {
        int u = edges[i].first;
        int v = edges[i].second;
        if (a[u] != a[v]) {
            int r1 = base_find(u);
            int r2 = base_find(v);
            base_edges[r1].push_back(r2);
            base_edges[r2].push_back(r1);
        }
    }
    
    while (!ready_queue.empty()) ready_queue.pop();
    
    int total_present = 0;
    for (int c = 1; c <= k; c++) {
        if (present[c]) {
            total_present++;
            // Kolory zaczynające się w jednym kawałku są gotowe
            if (regions_count[c] == 1) {
                ready_queue.push(c);
            }
        }
    }
    
    int processed_count = 0;
    
    // Krok 3: Przetwarzanie odmalowywania (zdejmowanie z grafu)
    while (!ready_queue.empty()) {
        int c = ready_queue.front();
        ready_queue.pop();
        
        processed_count++;
        int base_root = base_find(first_node[c]);
        is_wildcard[base_root] = true;
        
        for (int nxt : base_edges[base_root]) {
            int y = base_find(nxt);
            if (y == base_root) continue;
            
            if (is_wildcard[y]) {
                merge_wildcard(base_root, y);
            } else {
                int col_y = a[y];
                int w_root = wild_find(base_root); // Zawsze pytamy o aktualny korzeń wilcarda
                if (wild_adj[w_root].count(col_y)) {
                    int existing_y = base_find(wild_adj[w_root][col_y]);
                    if (is_wildcard[existing_y]) {
                        wild_adj[w_root][col_y] = y;
                    } else if (existing_y != y) {
                        merge_base(existing_y, y);
                        wild_adj[w_root][col_y] = base_find(y);
                    }
                } else {
                    wild_adj[w_root][col_y] = y;
                }
            }
        }
    }
    
    // Jeśli zdjęliśmy wszystkie istniejące w grafie kolory, układ jest możliwy
    if (processed_count == total_present) cout << "TAK\n";
    else cout << "NIE\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    if (cin >> t) {
        while (t--) {
            solve();
        }
    }
    return 0;
}