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
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
//mt19937 mrand(random_device{}());
const ll mod=1000000007;
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int gcd(int a,int b) { return b?gcd(b,a%b):a;}
// head

const int N=1e5+5;

#define INF 0x3f3f3f3f

template<class T> inline void read(T &x) {
    x=0; int c=getchar(),f=1;
    for (;!isdigit(c);c=getchar()) if (c==45) f=-1;
    for (;isdigit(c);c=getchar()) (x*=10)+=f*(c-'0');
}

int a[N],f_col[N],f_comp[N],q[N],comp_id[N],comp_col[N],total[N];
VI e[N],e_comp[N],col_comp[N];
unordered_map<int,int> hs[N];
bool used[N],avail[N],in_q[N];

int find_col(int c) { return f_col[c]=(f_col[c]==c?c:find_col(f_col[c]));}

int find_comp(int c) { return f_comp[c]=(f_comp[c]==c?c:find_comp(f_comp[c]));}

bool merge_col(int c_1,int c_2) {
    int r_1=find_col(c_1),r_2=find_col(c_2);
    if (r_1==r_2) return false;
    f_col[r_1]=r_2;
    return true;
}

int main() {
    int t;
    scanf("%d",&t);
    while (t--) {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        rep(i,0,n) {
            scanf("%d",&a[i]);
            a[i]--;
            e[i].clear();
        }
        rep(i,0,m) {
            int u,v;
            scanf("%d%d",&u,&v);
            u--;
            v--;
            e[u].pb(v);
            e[v].pb(u);
        }
        memset(comp_id,-1,n*sizeof(int));
        memset(used,false,k*sizeof(bool));
        int total_comp=0;
        rep(i,0,n) {
            if (comp_id[i]>=0) continue;
            int t_1=0,t_2=0;
            q[t_1++]=i;
            comp_id[i]=total_comp;
            while (t_2<t_1) {
                int u=q[t_2++];
                for (const auto &v:e[u]) if (comp_id[v]==-1&&a[u]==a[v]) {
                    comp_id[v]=total_comp;
                    q[t_1++]=v;
                }
            }
            e_comp[total_comp].clear();
            hs[total_comp].clear();
            total_comp++;
        }
        memset(avail,false,total_comp*sizeof(bool));
        memset(in_q,false,k*sizeof(bool));
        rep(i,0,k) col_comp[i].clear();
        rep(i,0,n) comp_col[comp_id[i]]=a[i];
        rep(i,0,total_comp) col_comp[comp_col[i]].pb(i);
        rep(u,0,n) for (const auto &v:e[u]) if (comp_id[u]!=comp_id[v]) e_comp[comp_id[u]].pb(comp_id[v]);
        rep(i,0,total_comp) {
            sort(all(e_comp[i]));
            e_comp[i].erase(unique(all(e_comp[i])),e_comp[i].end());
        }
        rep(i,0,total_comp) f_col[i]=i,f_comp[i]=i;
        int t_1=0,t_2=0;
        auto merge_comp=[&](int c_1,int c_2) {
            int r_1=find_comp(c_1),r_2=find_comp(c_2);
            if (r_1==r_2) return;
            if (SZ(hs[r_1])<SZ(hs[r_2])) swap(r_1,r_2);
            f_comp[r_2]=r_1;
            for (const auto &[col,c_3]:hs[r_2]) {
                if (used[col]) continue;
                auto it=hs[r_1].find(col);
                if (it!=hs[r_1].end()) {
                    if (merge_col(it->se,c_3)) {
                        total[col]--;
                        if (total[col]==1) if (!in_q[col]) {
                            q[t_1++]=col;
                            in_q[col]=true;
                        }
                    }
                } else hs[r_1][col]=c_3;
            }
            hs[r_2].clear();
        };
        int cnt=0;
        rep(i,0,k) {
            total[i]=SZ(col_comp[i]);
            if (total[i]==0) cnt++;
            if (total[i]==1) {
                q[t_1++]=i;
                in_q[i]=true;
            }
        }
        while (t_2<t_1) {
            int col_1=q[t_2++];
            used[col_1]=true;
            for (const auto &c:col_comp[col_1]) avail[c]=true;
            for (const auto &c_1:col_comp[col_1]) for (const auto &c_2:e_comp[c_1]) if (avail[c_2]) merge_comp(c_1,c_2);
            for (const auto &c_1:col_comp[col_1]) for (const auto &c_2:e_comp[c_1]) if (!avail[c_2]) {
                int col_2=comp_col[c_2];
                if (used[col_2]) continue;
                int r=find_comp(c_1);
                auto it=hs[r].find(col_2);
                if (it!=hs[r].end()) {
                    if (merge_col(it->se,c_2)) {
                        total[col_2]--;
                        if (total[col_2]==1) if (!in_q[col_2]) {
                            q[t_1++]=col_2;
                            in_q[col_2]=true;
                        }
                    }
                } else hs[r][col_2]=c_2;
            }
            cnt++;
        }
        if (cnt==k) puts("TAK");
        else puts("NIE");
    }
}