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
#include <bits/stdc++.h>
#define pii pair<int, int>
#define For(i, l, r) for (int i=l;(l<=r?i<=r:i>=r);(l<=r?i++:i--))
//#define DEBUG
#ifdef DEBUG
auto operator<<(auto &o,auto p)->decltype(p.first,o){return o<<'('<<p.first<<", "<<p.second<<')';}
auto operator<<(auto &o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto &e:x)o<<(", ")+i<<e,i=0;return o<<'}';}
#define LOG(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X);
#else
#define LOG(x...)(void)0
#endif
//#define int long long
using namespace std;

const int inf=1e9+1;
int n;
bool who;
string moves = "KNP";
vector<int> move_to_dig(128);

mt19937 gen(81730);
uniform_int_distribution<> distrib3(0, 2);

const int ch2_len = 19, ch3_len = 12;
vector<int> diffs;

string bin_chunk_to_ter(string w){
    while (w.size() < ch2_len)
        w.push_back('0');
    int x = 0;
    for (int i = 0; i < ch2_len; i++){
        x *= 2;
        x += w[i] - '0';
    }
    string res;
    for (int i = 0; i < ch3_len; ++i){
        res.push_back('0' + x % 3);
        x /= 3;
    }
    reverse(res.begin(), res.end());
    return res;
}

string ter_chunk_to_bin(string w){
    while (w.size() < ch3_len)
        w.push_back('0');
    int x = 0;
    for (int i = 0; i < ch3_len; i++){
        x *= 3;
        x += w[i] - '0';
    }
    string res;
    for (int i = 0; i < ch2_len; ++i){
        res.push_back('0' + x % 2);
        x /= 2;
    }
    reverse(res.begin(), res.end());
    return res;
}

string bin_to_ter(string w){
    string res = "";
    for (auto chun: w | std::views::chunk(ch2_len)){
        string chunk(chun.begin(), chun.end());
        res = res + bin_chunk_to_ter(chunk);
    }
    return res;
}

string ter_to_bin(string w){
    string res = "";
    for (auto chun: w | std::views::chunk(ch3_len)){
        string chunk(chun.begin(), chun.end());
        res = res + ter_chunk_to_bin(chunk);
    }
    while (res.size() > n)
        res.pop_back();
    return res;
}

string rand_string(int l = n, int x = 2){
    string res;
    for (int i = 0; i < n; ++i){
        res.push_back('0' + rand() % x);
    }
    return res;
}

vector<int> rand_vec_string(int l = n, int x = 2){
    vector<int> res(l);
    for (int i = 0; i < l; ++i){
        res[i] = distrib3(gen);
    }
    return res;
}

void trycheck(){
    string bin = rand_string();
    string ter = bin_to_ter(bin);
    string bin2 = ter_to_bin(ter);
    if (bin != bin2){
        cout << bin << '\n' << ter << '\n' << bin2 << '\n';
    }
    else
        cout << "Correct\n";
    cout << ter.size();
}

void send(int d){
    cout << moves[d] << endl;
}

char read(){
    char move;
    cin >> move;
    return move_to_dig[move];
}

int whowon(int a, int b){
    return (b - a + 3) % 3;
}

void solve(){
    string bin, bin2;
    cin >> bin;
    string ter = bin_to_ter(bin), ter2;
    int state = 0, terid = 0, last_his;
    while (terid < ter.size()){
        if (state == 0){
            int move = ter[terid] - '0';
            if (who){
                move = (move + diffs[terid]) % 3;
            }
            send(move);
            int trit = read();
            state += whowon(move, trit);
            last_his = trit;
            if (!who){
                trit = (trit - diffs[terid] + 3) % 3;
            }
            ter2.push_back(trit + '0');
            terid++;
        }
        else {
            send(last_his);
            int trit = read();
            state = 0;
        }
    }
    bin2 = ter_to_bin(ter2);
    cout << "! " << bin2 << endl;
}

signed main(){
    for (int i = 0; i < 3; ++i)
        move_to_dig[moves[i]] = i;
    cin.tie(0)->sync_with_stdio(0);
    int t=1;
    string w;
    cin >> w >> n >> t;
    diffs = rand_vec_string(n + 100, 3);
    who = (w[0] - 'A');
    while(t--)
        solve();
}