#include<bits/stdc++.h>
using namespace std;
int od[1000006];
vector<int> tab[1000006];
int main()
{
ios_base::sync_with_stdio(0);
int n,m,x;
cin >> n;
for(int i = 0;i < n;++i)
{
cin >> x;
tab[x].push_back(i);
}
cin >> m;
for(int i = 0;i < m;++i)
{
cin >> n; vector<int> tab1;
for(int j = 1;j <= 1000000;++j)
{
od[j] = 0;
}
for(int j = 1;j <= n;++j)
{
cin >> x;
tab1.push_back(x);
}
//vector<int> tab1;
bool p = 0;
for(int j = 0;j < n;++j)
{
auto k = lower_bound(od[tab1[j]]+tab[tab1[j]].begin(),tab[tab1[j]].end(),j);
//cout << k-tab[tab1[j]].begin() << ' ' << od[tab1[j]] << endl;
if(k == tab[tab1[j]].end())
{
cout << "NIE" << endl;
p = 1;
break;
}
od[tab1[j]] = k-tab[tab1[j]].begin()+1;
//cout << od[j] << ' ';
}
if(!p)
cout << "TAK" << endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBvZFsxMDAwMDA2XTsKdmVjdG9yPGludD4gdGFiWzEwMDAwMDZdOwoKaW50IG1haW4oKQp7Cglpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOwoJaW50IG4sbSx4OwoJY2luID4+IG47Cglmb3IoaW50IGkgPSAwO2kgPCBuOysraSkKCXsKCQljaW4gPj4geDsKCQl0YWJbeF0ucHVzaF9iYWNrKGkpOwoJfQoJY2luID4+IG07Cglmb3IoaW50IGkgPSAwO2kgPCBtOysraSkKCXsKCQljaW4gPj4gbjsgdmVjdG9yPGludD4gdGFiMTsKCQlmb3IoaW50IGogPSAxO2ogPD0gMTAwMDAwMDsrK2opCgkJewoJCQlvZFtqXSA9IDA7CgkJfQoJCWZvcihpbnQgaiA9IDE7aiA8PSBuOysraikKCQl7CgkJCWNpbiA+PiB4OwoJCQl0YWIxLnB1c2hfYmFjayh4KTsKCQl9CgkJLy92ZWN0b3I8aW50PiB0YWIxOwoJ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