Problem: agc072_d
Difficulty: hard
Categories: bit, greedy
Time limit: 4000 ms, memory limit: 1024 MB

Score : $1400$ points

### Problem Statement

**This is an interactive problem.**

There are $\tbinom{2N}{N}$ cards numbered $1$ to $\tbinom{2N}{N}$ on a desk. Card $i$ $(1 \le i \le \tbinom{2N}{N})$ shows on its front the $i$‑th string in lexicographic order among the strings consisting of $N$ zeros and $N$ ones.

Example
For $N = 2$, the fronts of cards $1$, $2$, $3$, $4$, $5$, $6$ are `0011`, `0101`, `0110`, `1001`, `1010`, `1100`, respectively.

Witch Alice writes one of the first $N$ uppercase letters (for example, `A` or `B` if $N = 2$) on the back of each card, then performs the following **magic** $T$ times.

> **A single magic**
>
> 1. The host announces a letter $s$ among the first $N$ uppercase letters.
> 2. The host writes a length-$2N$ string of all `0` on the blackboard.
> 3. For $i = 1, 2, \dots, N$ in this order, do the following. Here, $a_1, b_1, \dots, a_N, b_N$ are distinct integers from $1$ to $2N$.
>    * First, the host tells Alice integers $a_i$ and $b_i$.
>    * Then, Alice chooses exactly one of the $a_i$-th and $b_i$-th characters of the string on the blackboard, and sets it to `1`.
> 4. Let $x$ be the final string on the blackboard. Exactly one card shows $x$ on its front; look at its back. The magic succeeds if and only if the letter on its back equals $s$.

Note that Alice learns $a_{i+1}$ and $b_{i+1}$ only after choosing which of the $a_i$-th and $b_i$-th characters to set to `1`.

Implement a strategy that makes all $T$ magics succeed. Now, let us begin.

---

### Constraints

* $2 \le N \le 10$
* $1 \le T \le 5\,000$
* The letter $s$ is among the first $N$ uppercase letters.
* $1 \le a_i < b_i \le 2N$
* $a_1, b_1, a_2, b_2, \dots, a_N, b_N$ are distinct.
* $a_1, b_1, a_2, b_2, \dots, a_N, b_N, s$ are fixed before the interaction starts.
* $N, T, a_i, b_i$ are integers.

### Input and Output

First, read the integers $N$ and $T$ given from Standard Input in the following format:

```
$N$ $T$
```

Next, let $c_i$ be the letter written on the back of card $i$, and print those letters in the following format, followed by a newline. Here, $c_i$ must be among the first $N$ uppercase letters.

```
$c_1 c_2 \cdots c_{\tbinom{2N}{N}}$
```

Next, repeat the following process $T$ times. The $t$-th iteration $(1 \leq t \leq T)$ corresponds to the $t$-th magic.

> Read the announced letter $s$ given in the following format:
>
> ```
> $s$
> ```
>
> Then, for $i = 1, 2, \dots, N$ in this order, do the following.
>
> * Read integers $a_i$ and $b_i$ given in the following format:
>
> ```
> $a_i$ $b_i$
> ```
>
> * Print the chosen position $d$ in the following format, followed by a newline. Here, $d$ must be $a_i$ or $b_i$.
>
> ```
> $d$
> ```
>
> If your output was malformed or the previous magic failed, no more input such as $s$ or $(a_i, b_i)$ is given, but `-1` is given from Standard Input. In this case, exit immediately. (If you fail for the first time in the $T$-th magic, or your output was malformed for the first time at $i = N$ in the $T$-th magic, no more input follows.)

### Notes

**Flush after every output.** Otherwise, you may receive a TLE verdict.

If you read `-1`, exit immediately. If you do so, you will receive a WA verdict, but continuing leads to an indeterminate verdict. Unnecessary newlines may be seen as malformed.

The judge is **non‑adaptive**; the values $a_1, b_1, a_2, b_2, \dots, a_N, b_N, s$ are fixed before the interaction starts, and will not be affected by your output.

---

### Sample Interaction

Note that this is just an example of interaction.

| Input | Output | Comment |
| --- | --- | --- |
| `2 2` |  | The integers $N$ and $T$ are given from Standard Input. |
|  | `ABABAB` | Print letters for the backs of the six cards. |
| `A` |  | The first magic begins. The host declares `A`. |
| `1 2` |  | The host tells you $(a_1, b_1) = (1, 2)$. |
|  | `2` | Set the second character to `1`. |
| `3 4` |  | The host tells you $(a_2, b_2) = (3, 4)$. |
|  | `3` | Set the third character to `1`. The final string is `0110`, and the card with `0110` on its front has `A` on the back, so the magic succeeds. |
| `B` |  | The second magic begins. The host declars `B`. |
| `1 3` |  | The host tells you $(a_1, b_1) = (1, 3)$. |
|  | `1` | Set the first character to `1`. |
| `2 4` |  | The host tells you $(a_2, b_2) = (2, 4)$. |
|  | `2` | Set the second character to `1`. The final string is `1100`, and the card with `1100` on its front has `B` on the back, so the magic succeeds. |

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;


static long long queries = 0;
static long long query_limit = 0;
static bool finished = false;
static long long C[25][25]; 


static void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}


