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
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#define pr pair
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pll pr<ll,ll>
#define pii pr<int,int>
#define piii pr<int,pii>
using namespace std;
struct node
{
	int ls,rs;
	int v;
} nd[8000006];
int z;
int nwd()
{
	nd[z]=nd[0];
	return z++;
}
void st(int i,int l,int v,int L,int R)
{
	node&x=nd[i];
	if(L==R-1)
	{
		x.v=v;
		return;
	}
	int m=L+R>>1;
	if(l<m)
	{
		if(x.ls==0) x.ls=nwd();
		st(x.ls,l,v,L,m);
	}
	else
	{
		if(x.rs==0) x.rs=nwd();
		st(x.rs,l,v,m,R);
	}
}
queue<int> tdl;
int h[100005];
int f[100005];
map<int,int> al[100005];
vector<int> hd[100005];
int t;
int res[100005];
int ff(int x)
{
	return f[x]==x?x:f[x]=ff(f[x]);
}
void mg(int a,int b)
{
	a=ff(a);
	b=ff(b);
	if(a==b) return;
	f[a]=b;
	res[h[a]]--;
	if(res[h[a]]==1) tdl.push(h[a]);
}
int mg(int i,int j,int L,int R)
{
	int rt=nwd();
	node&x=nd[i],&y=nd[j];
	node&r=nd[rt];
	r=x;
	if(L==R-1)
	{
		mg(x.v,y.v);
		r.v=x.v;
		return rt;
	}
	if(x.ls&&y.ls) r.ls=mg(x.ls,y.ls,L,L+R>>1);
	else r.ls=x.ls+y.ls;
	if(x.rs&&y.rs) r.rs=mg(x.rs,y.rs,L+R>>1,R);
	else r.rs=x.rs+y.rs;
	return rt;
}
int rot[100005];
bool vd[100005];
int g[100005];
int fg(int x)
{
	return g[x]==x?x:g[x]=fg(g[x]);
}
map<int,int>*np()
{
	al[t].clear();
	return al+t++;
}
int n,m,k;
void mgv(int a,int b)
{
	a=fg(a);
	b=fg(b);
	if(a==b) return;
	g[a]=b;
	rot[b]=mg(rot[b],rot[a],0,k);
}
vector<int> e[100005];
void sl()
{
	z=1;
	cin>>n>>m>>k;
	for(int i=0;i<k;i++) res[i]=0,hd[i].clear();
	for(int i=0;i<n;i++) cin>>h[i],h[i]--,res[h[i]]++,hd[h[i]].push_back(i),f[i]=i,g[i]=i;
	for(int i=0;i<n;i++) rot[i]=nwd(),st(rot[i],h[i],i,0,k),vd[i]=0,e[i].clear();
	for(int i=0;i<k;i++) if(res[i]==1) tdl.push(i);
	while(m--)
	{
		int u,v;
		cin>>u>>v;
		u--;
		v--;
		e[u].push_back(v);
		e[v].push_back(u);
		if(h[u]==h[v]) mg(u,v);
	}
	while(tdl.size())
	{
		int c=tdl.front();
		tdl.pop();
		for(int i:hd[c]) vd[i]=1;
		for(int i:hd[c])
		{
			for(int j:e[i]) if(vd[j]) mgv(i,j);
			else rot[fg(i)]=mg(rot[fg(i)],rot[j],0,k);
		}
	}
	for(int i=0;i<n;i++) if(!vd[i])
	{
		cout<<"NIE\n";
		return;
	}
	cout<<"TAK\n";
}
int main()
{
	ios_base::sync_with_stdio(0);
	int tc;
	cin>>tc;
	while(tc--) sl();
	return 0;
}/*1
5 5 3
1 2 1 1 3
1 2
2 3
3 4
4 5
5 1
*/