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
// yolo
#include <bits/stdc++.h>

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

using namespace std;

#ifdef LOCAL
template<typename A, typename B>
auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

using i64 = long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
using vi = vector<int>;
using vll = vector<i64>;

struct Testcase {
	int N, M, K;

	vector<vi> graph;
	vi vtx_to_party_id;
	vi party_num_components;
	queue<int> party_queue;
	vector<vi> party_cities;
	vector<bool> is_party_resolved;

	vi fu_pnt;
	vector<map<int, int>> fu_neighbors;

	int FuFind(int v) {
		if (fu_pnt[v] == v) {
			return v;
		} else {
			return (fu_pnt[v] = FuFind(fu_pnt[v]));
		}
	}

	void Input() {
		cin >> N >> M >> K;
		vtx_to_party_id.resize(N);
		for (int &x : vtx_to_party_id) {
			cin >> x;
			--x;
		}
		graph.resize(N);
		for (int i = 0; i < M; ++i) {
			int u, v;
			cin >> u >> v;
			--u; --v;
			graph[u].push_back(v);
			graph[v].push_back(u);
		}
	}

	void UnionVerts(int vtx_u, int vtx_v) {
		debug("UnionVerts", vtx_u, vtx_v);
		queue<pii> pairs_to_union;
		pairs_to_union.emplace(vtx_u, vtx_v);

		while (!pairs_to_union.empty()) {
			const auto [u, v] = pairs_to_union.front();
			pairs_to_union.pop();
			int id1 = FuFind(u);
			int id2 = FuFind(v);
			if (id1 != id2) {
				if (SZ(fu_neighbors[id1]) > SZ(fu_neighbors[id2])) {
					swap(id1, id2);
				}
				fu_pnt[id1] = id2;
				
				const int party_id = vtx_to_party_id[u];
				if (party_id == vtx_to_party_id[v]) {
					--party_num_components[party_id];
					if (party_num_components[party_id] == 1) {
						party_queue.push(party_id);
					}
				}

				auto &small_neigh_map = fu_neighbors[id1];
				auto &large_neigh_map = fu_neighbors[id2];
				for (const auto &[neigh_party_id, neigh_vtx] : small_neigh_map) {
					const auto [iter, is_unique] = large_neigh_map.emplace(neigh_party_id, neigh_vtx);
					if (!is_unique) {
						pairs_to_union.emplace(neigh_vtx, iter->second);
					}
				}
				small_neigh_map.clear();
			}
		}
	}

	void InitFU() {
		fu_pnt.resize(N);
		iota(ALL(fu_pnt), 0);
		fu_neighbors.resize(N);
		party_num_components.resize(K);
		party_cities.resize(K);
		is_party_resolved.resize(K);
		for (int v = 0; v < N; ++v) {
			const int pid = vtx_to_party_id[v];
			party_cities[pid].push_back(v);
			++party_num_components[pid];
		}

		for (int pid = 0; pid < K; ++pid) {
			if (party_num_components[pid] == 1) {
				party_queue.push(pid);
			}
		}

		for (int u = 0; u < N; ++u) {
			for (int v : graph[u]) {
				if (vtx_to_party_id[u] == vtx_to_party_id[v]) {
					UnionVerts(u, v);
				}
			}
		}
	}

	void ResolveParty(int pid) {
		debug("ResolveParty", pid);
		assert(!is_party_resolved[pid]);
		is_party_resolved[pid] = true;

		for (int v : party_cities[pid]) {
			for (int s : graph[v]) {
				const int other_pid = vtx_to_party_id[s];
				if (is_party_resolved[other_pid]) {
					UnionVerts(v, s);
				}
			}
		}

		const int some_party_vtx = party_cities[pid][0];
		const int fu_loc = FuFind(some_party_vtx);

		for (int v : party_cities[pid]) {
			for (int s : graph[v]) {
				const int other_pid = vtx_to_party_id[s];
				if (!is_party_resolved[other_pid]) {
					const auto [iter, is_unique] = fu_neighbors[fu_loc].emplace(other_pid, s);
					if (!is_unique) {
						UnionVerts(s, iter->second);
					}
				}
			}
		}
	}

	void Run() {
		Input();
		InitFU();

		while (!party_queue.empty()) {
			const int pid = party_queue.front();
			party_queue.pop();
			ResolveParty(pid);
		}

		const bool is_ok = (*max_element(ALL(party_num_components)) <= 1);
		cout << (is_ok ? "TAK\n" : "NIE\n");
	}
};

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(14);
	cerr << fixed << setprecision(6);

	int t;
	cin >> t;
	while (t--) {
		Testcase{}.Run();
	}
}