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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const bool DEBUG = 0;
const int N = 1e5 + 3, tNum = 76;
vector<int> G[N];
vector<int> vertByCol[N];
int col[N];
int repr[N];
int reprM[N];
map<int, int> neigh[N];
int totalColAmount[N];
int groupSize[N];
int height[N];
vector<int> ColToProcces;
vector<bool> used;

int Find(int x){
    if(x == repr[x]) return x;
    return repr[x] = Find(repr[x]);
}

void Union(int x, int y){
    x = Find(x);
    y = Find(y);
    if(x == y) return;
    if(height[x] < height[y]){
        swap(x, y);
    }
    if(height[x] == height[y]) height[x]++;
    repr[y] = x;
    groupSize[x] += groupSize[y];
    if(groupSize[x] == totalColAmount[col[x]]) ColToProcces.push_back(col[x]);
}

int FindM(int x){
    if(x == reprM[x]) return x;
    return reprM[x] = FindM(reprM[x]);
}

void UnionM(int x, int y){
    x = FindM(x);
    y = FindM(y);
    if(x == y) return;
    if(neigh[x].size() < neigh[y].size()){
        swap(x, y);
    }
    reprM[y] = x;
    for(auto p : neigh[y]){
        if(neigh[x][p.first] != 0){
            Union(p.second, neigh[x][p.first]);
        }
        neigh[x][p.first] = p.second;
    }
    neigh[y].clear();
}

bool solve(int n, int k){
    for(int i = 1 ; i <= n ; i++){
        for(auto v : G[i]){
            if(col[i] == col[v]){
                Union(i, v);
            }
        }
    }
    for(int i = 1 ; i <= k ; i++){
        if(vertByCol[i].size() == 1){
            ColToProcces.push_back(i);
        }else if(vertByCol[i].size() == 0){
            used[i] = 1;
        }
    }
    while(!ColToProcces.empty()){
        int c = ColToProcces.back();
        ColToProcces.pop_back();
        used[c] = 1;
        for(auto v : vertByCol[c]){
            for(auto u : G[v]){
                if(!used[col[u]]){
                    if(neigh[v][col[u]] != 0) Union(neigh[v][col[u]], u);
                    neigh[v][col[u]] = u;
                }
            }
        }
        for(auto v : vertByCol[c]){
            for(auto u : G[v]){
                if(used[col[u]]) UnionM(v, u);
            }
        }
    }
    for(int i = 1 ; i <= k ; i++){
        if(!used[i]) return 0;
    }
    return 1;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int n, m, t, k, u, v;
    cin >> t;
    for(int h = 1 ; h <= t ; h++){
        cin >> n >> m >> k;
        used.resize(k + 2);
        if(DEBUG && h == tNum) cout << n << " " << m << " " << k << '\n';
        for(int i = 1 ; i <= n ; i++){
            cin >> col[i];
            if(DEBUG && h == tNum) cout << col[i] << " ";
            vertByCol[col[i]].push_back(i);
            repr[i] = i;
            reprM[i] = i;
            totalColAmount[col[i]]++;
            groupSize[i] = 1;
        }
        if(DEBUG && h == tNum) cout << '\n';
        for(int i = 0 ; i < m ; i++){
            cin >> u >> v;
            if(DEBUG && h == tNum) cout << u << " " << v << '\n';
            G[u].push_back(v);
            G[v].push_back(u);
        }
        if(solve(n, k)){
            cout << "TAK\n";
        }else{
            cout << "NIE\n";
        }
        for(int i = 1 ; i <= n ; i++){
            G[i].clear();
            col[i] = 0;
            repr[i] = 0;
            height[i] = 0;
            groupSize[i] = 0;
            reprM[i] = 0;
            neigh[i].clear();
        }
        for(int i = 1 ; i <= k ; i++){
            vertByCol[i].clear();
            totalColAmount[i] = 0;
        }
        used.clear();
    }
}