Problem: cf2096_g
Difficulty: medium
Categories: bit, math
Time limit: 2000 ms, memory limit: 256 MB

This is an interactive problem.

You are a proud teacher at the Millennium Science School. Today, a student named Alice challenges you to a guessing game.

Alice is thinking of an integer from $1$ to $n$, and you must guess it by asking her some queries.

To make things harder, she says you must ask all the queries first, and she will ignore exactly $1$ query.

For each query, you choose an array of $k$ distinct integers from $1$ to $n$, where $k$ is even. Then, Alice will respond with one of the following:

* $\texttt{L}$: the number is one of the first $\frac{k}{2}$ elements of the array;
* $\texttt{R}$: the number is one of the last $\frac{k}{2}$ elements of the array;
* $\texttt{N}$: the number is not in the array;
* $\texttt{?}$: this query is ignored.

Alice is impatient, so you must find a strategy that minimizes the number of queries. Can you do it?

Formally, let $f(n)$ be the minimum number of queries required to determine Alice's number. Then you must find a strategy that uses exactly $f(n)$ queries.

Note that the interactor is adaptive, which means Alice's number is not fixed at the beginning and may depend on your queries. However, it is guaranteed that there exists at least one number that is consistent with Alice's responses.

We can show that $f(n) \leq 20$ for all $n$ such that $2 \le n \le 2 \cdot 10^5$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The only line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the maximum possible value of Alice's number.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Interaction

The interaction begins by reading the integer $n$.

Then, output a single integer $q$ ($1 \leq q \leq 20$) — the number of queries.

To ask a query, output a line in the following format:

* $k\,a_1\,a_2 \ldots a_k$ ($2 \leq k \leq n$, $k$ is even, $1 \leq a_i \leq n$, the $a_i$ are distinct) — the length of the array, and the array itself.

Once you've asked all $q$ queries, read a string $s$ ($|s| = q$) — the responses to the queries as described above.

When you know Alice's number, output a single integer $x$ ($1 \leq x \leq n$) — the value of the number.

Then, move on to the next test case, or terminate the program if there are no more test cases.

After outputting all $q$ queries, do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* See the documentation for other languages.

Note that even if you correctly determine Alice's number but use more than $f(n)$ queries, you will get Wrong answer.

Example

Input

```
2
3

?N

5

R?L
```

Output

```
2
2 1 2
2 1 2

3

3
4 3 2 4 1
4 5 4 3 1
4 1 5 3 4

1
```

Note

In the first test case, $n = 3$. We ask $2$ queries: $[1, 2]$, and $[1, 2]$ again.

* For the first query, Alice's response is $\texttt{?}$, which means this query is ignored.
* For the second query, Alice's response is $\texttt{N}$, which means her number is not in the array $[1, 2]$.

From the information above, we can determine that Alice's number is $3$.

It can be shown that all valid strategies for $n = 3$ require at least $2$ queries.

In the second test case, $n = 5$. We ask $3$ queries: $[3, 2, 4, 1]$, $[5, 4, 3, 1]$, and $[1, 5, 3, 4]$.

* For the first query, Alice's response is $\texttt{R}$, which means her number is in the array $[4, 1]$.
* For the second query, Alice's response is $\texttt{?}$, which means this query is ignored.
* For the third query, Alice's response is $\texttt{L}$, which means her number is in the array $[1, 5]$.

From the information above, we can determine that Alice's number is $1$.

It can be shown that all valid strategies for $n = 5$ require at least $3$ queries.

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 void log_metrics() {
    try {
        tout << "queries=" << queries << "\n";
        tout << "query_limit=" << query_limit << "\n";
        tout.flush();
    } catch (...) {
        
    }
}

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

static bool readTokenFromContestant(string& tok) {
    tok.clear();
    if (!(cin >> tok)) return false;
    return true;
}

static bool parseLL(const string& s, long long& out) {
    if (s.empty()) return false;
    int i = 0;
    if (s[i] == '+') i++;
    if (i >= (int)s.size()) return false;
    bool neg = false;
    if (s[i] == '-') return false;
    long long val = 0;
    for (; i < (int)s.size(); i++) {
        unsigned char c = (unsigned char)s[i];
        if (c < '0' || c > '9') return false;
        int d = c - '0';
        if (val > (LLONG_MAX - d) / 10) return false;
        val = val * 10 + d;
    }
    out = neg ? -val : val;
    return true;
}

static long long readLLOrPE(const string& what) {
    string tok;
    if (!readTokenFromContestant(tok)) finish(_pe, "Unexpected EOF while reading " + what);
    long long v;
    if (!parseLL(tok, v)) finish(_pe, "Malformed integer token while reading " + what);
    return v;
}

