#include<bits/stdc++.h>
using namespace std;
#define ll long long
#include<math.h>
vector<vector<ll>> liczby(1e6+1);
vector<ll>wczytane;
ll binsearch(vector<ll> arr, ll target) {
ll left = 0,right = arr.size() - 1,result = -1;
while (left <= right) {
ll mid = left + (right - left) / 2;
if (arr[mid] >= target) {
result = arr[mid];
right = mid - 1;
} else {
left = mid + 1;
}
}
return result;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll ilosc,b,x,aktualna,c,czy;
cin >> ilosc;
for(ll i = 0; i<ilosc; i++){
cin >> b;
liczby[b].push_back(i);
}
cin >> ilosc;
for(ll i = 0; i<ilosc; i++){
cin >> x;
aktualna = 0;
czy = 0;
for(ll e = 0; e<x; e++){
cin >> c;
wczytane.push_back(c);
}
for(auto e:wczytane){
if(liczby[e].size() == 0 or liczby[e][liczby[e].size()-1] < aktualna){
cout << "NIE" << endl;
czy = 1;
break;
}
else{
aktualna = binsearch(liczby[e],aktualna)+1;
}
}
if(!czy){
cout << "TAK" << endl;
}
wczytane.clear();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKI2luY2x1ZGU8bWF0aC5oPgoKdmVjdG9yPHZlY3RvcjxsbD4+IGxpY3pieSgxZTYrMSk7CnZlY3RvcjxsbD53Y3p5dGFuZTsKCmxsIGJpbnNlYXJjaCh2ZWN0b3I8bGw+IGFyciwgbGwgdGFyZ2V0KSB7CiAgICBsbCBsZWZ0ID0gMCxyaWdodCA9IGFyci5zaXplKCkgLSAxLHJlc3VsdCA9IC0xOyAKCiAgICB3aGlsZSAobGVmdCA8PSByaWdodCkgewogICAgICAgIGxsIG1pZCA9IGxlZnQgKyAocmlnaHQgLSBsZWZ0KSAvIDI7CgogICAgICAgIGlmIChhcnJbbWlkXSA+PSB0YXJnZXQpIHsKICAgICAgICAgICAgcmVzdWx0ID0gYXJyW21pZF07CiAgICAgICAgICAgIHJpZ2h0ID0gbWlkIC0gMTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBsZWZ0ID0gbWlkICsgMTsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIHJlc3VsdDsKfQoKCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7CiAgICBsbCBpbG9zYyxiLHgsYWt0dWFsbmEsYyxjenk7CiAgICBjaW4gPj4gaWxvc2M7CiAgICAKICAgIGZvcihsbCBpID0gMDsgaTxpbG9zYzsgaSsrKXsKICAgICAgICBjaW4gPj4gYjsKICAgICAgICBsaWN6YnlbYl0ucHVzaF9iYWNrKGkpOwogICAgICAgIAogICAgfQogICAgY2luID4+IGlsb3NjOwogICAgZm9yKGxsIGkgPSAwOyBpPGlsb3NjOyBpKyspewogICAgICAgIAogICAgICAgIGNpbiA+PiB4OwogICAgICAgIGFrdHVhbG5hID0gMDsKICAgICAgICBjenkgPSAwOwogICAgICAgIGZvcihsbCBlID0gMDsgZTx4OyBlKyspewogICAgICAgICAgICBjaW4gPj4gYzsKICAgICAgICAgICAgd2N6eXRhbmUucHVzaF9iYWNrKGMpOwogICAgICAgIH0KICAgICAgICBmb3IoYXV0byBlOndjenl0YW5lKXsKICAgICAgICAgICAgaWYobGljemJ5W2VdLnNpemUoKSA9PSAwIG9yIGxpY3pieVtlXVtsaWN6YnlbZV0uc2l6ZSgpLTFdIDwgYWt0dWFsbmEpewogICAgICAgICAgICAgICAgY291dCA8PCAiTklFIiA8PCBlbmRsOwogICAgICAgICAgICAgICAgY3p5ID0gMTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICBha3R1YWxuYSA9IGJpbnNlYXJjaChsaWN6YnlbZV0sYWt0dWFsbmEpKzE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYoIWN6eSl7C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