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
209
210
211
212
213
214
215
216
#include<bits/stdc++.h>
#define VAR(i,n) __typeof(n) i = (n)
#define loop(i,j,s) for(int i=j;i<s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define SIZE(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<vector<int> > VVI;
const int INFTY=20000000;
const int MAX=100100;
const int MOD=10000000;

void coutTab(int* tab,int n){
	loop(i,0,n){
		cout<<tab[i]<<" ";
	}
	cout<<"\n";
}

template<class T> void coutVec(vector<T> tab){
	for(T t : tab){
		cout<<t<<" ";
	}
	cout<<"\n";
}
//------------------------------------------
int n,m,k;
VVI G;
VVI GC;
int a[MAX];
bool visCreateGC[MAX];
int compId[MAX]; // which GC node does original vertex belong to
int c[MAX]; //color of vertex in GC from a
int idCreateGC=0;
int N;

void coutG(VVI &G){
	loop(i,0,n){
		ps(i);ps(": ");
		coutVec(G[i]);
	}
	pln("");
}
void coutGC(){
	loop(i,0,N){
		ps(i);ps(": ");
		for(int a:GC[i]){
			ps(a);
		}
		pln("");
	}
	pln("");
}


void buildGC(){
	idCreateGC=0;
	loop(i,0,n) visCreateGC[i]=false;
	idCreateGC=0;
	loop(i,0,n){
		if(!visCreateGC[i]){
			c[idCreateGC]=a[i];
			queue<int> bfs;
			bfs.push(i);
			visCreateGC[i]=true;
			compId[i]=idCreateGC;
			while(!bfs.empty()){
				int v=bfs.front(); bfs.pop();
				for(int u: G[v]){
					if(!visCreateGC[u] && a[u]==a[v]){
						visCreateGC[u]=true;
						compId[u]=idCreateGC;
						bfs.push(u);
					}
				}
			}
			idCreateGC++;
		}
	}
	loop(i,0,n){
		for(int j: G[i]){
			if(compId[i]!=compId[j]){
				GC[compId[i]].pb(compId[j]);
				GC[compId[j]].pb(compId[i]);
			}
		}
	}
	loop(i,0,idCreateGC){
		sort(ALL(GC[i]));
		GC[i].erase(unique(ALL(GC[i])),GC[i].end());
	}
}

bool solve(){
	cin>>n>>m>>k;
	loop(i,0,n) cin>>a[i];
	G = VVI(n);
	GC = VVI(n);
	int u,v;
	loop(i,0,m){
		cin>>u>>v;
		G[u-1].pb(v-1);
		G[v-1].pb(u-1);
	}
	buildGC();
	N=idCreateGC;

	// comp[col] = number of GC components of that color
	VI comp(k+1,0);
	loop(i,0,N) comp[c[i]]++;

	VI parC(N), szC(N,1);
	loop(i,0,N) parC[i]=i;
	auto findC=[&](int x){ while(parC[x]!=x){ parC[x]=parC[parC[x]]; x=parC[x]; } return x; };
	auto uniteC=[&](int x, int y) -> bool {
		x=findC(x); y=findC(y);
		if(x==y) return false;
		if(szC[x]<szC[y]) swap(x,y);
		parC[y]=x; szC[x]+=szC[y];
		return true;
	};

	// Peeled DSU: only peeled GC-nodes
	VI parP(N), szP(N,1);
	loop(i,0,N) parP[i]=i;
	// colorRep[P][d] = a d-GC-node adjacent to peeled super-node P
	vector<map<int,int>> colorRep(N);

	auto findP=[&](int x){ while(parP[x]!=x){ parP[x]=parP[parP[x]]; x=parP[x]; } return x; };

	vector<bool> peeled(k+1,false);
	queue<int> q;

	auto checkAdd=[&](int col){
		if(comp[col]==1 && !peeled[col]) q.push(col);
	};

	auto uniteP=[&](int x, int y){
		x=findP(x); y=findP(y);
		if(x==y) return;
		if(colorRep[x].size()<colorRep[y].size()) swap(x,y);
		parP[y]=x; szP[x]+=szP[y];
		for(auto&[col,rep]:colorRep[y]){
			if(colorRep[x].count(col)){
				if(uniteC(colorRep[x][col], rep)){
					comp[col]--;
					checkAdd(col);
				}
			} else {
				colorRep[x][col]=rep;
			}
		}
		colorRep[y].clear();
	};

	vector<VI> colorNodes(k+1);
	loop(i,0,N) colorNodes[c[i]].pb(i);

	loop(col,1,k+1) if(comp[col]<=1) q.push(col);

	int peeledCount=0;
	while(!q.empty()){
		int col=q.front(); q.pop();
		if(peeled[col]) continue;
		peeled[col]=true;
		peeledCount++;

		// Step 1: unite col-nodes with peeled-color GC-neighbors in peeled DSU
		for(int node:colorNodes[col])
			for(int nb:GC[node])
				if(peeled[c[nb]])
					uniteP(node, nb);

		// Step 2: register unpeeled neighbors at their peeled super-node
		for(int node:colorNodes[col]){
			int P=findP(node);
			for(int nb:GC[node]){
				int d=c[nb];
				if(!peeled[d]){
					if(colorRep[P].count(d)){
						if(uniteC(colorRep[P][d], nb)){
							comp[d]--;
							checkAdd(d);
						}
					} else {
						colorRep[P][d]=nb;
					}
				}
			}
		}
	}

	return peeledCount==k;
}

int main(){
	ios_base::sync_with_stdio(0);
	int t;
	cin>>t;
	while(t--) {
		if(solve()) pln("TAK");
		else pln("NIE");
	}
}