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
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;

int t;
int n, m, k;
int col[100009];
int roots[100009];
int rep[100009];
vector<vector<int>> graph;
vector<pair<int, int>> e;
vector<map<int, int>> mp;
bool us[100009];
int crep[100009];
int siz[100009];
queue<int> q;

int find(int v) {
	if (rep[v] == v) return v;
	else return rep[v] = find(rep[v]);
}

void unin(int u, int v) {
	if (find(u) == find(v)) return;
	int fu = find(u);
	int fv = find(v);

	if (siz[fu] < siz[fv]) swap(fu, fv);

	rep[fv] = fu;
	siz[fu] += siz[fv];
}

void unin2(int u, int v) {
	if (find(u) == find(v)) return;
	int fu = find(u);
	int fv = find(v);

	if (graph[fv].size() > graph[fu].size()) swap(fu, fv);

	rep[fv] = fu;

	for (auto i : graph[fv]) {
		graph[fu].push_back(i);
	}

	roots[col[u]]--;
	if (roots[col[u]] == 1) q.push(col[u]);
}

void unin3(int u, int v) {
	int fu = find(u);
	int fv = find(v);

	if (mp[fv].size() > mp[fu].size()) swap(fu, fv);

	rep[fv] = fu;

	for (auto i : mp[fv]) {
		if (mp[fu][i.f]) unin2(mp[fu][i.f], i.s);
		else mp[fu][i.f] = i.s;
	}

}

int main() {
	cin.tie(0)->sync_with_stdio(0);

	cin >> t;

	while (t--) {
		cin >> n >> m >> k;

		fill(col, col + n + 9, 0);
		fill(roots, roots + k + 9, 0);
		fill(us, us + k + 9, 0);
		e.clear();
		graph.assign(n + 9, {});
		mp.assign(n + 9, {});
		fill(siz, siz + n + 9, 1);

		for (int i = 0; i <= n + 2; i++) rep[i] = i;


		for (int i = 1; i <= n; i++) {
			cin >> col[i];
			roots[col[i]]++;
			crep[col[i]] = i;
		}

		for (int i = 1; i <= m; i++) {
			int a, b;
			cin >> a >> b;
			e.push_back({a, b});

			if (col[a] == col[b] and find(a) != find(b)) {
				unin(a, b);
				roots[col[a]]--;
			}
		}

		for (auto i : e) {
			if (col[i.f] != col[i.s]) {
				graph[find(i.f)].push_back(i.s);
				graph[find(i.s)].push_back(i.f);
			}
		}



		for (int i = 1; i <= k; i++) {
			if (roots[i] == 1) q.push(i);
		}

		while (q.size()) {
			auto tp = q.front();
			q.pop();
			if (us[tp]) continue;
			us[tp] = 1;
			roots[tp]--;

			int v = find(crep[tp]);
			int vrep = v;

			for (auto i : graph[v]) {
				int iu  = find(i);
				if (iu == vrep) continue;

				int c = col[iu];

				if (us[c]) {
					unin3(iu, vrep);
					vrep = find(vrep);
				}

				else {
					if (mp[vrep][c]) unin2(iu, mp[vrep][c]);
					else mp[vrep][c] = iu;
				}
			}
		}

		for (int i = 1; i <= k; i++) {
			if (roots[i]) {
				cout << "NIE\n";
				goto sex;
			}
		}

		cout << "TAK\n";
sex:

	}

}