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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

// #define dprint(...) printf(__VA_ARGS__)
#define dprint(...)

vector<int> c;
vector<int> p;

int pp(int x) {
  if (p[x] == x) return x;
  return p[x] = pp(p[x]);
}

bool join(int x, int y) {
  int xx = pp(x);
  int yy = pp(y);
  p[xx] = yy;
  return xx != yy;
}

vector<int> a;
vector< vector<int> > ar;

struct E {
  // maps from a[v] into v
  unordered_map<int, int> n;

  E() {}

  E(int v) {
    n[a[v]] = v;
  }
};

vector<E> ee;
vector<int> pe;
vector< vector<int> > e;

int ppe(int x) {
  if (pe[x] == x) return x;
  return pe[x] = ppe(pe[x]);
}

vector<int> queue;

void merge(E& x, E& y) {
  if (x.n.size() < y.n.size()) swap(x, y);
  for (auto el: y.n) {
    auto el2 = x.n.find(el.first);
    if (el2 == x.n.end()) {
      x.n.insert(el);
    } else {
      if(join(el.second, el2->second)) {
        if (--c[a[el.second]] == 1) {
          queue.push_back(a[el.second]);
        }
      }
    }
  };
}

int n, m, k;

void printall() {
/*
  dprint("c:\n");
  for (int i=0; i<k; ++i) dprint("%d ", c[i]);
  dprint("\n");
  
  dprint("p:\n");
  for (int i=0; i<n; ++i) dprint("%d ", p[i]);
  dprint("\n");
  */
}

void work() {
  scanf("%d %d %d", &n, &m, &k);
  p.resize(n);
  for (int i=0; i<n; ++i) p[i] = i;
  dprint("HERE 3\n");

  a.resize(n);
  ar.clear();
  ar.resize(k);
  c.clear();
  c.resize(k, 0);
  for (int i=0; i<n; ++i) {
    scanf("%d", &a[i]); --a[i];
    ar[a[i]].push_back(i);
    c[a[i]]++;
  }

  for (int i=0; i<k; ++i) if (c[i] == 1) queue.push_back(i);
  
  dprint("HERE 2\n");

  e.clear();
  e.resize(n);
  pe.resize(m);
  ee.resize(m);
  for (int i=0; i<m; ++i) {
    int x, y; scanf("%d %d", &x, &y); --x; --y;
    e[x].push_back(i);
    e[y].push_back(i);
    pe[i] = i;
    ee[i] = E(x);
    E tmp(y);
    merge(ee[i], tmp);
  }


  dprint("HERE\n");
  

  while (!queue.empty()) {

    int q = queue.back(); queue.pop_back();
    
    printall();

    for (int v: ar[q]) {
      for (int i=1; i<e[v].size(); ++i) {
        int xx = ppe(e[v][i]);
        int yy = ppe(e[v][i-1]);
        pe[xx] = yy;
        if (xx != yy) {
          merge(ee[yy], ee[xx]);
        }
      }
    }
  }

  printall();
  
  bool ok = true;
  for (int i: c) if (i > 1) ok = false;
  if (ok) printf("TAK\n"); else printf("NIE\n");
}

int main() {
  int t; scanf("%d", &t);
  while (t--) work();
  return 0;
}