static string readStringOrPE(const string& what) {
    string tok;
    if (!readTokenFromContestant(tok)) finish(_pe, "Unexpected EOF while reading " + what);
    return tok;
}

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

    int t = inf.readInt(1, 10000, "t");
    struct Case { int n; int x; int bad; };
    vector<Case> cases;
    cases.reserve(t);

    for (int i = 0; i < t; i++) {
        int n = inf.readInt(2, 200000, "n");
        int x = inf.readInt(1, n, "x");
        int bad = inf.readInt(1, 20, "bad");
        cases.push_back({n, x, bad});
    }

    query_limit = 20LL * t;
    log_metrics(); 

    cout << t << endl;

    long long hard_cap = max(200000LL, 10LL * query_limit);

    for (int tc = 0; tc < t; tc++) {
        int n = cases[tc].n;
        int hidden_x = cases[tc].x;
        int bad_seed = cases[tc].bad;

        cout << n << endl;

        long long qll = readLLOrPE("q");
        if (qll < 1 || qll > 20) finish(_pe, "q must be in [1,20]");
        int q = (int)qll;

        queries += q;
        if (queries > hard_cap) finish(_pe, "Hard cap exceeded (too many queries)");

        int ignored = 1 + (bad_seed - 1) % q; 

        string answers;
        answers.reserve(q);

        for (int i = 1; i <= q; i++) {
            long long kll = readLLOrPE("k");
            if (kll < 2 || kll > n) finish(_pe, "k out of range");
            if (kll % 2 != 0) finish(_pe, "k must be even");
            int k = (int)kll;

            vector<int> a(k);
            for (int j = 0; j < k; j++) {
                long long all = readLLOrPE("a_j");
                if (all < 1 || all > n) finish(_pe, "a_j out of range");
                a[j] = (int)all;
            }

            vector<int> b = a;
            sort(b.begin(), b.end());
            for (int j = 1; j < k; j++) {
                if (b[j] == b[j - 1]) finish(_pe, "Duplicate values in query array");
            }

            if (i == ignored) {
                answers.push_back('?');
                continue;
            }

            int pos = -1;
            for (int j = 0; j < k; j++) {
                if (a[j] == hidden_x) {
                    pos = j;
                    break;
                }
            }
            if (pos == -1) {
                answers.push_back('N');
            } else if (pos < k / 2) {
                answers.push_back('L');
            } else {
                answers.push_back('R');
            }
        }

        cout << answers << endl;

        long long guessll = readLLOrPE("answer x");
        if (guessll < 1 || guessll > n) finish(_pe, "Answer x out of range");
        int guess = (int)guessll;

        if (guess != hidden_x) finish(_wa, "Wrong answer");
    }

    finish(_ok, "OK");
    return 0;
}


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 void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}

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

static bool readLongLong(long long& out) {
    if (!(cin >> out)) return false;
    return true;
}

static int f_min_queries(int n) {
    
    long long pw = 1;
    int q = 1;
    while (pw < n) {
        pw *= 3;
        q++;
        if (q > 60) break;
    }
    return q;
}