static void finish(TResult verdict, const string& msg) {
    if (finished) return;
    finished = true;
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}


void precompute() {
    for (int i = 0; i <= 22; ++i) {
        C[i][0] = 1;
        for (int j = 1; j <= i; ++j) {
            C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
}




long long get_rank(int N, const vector<int>& chosen_bits) {
    long long r = 1;
    int zeros = N;
    int ones = N;
    
    for (int i = 1; i <= 2 * N; ++i) {
        if (chosen_bits[i] == 0) {
            zeros--;
        } else {
            
            
            
            if (zeros > 0) {
                r += C[zeros - 1 + ones][ones];
            }
            ones--;
        }
    }
    return r;
}

int main(int argc, char** argv) {
    
    registerInteraction(argc, argv);
    atexit(log_metrics);
    precompute();

    
    int N = inf.readInt(2, 10, "N");
    int T = inf.readInt(1, 5000, "T");

    
    query_limit = T;
    log_metrics();

    
    cout << N << " " << T << endl;

    
    
    long long K = C[2 * N][N];
    string user_S;
    
    
    if (!(cin >> user_S)) {
        finish(_pe, "Could not read initial string (EOF)");
    }

    if ((long long)user_S.length() != K) {
        
        cout << "-1" << endl;
        finish(_wa, format("String length mismatch. Expected %lld, got %d", K, (int)user_S.length()));
    }

    
    for (int i = 0; i < (int)user_S.length(); ++i) {
        if (user_S[i] < 'A' || user_S[i] >= 'A' + N) {
            cout << "-1" << endl;
            finish(_wa, format("Invalid character '%c' at index %d. Expected 'A'..'%c'", 
                               user_S[i], i, (char)('A' + N - 1)));
        }
    }

    
    for (int t = 1; t <= T; ++t) {
        
        string s_token = inf.readToken(); 
        if (s_token.empty()) finish(_fail, "Generator output ended prematurely (reading s)");
        char s_char = s_token[0];

        
        vector<pair<int, int>> pairs(N);
        for (int i = 0; i < N; ++i) {
            pairs[i].first = inf.readInt(1, 2 * N, "u");
            pairs[i].second = inf.readInt(1, 2 * N, "v");
        }

        
        cout << s_char << endl;

        
        vector<int> board(2 * N + 1, 0); 
        for (int i = 0; i < N; ++i) {
            int u = pairs[i].first;
            int v = pairs[i].second;

            
            cout << u << " " << v << endl;

            
            string token;
            if (!(cin >> token)) {
                finish(_pe, "Unexpected EOF while reading choice");
            }
            
            
            if (token == "?" || token == "!") {
                 if (!(cin >> token)) finish(_pe, "Unexpected EOF after special token");
            }

            int d = -1;
            try {
                size_t idx;
                d = stoi(token, &idx);
                
                if (idx != token.length()) {
                     cout << "-1" << endl;
                     finish(_pe, format("Malformed integer: %s", token.c_str()));
                }
            } catch (...) {
                cout << "-1" << endl;
                finish(_pe, format("Expected integer, got: %s", token.c_str()));
            }

            
            if (d != u && d != v) {
                cout << "-1" << endl;
                finish(_wa, format("Invalid choice %d. Expected %d or %d", d, u, v));
            }
            
            board[d] = 1;
        }

        
        long long rank = get_rank(N, board);
        if (rank < 1 || rank > K) {
             finish(_fail, format("Logic error: Calculated rank %lld out of bounds [1, %lld]", rank, K));
        }

        
        char user_char = user_S[rank - 1];
        if (user_char != s_char) {
            cout << "-1" << endl;
            finish(_wa, format("Magic %d failed. Target '%c', got '%c' (from rank %lld string)", 
                               t, s_char, user_char, rank));
        }

        queries++;
        
        if (queries % 10 == 0) log_metrics();
    }

    finish(_ok, format("Passed %d magics", T));
    return 0;
}


Adaptive interactor (C++):

Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);

    
    string mode = opt<string>("mode", "non");

    
    int N = rnd.next(2, 10);
    int T = rnd.next(1, 5000);

    
    
    string seed = argv[1];
    if (seed == "1") {
        N = 2; T = 2;       
    } else if (seed == "2") {
        N = 10; T = 5;      
    } else if (seed == "3") {
        N = 10; T = 5000;   
    } else if (seed == "4") {
        N = 5; T = 100;     
    } else if (seed == "5") {
        N = 2; T = 5000;    
    } else {
        
        if (rnd.next(3) == 0) N = 10;
        if (rnd.next(3) == 0) T = 5000;
    }

    
    if (opt<int>("n", 0) != 0) N = opt<int>("n");
    if (opt<int>("t", 0) != 0) T = opt<int>("t");

    
    cout << N << " " << T << endl;

    if (mode == "non") {
        
        for (int t = 0; t < T; ++t) {
            
            char s = (char)('A' + rnd.next(N));
            cout << s << endl;

            
            
            vector<int> p(2 * N);
            iota(p.begin(), p.end(), 1);
            shuffle(p.begin(), p.end());

            
            for (int i = 0; i < N; ++i) {
                int u = p[2 * i];
                int v = p[2 * i + 1];
                if (u > v) swap(u, v); 
                cout << u << " " << v << endl;
            }
        }
    }
    
    

    return 0;
}