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
#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 9;
vector<int> przedstawiciele[maxn], D[maxn];
int tab[maxn], spojne[maxn], rep_kolory[maxn], ranga_kolory[maxn];
int rep_inter[maxn];
bool czy_inter[maxn];
queue<int> Q;
map<int, int> M[maxn];

int Find_kolor(int v) {
	if(v==rep_kolory[v]) {return v;}
	return rep_kolory[v]=Find_kolor(rep_kolory[v]);
}	

void Union_kolor(int v, int w) {
	v=Find_kolor(v);
	w=Find_kolor(w);
	if(v==w) {return;}
	spojne[tab[v]]--;
	if(spojne[tab[v]] == 1) {
		Q.push(tab[v]);
	}	
	if(ranga_kolory[v] < ranga_kolory[w]) {swap(v, w);}
	if(ranga_kolory[v] == ranga_kolory[w]) {ranga_kolory[v]++;}
	rep_kolory[w]=v;
}

int Find_inter(int v) {
	if(v==rep_inter[v]) {return v;}
	return rep_inter[v]=Find_inter(rep_inter[v]);
}	

void dodaj(int gdzie, int kogo) {
	gdzie=Find_inter(gdzie);
	if(M[gdzie][tab[kogo]] == 0) {
		M[gdzie][tab[kogo]] = kogo;
	}	
	else {
		Union_kolor(M[gdzie][tab[kogo]], kogo);
	}	
}	

void Union_inter(int v, int w) {
	v=Find_inter(v);
	w=Find_inter(w);
	if(v==w){return;}
	if(M[v].size()<M[w].size()){swap(v, w);}
	for(auto para : M[w]) {
		dodaj(v, para.second);
	}	
	rep_inter[w]=v;
}	

void solve() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, m, k, a, b, usuwamy;
	cin >> n >> m >> k;
	for(int i=1; i<=n; i++) {
		cin >> tab[i];
		przedstawiciele[tab[i]].push_back(i);
		spojne[tab[i]]++;
		rep_kolory[i]=i;
		ranga_kolory[i]=i;
		
		rep_inter[i]=i;
	}	
	for(int i=1; i<=m; i++) {
		cin >> a >> b;
		D[a].push_back(b);
		D[b].push_back(a);
		if(tab[a] == tab[b]) {
			Union_kolor(a, b);
		}	
	}	
	int wynik=k;
	for(int i=1; i<=k; i++) {
		if(spojne[i]==0) {
			wynik--;
		}	
		else if(przedstawiciele[i].size()==1){
			Q.push(i);
		}	
	}	
	while(!Q.empty()) {
		usuwamy = Q.front();
		//cout << usuwamy << endl;
		Q.pop();
		wynik--;
		for(auto nowy : przedstawiciele[usuwamy]) {
			czy_inter[nowy]=1;
			for(auto sasiad : D[nowy]) {
				//cout << "::::::::::PRZEDSTAWICIEL 3 to: " << Find_inter(nowy) << endl;
				if(czy_inter[sasiad] or tab[sasiad] == tab[nowy]) {
					//polacz nowy i sasiad
					//cout << "------------LACZYMY: " << nowy << " I: " << sasiad << endl;
					Union_inter(nowy, sasiad);
				}	
				else {
					//cout << "DODAJEMY DO: " << nowy << " ZIOMECZKA: " << sasiad << endl;
					//cout << "A PRZEDSTAWICIEL nowego to: " << Find_inter(nowy) << endl;
					dodaj(nowy, sasiad);
				}	
				//cout << "SPOJNE[1] = " << spojne[1] << endl;
			}	
		}	
	}	
	if(wynik==0) {
		cout << "TAK" << '\n';
	}	
	else {
		cout << "NIE" << '\n';
	}
	
	for(int i=0; i<=n+1; i++) {
		tab[i]=0;
		rep_kolory[i]=0;
		ranga_kolory[i]=0;
		rep_inter[i]=0;
		czy_inter[i]=0;
		M[i].clear();
		D[i].clear();
	}	
		
	for(int i=0; i<=k+1; i++) {
		spojne[i]=0;
		przedstawiciele[i].clear();
	}	
}	

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while(t--) {
		solve();
	}	
}	
/*
1
6 6 2
2 1 2 1 1 2 
1 2
3 1
4 1
3 5
6 3
6 5
*/