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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <bits/stdc++.h>

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

using namespace std;

#ifdef LOCAL
template<typename A, typename B>
auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

using i64 = long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
using vi = vector<int>;
using vll = vector<i64>;

class BitReader {
	string data;
	int ptr = 0;
	int sz;

	vi trit_buf;

public:
	BitReader(string bits) : data{bits}, sz{SZ(bits)} {}

	int ReadBit() {
		if (ptr < sz) {
			return data[ptr++] - '0';
		} else {
			return 0;
		}
	}

	int ReadTrit() {
		if (trit_buf.empty()) {
			if (ptr == sz) {
				return 0;
			}
			int num = 0;
			for (int i = 0; i < 11; ++i) {
				num = num * 2 + ReadBit();
			}
			for (int i = 0; i < 7; ++i) {
				trit_buf.push_back(num % 3);
				num /= 3;
			}
			assert(num == 0);
		}
		const int val = trit_buf.back();
		trit_buf.pop_back();
		return val;
	}

	bool Done() const {
		return (ptr == sz && trit_buf.empty());
	}
};

class BitWriter {
	string data;
	vi trit_buf;
	int start_trit;

public:
	BitWriter() : start_trit{-1} {}

	int NumReadyBits() const {
		if (start_trit == -1) {
			return SZ(data);
		} else {
			return start_trit;
		}
	}

	string Get(int sz) const {
		assert(NumReadyBits() >= sz);
		string res = data;
		res.resize(sz);
		return res;
	}

	void WriteBit(int bit) {
		data += '0' + bit;
	}

	void WriteTrit(int trit) {
		if (start_trit == -1) {
			start_trit = SZ(data);
			data += string(11, '-');
		}
		trit_buf.push_back(trit);
		if (SZ(trit_buf) == 7) {
			int num = 0;
			for (int t : trit_buf) {
				num = num * 3 + t;
			}
			trit_buf.clear();
			for (int i = 10; i >= 0; --i) {
				data[start_trit + i] = '0' + num % 2;
				num /= 2;
			}
			start_trit = -1;
		}
	}
};

void StressBitRW() {
	srand(time(NULL));
	const int TT = 100;
	const int NN = 5000;
	for (int run = 0; run < TT; ++run) {
		string s(NN, '-');
		for (char &ch : s) {
			ch = rand() % 2 + '0';
		}

		BitReader br(s);
		BitWriter bw;
		int num_iters = 0;

		while (!br.Done()) {
			assert(bw.NumReadyBits() < NN);
			++num_iters;
			if (rand() % 2 == 0) {
				const int bit = br.ReadBit();
				bw.WriteBit(bit);
			} else {
				const int trit = br.ReadTrit();
				bw.WriteTrit(trit);
			}
		}
		assert(bw.NumReadyBits() >= NN);
		cerr << num_iters << " ";

		assert(bw.Get(NN) == s);
	}
}


int agent_idx;
int N, T;
vector<vector<vi>> rand_bits;

const string actions = "PKN";  // `i` wins against `i+1`

char TritToAction(int trit) {
	return actions[trit];
}

int ActionToTrit(char action) {
	return actions.find(action);
}

void InitInput() {
	string who;
	cin >> who >> N >> T;
	agent_idx = (who == "Algosia" ? 0 : 1);
}

void InitRnd() {
	mt19937 gen{3895712};

	rand_bits.resize(T);
	for (auto &tc_bits : rand_bits) {
		tc_bits.resize(2);
		for (auto &agent_bits : tc_bits) {
			agent_bits.resize(N);
			for (auto &b : agent_bits) {
				b = gen() % 2;
			}
		}
	}
}

void SubmitAction(char ch) {
	cout << ch << endl;
}

char ReadAction() {
	string s;
	cin >> s;
	return s[0];
}

void DoAnswer(string answer) {
	cout << "! " << answer << endl;
}

int ProcessAction(char my_action, char other_action) {
	const int my_idx = ActionToTrit(my_action);
	const int other_idx = ActionToTrit(other_action);
	if (my_idx == other_idx) {
		return 0;
	} else if (other_idx == (my_idx + 1) % 3) {
		return (+1);
	} else {
		return (-1);
	}
}

void Run(int tc) {
	string my_bits;
	cin >> my_bits;
	for (int i = 0; i < N; ++i) {
		my_bits[i] ^= rand_bits[tc][agent_idx][i];
	}
	BitReader br(my_bits);
	BitWriter bw;
	int cur_delta = 0;

	while (!br.Done() || bw.NumReadyBits() < N) {
		if (cur_delta == 0) {
			const int my_trit = br.ReadTrit();
			const char my_action = TritToAction(my_trit);
			SubmitAction(my_action);
			const char other_action = ReadAction();
			const int other_trit = ActionToTrit(other_action);
			bw.WriteTrit(other_trit);
			cur_delta += ProcessAction(my_action, other_action);
		} else if (cur_delta == (+1)) {
			const int my_trit_st = 1;
			const char my_action_st = TritToAction(my_trit_st);
			SubmitAction(my_action_st);
			const char other_action_st = ReadAction();
			cur_delta += ProcessAction(my_action_st, other_action_st);
			if (cur_delta == (+1)) {
				bw.WriteBit(1);
				bw.WriteBit(1);
			} else {
				assert(cur_delta == 0);
				const int my_trit_nd = br.ReadTrit();
				const char my_action_nd = TritToAction(my_trit_nd);
				SubmitAction(my_action_nd);
				const char other_action_nd = ReadAction();
				const int other_trit_nd = ActionToTrit(other_action_nd);
				cur_delta += ProcessAction(my_action_nd, other_action_nd);
				bw.WriteBit(other_trit_nd / 2);
				bw.WriteBit(other_trit_nd % 2);
			}
		} else {
			assert(cur_delta == (-1));
			const int my_bit_st = br.ReadBit();
			const int my_bit_nd = br.ReadBit();
			if (my_bit_st == 1 && my_bit_nd == 1) {
				const char my_action_st = TritToAction(1);
				SubmitAction(my_action_st);
				const char other_action_st = ReadAction();
				assert(other_action_st == TritToAction(1));
				cur_delta += ProcessAction(my_action_st, other_action_st);
				assert(cur_delta == (-1));
			} else {
				const char my_action_st = TritToAction(0);
				SubmitAction(my_action_st);
				const char other_action_st = ReadAction();
				assert(other_action_st == TritToAction(1));
				cur_delta += ProcessAction(my_action_st, other_action_st);
				assert(cur_delta == 0);
				
				const int my_trit_nd = my_bit_st * 2 + my_bit_nd;
				assert(my_trit_nd <= 2);
				const char my_action_nd = TritToAction(my_trit_nd);
				SubmitAction(my_action_nd);
				const char other_action_nd = ReadAction();
				const int other_trit_nd = ActionToTrit(other_action_nd);
				bw.WriteTrit(other_trit_nd);
				cur_delta += ProcessAction(my_action_nd, other_action_nd);
			}
		}
	}

	string other_bits = bw.Get(N);
	for (int i = 0; i < N; ++i) {
		other_bits[i] ^= rand_bits[tc][agent_idx ^ 1][i];
	}
	DoAnswer(other_bits);
}

int main() {
	// StressBitRW();
	InitInput();
	InitRnd();
	for (int i = 0; i < T; ++i) {
		Run(i);
	}
}