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;

#ifdef DEBUG
auto &operator<<(auto &o, pair<auto, auto> p) {
    return o << "(" << p.first << ", " << p.second <<")";
}
auto operator<<(auto &o, auto x)-> decltype(x.end(), o) {
    o << "{";int i = 0;
    for(auto e : x) o << ", "+!i++<<e;
    return o <<"}";
}
#define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x)
#else
#define debug(...) {}
#endif

#define int long long
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define fi first
#define se second
typedef pair <int, int> pii;


void dfs(int v, int org, vector <int> & ok, vector <int> &odw, vector <vector<int>> &G, vector <int> &val, int &cnt) {
	if (val[v] == org) {
		cnt++;
	}
	odw[v] = org;

	for (auto u : G[v]) {
		if (odw[u] != org) {
			if (ok[val[u]] || val[u] == org) {
				dfs(u, org, ok, odw, G, val, cnt);
			}
		}
	}
	
}


bool dir_acyclic(vector <vector <int> > & G) {
    int k = sz(G) - 1;
    vector <int> deg(k+1);

    queue <int> Q;
    for (int i=1; i<=k; ++i) {
        for (auto u : G[i]) {
            deg[u]++;
        }
    }

    for (int i=1; i<=k; ++i) {
        if (deg[i] == 0) {
            Q.push(i);
        }
    }

    int vis = 0;
    while (!Q.empty()) {
        int v = Q.front(); Q.pop();
        vis++;
        for (auto u : G[v]) {
            deg[u]--;
            if (deg[u] == 0) {
                Q.push(u);
            }
        }
    }

    return vis == k;
}


void test(int t) {
    debug("TC______________________");
    int n, m, k; cin>>n>>m>>k;

    vector <int> val(n+1);
    vector <vector <int> > G(n+1);

    vector <vector <int> > types(k+1);
	set <int> active;
	
    for (int i=1; i<=n; ++i) {
        cin>>val[i];
        types[val[i]].push_back(i);
		active.insert(val[i]);
    }

    for (int i=0; i<m; i++) {
        int a, b; cin>>a>>b;

		G[a].push_back(b);
        G[b].push_back(a);
    }

	int ok = 0;
	vector <int> al(k+1);
	for (int i=1; i<=k; ++i) {
		if (types[i].empty()) {
			al[i] = 1;
			ok++;
		}
	}

	for (int i=1; i<=k; ++i) {
		int nok = ok;
		vector <int> odw(n+1);
		for (int j=1; j<=k; ++j) {
			if (al[j]) continue;

			int cnt = 0;
			dfs(types[j][0], j, al, odw, G, val, cnt);

			if (cnt == sz(types[j])) {
				ok++;
				al[j] = 1;
			}
		}

		if (nok == ok) break;
	}

	if (ok == k) {
		cout<<"TAK\n";
	}
	else {
		cout<<"NIE\n";
	}
}

signed main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int t = 1; cin>>t;
  for (int i=0; i<t; ++i) {
    test(i);
  }
}