static int symToDigit(char c) {
    if (c == 'L') return 0;
    if (c == 'R') return 1;
    return 2; 
}

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

    int t = 0;
    try {
        t = inf.readInt();
    } catch (...) {
        finish(_pe, "failed to read t from input");
    }
    if (t < 1 || t > 100000) finish(_pe, "t out of reasonable range");

    query_limit = 20LL * t;
    log_metrics(); 

    cout << t << "\n";
    cout.flush();

    const long long hard_cap = max(200000LL, 10LL * query_limit);

    for (int tc = 1; tc <= t; tc++) {
        int n = 0;
        try {
            n = inf.readInt();
        } catch (...) {
            finish(_pe, "failed to read n from input");
        }
        if (n < 2 || n > 200000) finish(_pe, "n out of range");

        cout << n << "\n";
        cout.flush();

        long long qll = 0;
        if (!readLongLong(qll)) finish(_pe, "failed to read q from contestant");
        if (qll < 1 || qll > 20) finish(_pe, "q must be in [1,20]");
        int q = (int)qll;

        int fq = f_min_queries(n);
        if (q != fq) finish(_wa, "wrong number of queries (must be minimal f(n))");

        queries += q;
        if (queries > hard_cap) finish(_pe, "too many queries (hard cap exceeded)");

        vector<vector<char>> code(n + 1, vector<char>(q, 'N'));

        for (int qi = 0; qi < q; qi++) {
            long long kll = 0;
            if (!readLongLong(kll)) finish(_pe, "failed to read k");
            if (kll < 2 || kll > n) finish(_pe, "k out of range");
            int k = (int)kll;
            if (k % 2 != 0) finish(_pe, "k must be even");

            vector<int> a(k);
            vector<int> seen(n + 1, 0);
            for (int i = 0; i < k; i++) {
                long long xll = 0;
                if (!readLongLong(xll)) finish(_pe, "failed to read array element");
                if (xll < 1 || xll > n) finish(_pe, "array element out of range");
                int x = (int)xll;
                if (seen[x]) finish(_pe, "array elements must be distinct");
                seen[x] = 1;
                a[i] = x;
            }

            int h = k / 2;
            for (int i = 0; i < h; i++) code[a[i]][qi] = 'L';
            for (int i = h; i < k; i++) code[a[i]][qi] = 'R';
        }

        int bestJ = 0;
        int bestRep = 1;
        int bestSize = -1;

        vector<pair<long long, int>> keys;
        keys.reserve(n);

        for (int j = 0; j < q; j++) {
            keys.clear();
            for (int x = 1; x <= n; x++) {
                long long key = 0;
                for (int i = 0; i < q; i++) {
                    if (i == j) continue;
                    key = key * 3 + symToDigit(code[x][i]);
                }
                keys.emplace_back(key, x);
            }
            sort(keys.begin(), keys.end(), [](const auto& p1, const auto& p2) {
                if (p1.first != p2.first) return p1.first < p2.first;
                return p1.second < p2.second;
            });

            for (int idx = 0; idx < n; ) {
                int nxt = idx + 1;
                while (nxt < n && keys[nxt].first == keys[idx].first) nxt++;
                int sz = nxt - idx;
                int rep = keys[idx].second; 
                if (sz > bestSize ||
                    (sz == bestSize && (j < bestJ || (j == bestJ && rep < bestRep)))) {
                    bestSize = sz;
                    bestJ = j;
                    bestRep = rep;
                }
                idx = nxt;
            }
        }

        string s(q, 'N');
        for (int i = 0; i < q; i++) s[i] = (i == bestJ ? '?' : code[bestRep][i]);

        cout << s << "\n";
        cout.flush();

        long long xll = 0;
        if (!readLongLong(xll)) finish(_pe, "failed to read final answer x");
        if (xll < 1 || xll > n) finish(_pe, "final answer x out of range");
        int x = (int)xll;

        bool inGroup = true;
        for (int i = 0; i < q; i++) {
            if (i == bestJ) continue;
            if (code[x][i] != code[bestRep][i]) {
                inGroup = false;
                break;
            }
        }

        if (!(bestSize == 1 && inGroup)) {
            finish(_wa, "answer not uniquely determined by the interaction");
        }
    }

    finish(_ok, "OK");
    return 0;
}


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

static int clampInt(long long x, int lo, int hi) {
    if (x < lo) return lo;
    if (x > hi) return hi;
    return (int)x;
}

static bool isPowerOfTwo(int x) {
    return x > 0 && (x & (x - 1)) == 0;
}

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

    string mode = opt<string>("mode", "non");
    int maxN = opt<int>("maxn", 200000);

    ensuref(maxN >= 2, "maxn must be >= 2");
    ensuref(mode == "non" || mode == "adp", "mode must be 'non' or 'adp'");

    int seed = 0;
    if (argc >= 2) seed = atoi(argv[1]);

    int n = 2;
    int x = 1;
    int bad = 1;

    if (seed == 1) {
        n = 2;
        x = 2;
        bad = 1;
    } else if (seed == 2) {
        n = 3;
        x = 3;
        bad = 2;
    } else if (seed == 3) {
        n = maxN;
        x = 1;
        bad = 20;
    } else {
        int pattern = rnd.next(0, 11);

        if (pattern <= 2) {
            
            int p = 1 << rnd.next(1, 17); 
            int delta = rnd.next(-7, 7);
            n = clampInt((long long)p + (long long)delta, 2, maxN);
            if (isPowerOfTwo(n)) {
                int tweak = rnd.next(0, 1) ? 1 : -1;
                n = clampInt((long long)n + tweak, 2, maxN);
            }
        } else if (pattern <= 5) {
            
            n = maxN - rnd.next(0, 5000);
            if (n < 2) n = 2;
        } else if (pattern <= 7) {
            
            n = maxN - rnd.next(0, 2000);
            if (n < 2) n = 2;
            if ((n & 1) == 0) n = clampInt((long long)n - 1, 2, maxN);
        } else {
            
            int lo = max(2, maxN - 40000);
            n = rnd.next(lo, maxN);
        }

        int xtype = rnd.next(0, 9);
        if (xtype <= 2) x = 1;
        else if (xtype <= 5) x = n;
        else if (xtype <= 7) x = (n + 1) / 2;
        else x = rnd.next(1, n);

        bad = rnd.next(1, 20);
    }

    
    cout << 1 << "\n";
    if (mode == "non") {
        cout << n << " " << x << " " << bad << "\n";
    } else {
        cout << n << "\n";
    }

    return 0;
}