#include <iostream>
#include <vector>
#include <string>
using namespace std;
int n, m;
int k, logk;
int fuck;
int main(){
cin>>n;
k=1;
logk=1;
while(k<n){
k*=2;
logk++;
}
vector< pair<int,int> > tree[2*k-1];
int a,b;
for(int i=2*k-2; i>(2*k-2-(n-k)); i--){
tree[i].push_back(make_pair(0,0));
}
for(int i=k-1; i<=(2*k-2+(n-k)); i++){
cin>>a;
cin>>b;
if(a==b){
tree[i].push_back(make_pair(a,a));
}
else{
tree[i].push_back(make_pair(min(a,b),max(a,b)));
tree[i].push_back(make_pair(a,a));
tree[i].push_back(make_pair(b,b));
}
}
for(int i=k-2; i>=0; i--){
//Bierzemy pod uwagę węzły o numerach 2*i+1 oraz 2*i+2
for(pair<int,int> t : tree[2*i+1]){
for(pair<int,int> s : tree[2*i+2]){
if(t.second<=s.first){
tree[i].push_back(make_pair(t.first,s.second));
}
}
}
//Redukcja elementów w tree[i]
for (int j = 0; j < tree[i].size(); j++)
{
for (int q = j+1; q < tree[i].size(); q++)
{
if((tree[i][j].first==tree[i][q].first) and (tree[i][j].second==tree[i][q].second)){
tree[i].erase(tree[i].begin()+q);
q--;
}
}
}
}
/*
4
2 5
3 4
6 3
2 7
2
3 4
1 3
4 5
1 3
2 4
1 5
5 6
*/
/*while(true){
cin>>a;
if(tree[a].size()){
cout<<tree[a][0].first<<':'<<tree[a][0].second<<endl;
}
else{
cout<<"Empty"<<endl;
}
}*/
//Teraz zaczyna się zabawa - zmieniamy 2 kolejne karty
pair <int,int> robocze1, robocze2;
//cout<<endl<<'a'<<endl;
cin>>m;
int swap1[m], swap2[m];
for (int i = 0; i < m; i++)
{
cin>>swap1[i];
cin>>swap2[i];
}
cout<<endl<<swap2[1];
for (int id = 0; id < m; id++)
{
cout<<id;
int c = 3;
swap1[id]=swap1[id]+c;
swap2[id]=swap2[id]+c;
robocze1=tree[swap1[id]][0];
robocze2=tree[swap2[id]][0];
tree[swap1[id]].clear();
tree[swap2[id]].clear();
tree[swap1[id]].push_back(robocze2);
tree[swap2[id]].push_back(robocze1);
for (int i = 1; i < logk; i++)
{
swap1[id]=(swap1[id]-1)/2;
swap2[id]=(swap2[id]-1)/2;
if(swap1[id]!=swap2[id]){
//Zmiana w swap1[id]
for(pair<int,int> t :tree[2*swap1[id]+1]){
for(pair<int,int> s :tree[2*swap1[id]+2]){
if(t.second<=s.first){
tree[i].push_back(make_pair(t.first,s.second));
}
}
}
//Redukcja elementów w tree[i]
for (int j = 0; j < tree[swap1[id]].size(); j++)
{
for (int q = j+1; q < tree[swap1[id]].size(); q++)
{
if((tree[swap1[id]][j].first==tree[swap1[id]][q].first) and (tree[swap1[id]][j].second==tree[swap1[id]][q].second)){
tree[swap1[id]].erase(tree[swap1[id]].begin()+q);
q--;
}
}
}
//Zmiana w d
for(pair<int,int> t :tree[2*swap2[id]+1]){
for(pair<int,int> s :tree[2*swap2[id]+2]){
if(t.second<=s.first){
tree[i].push_back(make_pair(t.first,s.second));
}
}
}
//Redukcja elementów w tree[i]
for (int j = 0; j < tree[swap2[id]].size(); j++)
{
for (int q = j+1; q < tree[swap2[id]].size(); q++)
{
if((tree[swap2[id]][j].first==tree[swap2[id]][q].first) and (tree[swap2[id]][j].second==tree[swap2[id]][q].second)){
tree[swap2[id]].erase(tree[swap2[id]].begin()+q);
q--;
}
}
}
}
}
if(tree[0].size()>0){
cout<<"TAK"<<endl;
}
else{
cout<<"NIE"<<endl;
}
}
return 0;
}