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
// PA2026, @mjm, r3b-kam

#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <unordered_map>
using namespace std;
using lol = long long;
int nextInt() { int n; scanf("%d", &n); return n; }
lol nextLol() { lol n; scanf("%lld", &n); return n; }
inline lol myMax(lol a, lol b) { return a >= b ? a : b; }
inline int myMin(int a, int b) { return a <= b ? a : b; }

class FU {
	vector<int> p;
public:
	FU(int n) : p(n, -1) {}
	int f(int x) {
		int r = x;
		while (p[r] >= 0) r = p[r];
		while (x != r) {
			int t = p[x];
			p[x] = r;
			x = t;
		}
		return r;
	}
	bool u(int x, int y) {
		x = f(x);
		y = f(y);
		if (x == y) return false;
		if (p[x] <= p[y]) {
			p[x] += p[y];
			p[y] = x;
		} else {
			p[y] += p[x];
			p[x] = y;
		}
		return true;
	}
};

bool solve() {
	int n = nextInt();
	int m = nextInt();
	int k = nextInt(); // unused?
	vector<int> color(n);
	unordered_map<int, vector<int>> vertexListByColor;
	unordered_map<int, int> groupCountByColor;
	for (int i = 0; i < n; ++i) {
		color[i] = nextInt();
		vertexListByColor[color[i]].push_back(i);
	}
	vector<vector<int>> graph(n);
	for (int i = 0; i < m; ++i) {
		int a = nextInt() - 1;
		int b = nextInt() - 1;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	vector<int> id(n, -1);
	unordered_map<int, set<int>> idByColor;
	unordered_map<int, vector<int>> vertexById;
	queue<int> todo;
	for (int i = 0; i < n; ++i) {
		if (id[i] >= 0)
			continue;
		vector<int>& vertexList = vertexById[i];
		queue<int> q;
		q.push(i);
		id[i] = i;
		idByColor[color[i]].insert(i);
		while (!q.empty()) {
			int a = q.front();
			q.pop();
			vertexList.push_back(a);
			vector<int>& v = graph[a];
			for (int j = 0; j < v.size(); ++j) {
				int b = v[j];
				if (color[b] != color[i])
					continue;
				if (id[b] >= 0)
					continue;
				q.push(b);
				id[b] = i;
			}
		}
		if (vertexList.size() == vertexListByColor[color[i]].size())
			todo.push(i);
		++groupCountByColor[color[i]];
	}

	FU fu(n);
	int doneCount = 0;
	while (!todo.empty()) {
		++doneCount;
		int curId = todo.front();
		todo.pop();
		vector<int>& vertexList = vertexListByColor[color[curId]];
		unordered_map<int, set<int>> otoczkaIdsByColor;
		for (int i = 0; i < vertexList.size(); ++i) {
			int a = vertexList[i];
			vector<int>& listaSasiadow = graph[a];
			for (int j = 0; j < listaSasiadow.size(); ++j) {
				int b = listaSasiadow[j];
				otoczkaIdsByColor[color[b]].insert(fu.f(id[b]));
			}
		}
		for (auto it = otoczkaIdsByColor.begin(); it != otoczkaIdsByColor.end(); ++it) {
			auto& idsList = it->second;
			if (idsList.size() <= 1) continue;
			int fstId = -1;
			for (auto it2 = idsList.begin(); it2 != idsList.end(); ++it2) {
				int curId = *it2;
				if (fstId < 0) {
					fstId = curId;
					continue;
				}
				fu.u(fstId, curId);
				--groupCountByColor[color[fstId]];
			}
			if (groupCountByColor[color[fstId]] == 1)
				todo.push(fu.f(fstId));
		}
	}

	return doneCount == vertexListByColor.size();
}

int main() {
	int TC = nextInt();
	for (int tc = 0; tc < TC; ++tc) {
		bool res = solve();
		printf("%s\n", res ? "TAK" : "NIE");
	}
	return 0;
}