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

using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int,int>;

#define mp make_pair
#define pb push_back
#define eb emplace_back

#define rep(i,b,e) for(int i=(b); i<(e); ++i)
#define each(a,x) for(auto &a : (x))
#define all(x) (x).begin(),(x).end()
#define sz(x) int((x).size())

const int N = 1e5+5;
vector<vi> g(N);
int color[N];
int cnt_color[N];
int col_any_v[N];

struct DSU{
    vi par,cnt,cnt_marked;

    void build(int n){
        n+=2;
        par.resize(n);
        cnt.resize(n);
        cnt_marked.resize(n);
        rep(i,0,n){
            par[i] = i;
            cnt[i] = 1;
            cnt_marked[i] = 0;
        }
    }

    void Mark(int a){
        cnt_marked[a] = 1;
    }

    int Find(int a){
        if(par[a] != a) par[a] = Find(par[a]); 
        return par[a];
    }

    void Union(int a, int b){
        a = Find(a);
        b = Find(b);
        if(a==b) return;
        if(cnt[a] < cnt[b]) swap(a,b);
        cnt[a] += cnt[b];
        cnt_marked[a] += cnt_marked[b];
        par[b] = a;
    }
};


void solve(){
    int n,m,k;
    cin >> n >> m >> k;
    vector<DSU> dsu(k+1);
    rep(i,1,k+1){
        dsu[i].build(n);
    }
    rep(i,1,n+1){
        int c;
        cin >> c;
        color[i] = c;
        col_any_v[c] = i;
        cnt_color[c] += 1;
        dsu[c].Mark(i);
    }

    auto clear = [&](){
        rep(i,0,n+2){
            g[i].clear();
            cnt_color[i] = 0;
        }
    };
    
    rep(i,0,m){
        int a,b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
        if(color[a] == color[b])
            dsu[color[a]].Union(a,b);
    }

    while(true){
        // warunek końca
        bool conn = true;
        rep(c,1,k+1){
            if(cnt_color[c] == 0) continue;
            int any_v = col_any_v[c];
            int comp = dsu[c].Find(any_v);
            int cnt_marked = dsu[c].cnt_marked[comp];
            if(cnt_marked != cnt_color[c]){
                conn = false;
                break;
            }
        }
        if(conn){
            break;
        }
        // szukamy kogokolwiek z cnt_marked == cnt_color
        int c,v=-1;
        for(c=1; c<=k+1; ++c){
            if(cnt_color[c] == 0) continue;
            int any_v = col_any_v[c];
            int comp = dsu[c].Find(any_v);
            int cnt_marked = dsu[c].cnt_marked[comp];
            if(cnt_marked == cnt_color[c]){
                v = any_v;
                break;
            }
        }
        if(v == -1){
            clear();
            cout << "NIE\n";
            return;
        }
        rep(v,1,n+1){
            if(color[v] != c) continue;
            rep(c,1,k+1){
                each(u,g[v]){
                    if(color[u] == color[v]) continue;
                    dsu[c].Union(v,u);
                }
            }
        }
    }
    clear();
    cout << "TAK\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}