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
#include <bits/stdc++.h>
using namespace std;
void solve()
{
	int n,m,k;
	cin>>n>>m>>k;
	vector<int>p(n+1);
	vector<vector<int>>s(n+1),colv(k+1);
	vector<int>repc(n+1)/*rep color*/,colc(k+1)/*ile spoj color*/,repu(n+1)/*rep used*/,roz(n+1)/*roz col*/;
	vector<bool>used(n+1),ucol(k+1)/*color used*/;
	vector<map<int,int>>tcol(n+1)/*touch color*/;
	vector<bool>fff(k+1);
	for(int i=1; i<=n; i++)
	{
		cin>>p[i];
		fff[p[i]]=1;
	}
	for(int i=1; i<=n; i++)
	{
		repc[i]=i;
		repu[i]=i;
		roz[i]=1;
		colc[p[i]]++;
		colv[p[i]].push_back(i);
	}
	queue<int>q;
	function<int(int)> fndc=[&](int x)
	{
		if(x==repc[x])
			return x;
		return repc[x]=fndc(repc[x]);
	};
	function<void(int,int,int)> unic=[&](int x, int y, int c)
	{
		x=fndc(x);
		y=fndc(y);
		if(x!=y)
		{
			if(roz[x]<roz[y])
				swap(x,y);
			repc[y]=x;
			roz[x]+=roz[y];
			colc[c]--;
			if(colc[c]==1&&!ucol[c])
			{
				q.push(c);
				ucol[c]=1;
			}
		}
	};
	for(int i=1; i<=n; i++)
		if(colc[p[i]]==1)
		{
			q.push(p[i]);
			used[i]=1;
			ucol[p[i]]=1;
			
		}
	for(int i=0; i<m; i++)
	{
		int x,y;
		cin>>x>>y;
		s[x].push_back(y);
		s[y].push_back(x);
		if(p[x]==p[y])
			unic(x,y,p[x]);
	}
	function<int(int)>fndu=[&](int x)
	{
		if(x==repu[x])
			return x;
		return repu[x]=fndu(repu[x]);
	};
	function<void(int,int,int)>touch=[&](int us, int c, int x)
	{
		x=fndc(x);
		if(tcol[us].count(c))
		{
			int pm=fndc(tcol[us][c]);
			if(pm!=x)
				unic(pm,x,c);
		}
		else
			tcol[us][c]=x;
	};
	function<void(int,int)>uniu=[&](int x, int y)
	{
		x=fndu(x);
		y=fndu(y);
		if(x!=y)
		{
			if(tcol[x].size()<tcol[y].size())
				swap(x,y);
			repu[y]=x;
			for(auto& pr:tcol[y])
			{
				int pc=pr.first,ss=pr.second;
				touch(x,pc,ss);
			}
			tcol[y].clear();
		}
	};
	while(!q.empty())
	{
		auto x=q.front();
		//cout<<x<<"\n";
		q.pop();
		for(auto i:colv[x])
		{
			used[i]=1;
			for(int j:s[i])
			{
				if(ucol[p[j]])
					uniu(i,j);
				else
					touch(fndu(i),p[j],j);
			}
		}
	}
	for(int i=1; i<=k; i++)
	{
		if(fff[i]==1&&ucol[i]==0)
		{
			//cout<<i<<"\n";
			cout<<"NIE\n";
			return;
		}
	}
	cout<<"TAK\n";
	
}
int main()
{
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	cin>>t;
	while(t--)
		solve();
}