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;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;

const ll I = 1'000'000'000'000'000'000LL;
const int II = 2'000'000'000;
const ll M = 1'000'000'007LL;
const int N = 1<<19;
int fau[N], fau2[N];
int clr[N], il[N];

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

unordered_map<int, int, custom_hash> zbi[N];
vector<int> ed[N];
vector<int> wh[N];

int vis[N];
queue<int> q;

int Find2(int v)
{
    if(fau2[v] == v) return v;
    return (fau2[v] = Find2(fau2[v]));
}

void Union2(int a, int b)
{
    a = Find2(a); b = Find2(b);
    if(a == b) return;
    --il[clr[a]];
    if(il[clr[a]] == 1)
        q.push(clr[a]);
    fau2[b] = a;
}

int Find(int v)
{
    if(fau[v] == v) return v;
    return (fau[v] = Find(fau[v]));
}


void Union(int a, int b)
{
    a = Find(a); b = Find(b);
    if(a == b) return;
    if((int)zbi[a].size() < (int)zbi[b].size()) swap(zbi[a], zbi[b]);
    unordered_map<int, int, custom_hash>::iterator it;
    for(it = zbi[b].begin(); it != zbi[b].end(); ++it)
    {
        int x = (*it).st, y = (*it).nd;
        if(zbi[a].find(x) == zbi[a].end())
            zbi[a][x] = y;
        else
            Union2(zbi[a][x], y);
    }
    zbi[b].clear();
    fau[b] = a;
}

void Solve()
{
    int n, m, k, a, b;
    cin >> n >> m >> k;
    for(int i = 1; i <= n; ++i)
    {
        cin >> clr[i];
        ++il[clr[i]];
        fau[i] = i; fau2[i] = i;
        wh[clr[i]].pb(i);
    }
    for(int i = 1; i <= m; ++i)
    {
        cin >> a >> b;
        ed[a].pb(b); ed[b].pb(a);
    }
    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < (int)ed[i].size(); ++j)
            if(ed[i][j] > i && clr[i] == clr[ed[i][j]])
                Union2(i, ed[i][j]);
    while((int)q.size() > 0) q.pop();
    for(int i = 1; i <= k; ++i)
        if(il[i] <= 1)
            q.push(i);
    int cnt = 0;
    while((int)q.size() > 0)
    {
        ++cnt; a = q.front(); q.pop();
        vis[a] = 1;
        for(int l = 0; l < (int)wh[a].size(); ++l)
        {
            int v = wh[a][l], p = Find(v);
            for(int i = 0; i < (int)ed[v].size(); ++i)
                if(!vis[clr[ed[v][i]]])
                {
                    if(zbi[p].find(clr[ed[v][i]]) == zbi[p].end())
                        zbi[p][clr[ed[v][i]]] = ed[v][i];
                    else
                        Union2(zbi[p][clr[ed[v][i]]], ed[v][i]);
                }else
                {
                    Union(v, ed[v][i]);
                    p = Find(v);
                }
        }
    }

    if(cnt == k)
        cout << "TAK\n";
    else
        cout << "NIE\n";

    for(int i = 1; i <= n; ++i)
    {
        zbi[i].clear();
        ed[i].clear();
    }
    for(int i = 1; i <= k; ++i)
    {
        il[i] = 0;
        wh[i].clear();
        vis[i] = 0;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t; cin >> t;
    while(t--)
        Solve();

    return 0;
}