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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <fstream>
#include <iostream>
#include <string>
#include <list>
#include <algorithm>

using namespace std;

string text;
int word;
list<int> arguments = {};
list<list<int>> lists1 = {};
list<list<int>> lists2 = {};

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    while (std::cin >> word) {
        //cout << word << "\n";
        arguments.push_back(word);
    }
    //cout << "-----" << "\n";
    auto n = next(arguments.begin(), 0);
    for (int i = 1; i <= *n; i++)
    {
        list<int> list1 = {};
        for (int k = 1; k <= *n; k++)
        {
            if (k % i == 0)
            {
                list1.push_back(k);
            }
        }
        lists1.push_back(list1);
    }
    for (auto v : lists1)
    {
        for (auto b : v)
        {
            //cout << b << " ";
        }
        //cout << "\n";
    }
    //cout << "-----" << "\n";
    auto m = next(arguments.begin(), 1);
    int l = 1;
    int p = 0;
    for (int i = 1; i <= *m; i++)
    {
        auto k = next(arguments.begin(), 1 + i + l);
        //cout << *k << "!! \n";
        if (*k == 1)
        {
            l++;
            auto o1 = next(arguments.begin(), 1 + i + l);
            //cout << *o1 << "! \n";
            auto u1 = next(lists1.begin(), *o1 - 1);
            l++;
            auto o2 = next(arguments.begin(), 1 + i + l);
            //cout << *o2 << "! \n";
            auto u2 = next(lists1.begin(), *o2 - 1);
            list<int> list = {};
            for (auto b : *u1)
            {
                list.push_back(b);
            }
            for (auto a : *u2)
            {
                auto it = std::find(list.begin(), list.end(), a);
                if (it != list.end())
                {

                }
                else
                {
                    list.push_back(a);
                }
            }
            lists1.push_back(list);
        }
        if (*k == 2)
        {
            l++;
            auto o1 = next(arguments.begin(), 1 + i + l);
            //cout << *o1 << "! \n";
            auto u1 = next(lists1.begin(), *o1 - 1);
            l++;
            auto o2 = next(arguments.begin(), 1 + i + l);
            //cout << *o2 << "! \n";
            auto u2 = next(lists1.begin(), *o2 - 1);
            list<int> list = {};
            for (auto b : *u1)
            {
                for (auto a : *u2)
                {
                    if (a == b)
                    {
                        list.push_back(a);
                    }
                }
            }
            lists1.push_back(list);
        }
        if (*k == 3)
        {
            l++;
            auto o1 = next(arguments.begin(), 1 + i + l);
            //cout << *o1 << "! \n";
            auto u1 = next(lists1.begin(), *o1 - 1);
            list<int> list1 = {};
            list<int> list2 = {};
            for (auto b : *u1)
            {
                list1.push_back(b);
            }
            for (int i = 1; i <= *n; i++)
            {
                auto it = std::find(list1.begin(), list1.end(), i);
                if (it != list1.end())
                {

                }
                else
                {
                    list2.push_back(i);
                }
            }
            lists1.push_back(list2);
        }
        p = 1 + i + l;
    }
    //cout << "-----" << "\n";
    for (auto v : lists1)
    {
        for (auto b : v)
        {
            //cout << b << " ";
        }
        //cout << "\n";
    }
    //cout << "-----" << "\n";
    auto q = next(arguments.begin(), 2);
    //cout << "p: " << p << "\n";
    //ofstream output("output.out");
    for (int i = 1; i <= *q; i++)
    {
        auto u1 = next(arguments.begin(), i + p);
        //cout << *u1 << " u1 \n";
        p++;
        auto u2 = next(lists1.begin(), *u1 - 1);
        auto u3 = next(arguments.begin(), i + p);
        //cout << *u3 << " u3 \n";
        list<int> list = {};
        for (auto b : *u2)
        {
            list.push_back(b);
        }
        auto it = std::find(list.begin(), list.end(), *u3);
        if (it != list.end())
        {
            std::cout << "TAK";
        }
        else
        {
            std::cout << "NIE";
        }

        if (i != *q) 
        {
            std::cout << "\n";
        }

    }
    //output.close();
    return 0;
}