#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<vector<int>> wektor;
vector<int> odwiedzone;
vector<int> komponent;
void BFS(int V, int componentId) {
queue<int> q;
q.push(V);
odwiedzone[V] = 1;
komponent[V] = componentId;
while (!q.empty()) {
int p = q.front();
q.pop();
for (int j : wektor[p]) {
if (odwiedzone[j] == 0) {
odwiedzone[j] = 1;
komponent[j] = componentId;
q.push(j);
}
}
}
}
int main() {
int a {0}, b {0};
int ile_v {0};
int ile_e {0};
cin >> ile_v >> ile_e;
wektor.resize(ile_v + 1);
odwiedzone.resize(ile_v + 1, 0);
komponent.resize(ile_v + 1, 0);
for (int i = 0; i < ile_e; i++) {
cin >> a >> b;
wektor[a].push_back(b);
wektor[b].push_back(a);
}
int componentId = 0;
for (int i = 1; i <= ile_v; i++) {
if (odwiedzone[i] == 0) {
componentId++;
BFS(i, componentId);
}
}
int ile_q {0};
cin >> ile_q;
for (int i = 0; i < ile_q; i++) {
int p, k;
cin >> p >> k;
if (komponent[p] == komponent[k]) {
cout << "TAK\n";
} else {
cout << "NIE\n";
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2ZWN0b3I8dmVjdG9yPGludD4+IHdla3RvcjsKdmVjdG9yPGludD4gb2R3aWVkem9uZTsKdmVjdG9yPGludD4ga29tcG9uZW50OwoKdm9pZCBCRlMoaW50IFYsIGludCBjb21wb25lbnRJZCkgewogICAgcXVldWU8aW50PiBxOwogICAgcS5wdXNoKFYpOwogICAgb2R3aWVkem9uZVtWXSA9IDE7CiAgICBrb21wb25lbnRbVl0gPSBjb21wb25lbnRJZDsKCiAgICB3aGlsZSAoIXEuZW1wdHkoKSkgewogICAgICAgIGludCBwID0gcS5mcm9udCgpOwogICAgICAgIHEucG9wKCk7CgogICAgICAgIGZvciAoaW50IGogOiB3ZWt0b3JbcF0pIHsKICAgICAgICAgICAgaWYgKG9kd2llZHpvbmVbal0gPT0gMCkgewogICAgICAgICAgICAgICAgb2R3aWVkem9uZVtqXSA9IDE7CiAgICAgICAgICAgICAgICBrb21wb25lbnRbal0gPSBjb21wb25lbnRJZDsKICAgICAgICAgICAgICAgIHEucHVzaChqKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBpbnQgYSB7MH0sIGIgezB9OwogICAgaW50IGlsZV92IHswfTsKICAgIGludCBpbGVfZSB7MH07CgogICAgY2luID4+IGlsZV92ID4+IGlsZV9lOwoKICAgIHdla3Rvci5yZXNpemUoaWxlX3YgKyAxKTsKICAgIG9kd2llZHpvbmUucmVzaXplKGlsZV92ICsgMSwgMCk7CiAgICBrb21wb25lbnQucmVzaXplKGlsZV92ICsgMSwgMCk7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBpbGVfZTsgaSsrKSB7CiAgICAgICAgY2luID4+IGEgPj4gYjsKICAgICAgICB3ZWt0b3JbYV0ucHVzaF9iYWNrKGIpOwogICAgICAgIHdla3RvcltiXS5wdXNoX2JhY2soYSk7CiAgICB9CgogICAgaW50IGNvbXBvbmVudElkID0gMDsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IGlsZV92OyBpKyspIHsKICAgICAgICBpZiAob2R3aWVkem9uZVtpXSA9PSAwKSB7CiAgICAgICAgICAgIGNvbXBvbmVudElkKys7CiAgICAgICAgICAgIEJGUyhpLCBjb21wb25lbnRJZCk7CiAgICAgICAgfQogICAgfQoKICAgIGludCBpbGVfcSB7MH07CiAgICBjaW4gPj4gaWxlX3E7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBpbGVfcTsgaSsrKSB7CiAgICAgICAgaW50IHAsIGs7CiAgICAgICAgY2luID4+IHAgPj4gazsKICAgICAgICBpZiAoa29tcG9uZW50W3BdID09IGtvbXBvbmVudFtrXSkgewogICAgICAgICAgICBjb3V0IDw8ICJUQUtcbiI7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY291dCA8PCAiTklFXG4iOwogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gMDsKfQ==