Problem: cf2031_f
Difficulty: medium
Categories: math
Time limit: 3000 ms, memory limit: 256 MB

This is an interactive problem.

Returning from a restful vacation on Australia's Gold Coast, Penchick forgot to bring home gifts for his pet duck Duong Canh! But perhaps a beautiful problem crafted through deep thought on the scenic beaches could be the perfect souvenir.

There is a hidden permutation$^{\text{∗}}$ $p$ of length $n$, where $n$ is even. You are allowed to make the following query:

* Choose a subsequence$^{\text{†}}$ of the permutation $p$ with even length $4\le k\le n$. The interactor will return the value of the two medians$^{\text{‡}}$ in the chosen subsequence.

Find the index of the two medians in permutation $p$ using at most $80$ queries.

Note that the interactor is non-adaptive. This means that the permutation $p$ is fixed at the beginning and will not change based on your queries.

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

$^{\text{†}}$A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) element from arbitrary positions.

$^{\text{‡}}$The two medians of an array $a$ with even length $k$ are defined as the $\frac{k}{2}$-th and $\left(\frac{k}{2} + 1\right)$-th smallest element in the array ($1$-indexed).

Input

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

The only line of each test case contains a single integer $n$ ($6 \le n \le 100$, $n$ is even) — the length of the hidden permutation $p$.

For each test case, after reading the integer $n$, you should begin the interaction and find the answer before reading $n$ for the next test case.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.

Interaction

To make a query, print a single line in the following format:

* $\mathtt{?}\;k\;x_1\;x_2 \ldots x_{k-1}\;x_k$ ($4\le k\le n$, $k$ is even, $1 \le x_i \le n$, $x_i$ is pairwise distinct) — the length of the chosen subsequence followed by the indices of the chosen subsequence.

After each query, you should read a line containing two integers $m_1$ and $m_2$ ($1 \le m_1 < m_2 \le n$) — the value of the two medians in array $[p_{x_1}, p_{x_2}, \ldots, p_{x_{k-1}}, p_{x_k}]$.

You can make at most $80$ such queries in each test case.

To give the final answer, print a single line in the following format:

* $\mathtt{!}\;i_1\;i_2$ ($1\le i_1, i_2 \le n$) — the index of the two medians.

Note that the order in which $i_1$ and $i_2$ is printed does not matter. In other words, your solution is valid as long as $p_{i_1} = \frac{n}{2}$ and $p_{i_2} = \frac{n}{2} + 1$, or $p_{i_1} = \frac{n}{2} + 1$ and $p_{i_2} = \frac{n}{2}$.

After printing each query do not forget to output the end of line and flush$^{\text{∗}}$ the output. Otherwise, you will get Idleness limit exceeded verdict.

If, at any interaction step, you read $-1$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.

Example

Input

```
2
6

3 4

3 4

2 3

10

3 4

6 7
```

Output

```
? 6 1 2 3 4 5 6

? 4 3 6 1 5

? 4 3 6 2 5

! 3 6

? 6 1 3 7 8 9 10

? 8 1 2 3 4 5 6 7 8

! 6 5
```

Note

In the first test case, the hidden permutation is $p = [6, 2, 3, 5, 1, 4]$.

1. The entire permutation was chosen for the first query. The two medians of the entire permutation $p$ are $3$ and $4$.
2. The indices of the chosen subsequence in the second query are $3$, $6$, $1$, and $5$. The interactor returns the two medians of the subsequence $[p_3, p_6, p_1, p_5] = [3, 4, 6, 1]$, which are $3$ and $4$.
3. The indices of the chosen subsequence in the second query are $3$, $6$, $2$, and $5$. The interactor returns the two medians of the subsequence $[p_3, p_6, p_2, p_5] = [3, 4, 2, 1]$, which are $2$ and $3$.

The answer "! 3 6" is valid as $p_3 = 3$ and $p_6 = 4$.

In the second test case, the hidden permutation is $p = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]$.

1. The indices of the chosen subsequence in the second query are $1$, $3$, $7$, $8$, $9$, and $10$. The interactor returns the two medians of the subsequence $[p_1, p_3, p_7, p_8, p_9, p_{10}] = [10, 8, 4, 3, 2, 1]$, which are $3$ and $4$.
2. The indices of the chosen subsequence in the second query are $1$, $2$, $3$, $4$, $5$, $6$, $7$, and $8$. The interactor returns the two medians of the subsequence $[p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8] = [10, 9, 8, 7, 6, 5, 4, 3]$, which are $6$ and $7$.

The answer "! 5 6" is valid as $p_5 = 6$ and $p_6 = 5$.

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 << 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 isIntegerToken(const string& s) {
    if (s.empty()) return false;
    int i = 0;
    if (s[0] == '-') {
        if ((int)s.size() == 1) return false;
        i = 1;
    }
    for (; i < (int)s.size(); ++i) if (!isdigit((unsigned char)s[i])) return false;
    return true;
}

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

static bool readLongLong(long long& x) {
    string tok;
    if (!readToken(tok)) return false;
    if (!isIntegerToken(tok)) return false;
    try {
        size_t pos = 0;
        long long v = stoll(tok, &pos);
        if (pos != tok.size()) return false;
        x = v;
        return true;
    } catch (...) {
        return false;
    }
}

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

    int t = 0;
    t = inf.readInt(1, 1000, "t");

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

    
    log_metrics();

    vector<int> ns(t);
    vector<vector<int>> perms(t); 
    for (int tc = 0; tc < t; ++tc) {
        int n = inf.readInt(6, 100, "n");
        ensuref(n % 2 == 0, "n must be even");
        ns[tc] = n;
        perms[tc].assign(n + 1, 0);
        vector<int> seen(n + 1, 0);
        for (int i = 1; i <= n; ++i) {
            int v = inf.readInt(1, n, "p[i]");
            ensuref(!seen[v], "Permutation contains duplicates");
            seen[v] = 1;
            perms[tc][i] = v;
        }
    }

    cout << t << endl;

    for (int tc = 0; tc < t; ++tc) {
        int n = ns[tc];
        const auto& p = perms[tc];
        int needA = n / 2;
        int needB = needA + 1;

        cout << n << endl;

        bool answered = false;
        while (!answered) {
            string cmd;
            if (!readToken(cmd)) {
                finish(_pe, "Unexpected EOF from contestant");
            }

            if (cmd == "?") {
                long long kll = 0;
                if (!readLongLong(kll)) finish(_pe, "Malformed query: missing/invalid k");
                if (kll < 0 || kll > 1000000) finish(_pe, "Malformed query: k out of sane bounds");
                int k = (int)kll;

                if (k < 4 || k > n || (k % 2 != 0)) finish(_pe, "Invalid query: k must be even and in [4,n]");

                vector<int> idx(k);
                vector<char> used(n + 1, 0);
                for (int i = 0; i < k; ++i) {
                    long long xll = 0;
                    if (!readLongLong(xll)) finish(_pe, "Malformed query: missing/invalid index");
                    if (xll < 1 || xll > n) finish(_pe, "Invalid query: index out of range");
                    int x = (int)xll;
                    if (used[x]) finish(_pe, "Invalid query: duplicate indices");
                    used[x] = 1;
                    idx[i] = x;
                }

                queries++;
                if (queries > hard_cap) finish(_pe, "Hard cap exceeded");

                vector<int> vals;
                vals.reserve(k);
                for (int x : idx) vals.push_back(p[x]);
                nth_element(vals.begin(), vals.begin() + (k / 2) - 1, vals.end());
                int m1 = vals[(k / 2) - 1];
                nth_element(vals.begin(), vals.begin() + (k / 2), vals.end());
                int m2 = vals[(k / 2)];
                if (m1 > m2) swap(m1, m2);

                cout << m1 << " " << m2 << endl;
            } else if (cmd == "!") {
                long long i1ll = 0, i2ll = 0;
                if (!readLongLong(i1ll) || !readLongLong(i2ll)) finish(_pe, "Malformed answer: need two indices");
                if (i1ll < 1 || i1ll > n || i2ll < 1 || i2ll > n) finish(_pe, "Answer indices out of range");
                int i1 = (int)i1ll, i2 = (int)i2ll;
                if (i1 == i2) finish(_wa, "Answer indices must be distinct");

                int v1 = p[i1], v2 = p[i2];
                bool ok = (v1 == needA && v2 == needB) || (v1 == needB && v2 == needA);
                if (!ok) finish(_wa, "Wrong median indices");
                answered = true;
            } else {
                finish(_pe, "Unknown command from contestant");
            }
        }
    }

    finish(_ok, "All test cases solved");
    return 0;
}


Adaptive interactor (C++):

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

static vector<int> buildAlternatingExtremes(int n, int ex1, int ex2) {
    vector<int> vals;
    vals.reserve(n - 2);
    int lo = 1, hi = n;
    while (lo <= hi) {
        if (hi != ex1 && hi != ex2) vals.push_back(hi);
        hi--;
        if (lo <= hi + 1) {
            if (lo != ex1 && lo != ex2) vals.push_back(lo);
            lo++;
        }
    }
    return vals;
}

static vector<int> buildTwoBlocks(int n, int ex1, int ex2) {
    vector<int> hiBlock, loBlock;
    hiBlock.reserve(n);
    loBlock.reserve(n);
    for (int v = n; v >= 1; --v) {
        if (v == ex1 || v == ex2) continue;
        if (v > ex2) hiBlock.push_back(v);
        else if (v < ex1) loBlock.push_back(v);
        else {
            if (v < ex1) loBlock.push_back(v);
        }
    }
    vector<int> vals;
    vals.reserve(n - 2);
    for (int v : hiBlock) vals.push_back(v);
    for (int v : loBlock) vals.push_back(v);
    for (int v = ex2 - 1; v >= ex1 + 1; --v) {
        if (v == ex1 || v == ex2) continue;
        vals.push_back(v);
    }
    return vals;
}

static vector<int> buildBitReverseLike(int n, int ex1, int ex2) {
    vector<int> vals;
    vals.reserve(n - 2);
    int bits = 0;
    while ((1 << bits) < n) bits++;
    vector<int> used(n + 1, 0);
    used[ex1] = used[ex2] = 1;

    auto bitrev = [&](int x) -> int {
        int y = 0;
        for (int b = 0; b < bits; ++b) {
            y = (y << 1) | ((x >> b) & 1);
        }
        return y;
    };

    vector<pair<int,int>> order;
    order.reserve(n);
    for (int v = 1; v <= n; ++v) {
        if (used[v]) continue;
        order.push_back({bitrev(v), v});
    }
    sort(order.begin(), order.end());
    for (auto &pr : order) vals.push_back(pr.second);
    return vals;
}

static void chooseMedianPositions(int n, const string& placeMode, int &posA, int &posB) {
    if (placeMode == "adj") {
        int i = rnd.next(1, n - 1);
        if (i == n) i = n - 1;
        if (i == n) i = n - 1;
        posA = i;
        posB = i + 1;
    } else if (placeMode == "far") {
        posA = 1;
        posB = n;
    } else if (placeMode == "ends") {
        posA = 1;
        posB = n;
    } else if (placeMode == "rand") {
        posA = rnd.next(1, n);
        do {
            posB = rnd.next(1, n);
        } while (posB == posA);
    } else {
        int t = rnd.next(1, 100);
        if (t <= 40) {
            int i = rnd.next(1, n - 1);
            posA = i;
            posB = i + 1;
        } else if (t <= 70) {
            posA = 1;
            posB = n;
        } else {
            posA = rnd.next(1, n);
            do {
                posB = rnd.next(1, n);
            } while (posB == posA);
        }
    }
    if (posA > posB) swap(posA, posB);
}

static vector<int> makePermutation(int n, int seed, int pattern, const string& placeMode) {
    int a = n / 2;
    int b = a + 1;

    int posA = 1, posB = n;
    chooseMedianPositions(n, placeMode, posA, posB);

    vector<int> remaining;
    if (pattern == 0) remaining = buildAlternatingExtremes(n, a, b);
    else if (pattern == 1) {
        remaining = buildAlternatingExtremes(n, a, b);
        
        int swaps = rnd.next(0, min(200, (int)remaining.size() * 3 + 5));
        for (int s = 0; s < swaps; ++s) {
            int i = rnd.next(0, (int)remaining.size() - 1);
            int j = rnd.next(0, (int)remaining.size() - 1);
            swap(remaining[i], remaining[j]);
        }
    } else if (pattern == 2) remaining = buildTwoBlocks(n, a, b);
    else remaining = buildBitReverseLike(n, a, b);

    
    int revs = rnd.next(0, 6);
    for (int r = 0; r < revs; ++r) {
        if ((int)remaining.size() <= 2) break;
        int L = rnd.next(0, (int)remaining.size() - 2);
        int R = rnd.next(L + 1, (int)remaining.size() - 1);
        reverse(remaining.begin() + L, remaining.begin() + R + 1);
    }

    vector<int> p(n + 1, 0);
    p[posA] = a;
    p[posB] = b;

    int idx = 0;
    for (int i = 1; i <= n; ++i) {
        if (i == posA || i == posB) continue;
        if (idx >= (int)remaining.size()) {
            
            vector<int> used(n + 1, 0);
            for (int j = 1; j <= n; ++j) if (p[j]) used[p[j]] = 1;
            for (int v = 1; v <= n; ++v) {
                if (!used[v]) {
                    p[i] = v;
                    used[v] = 1;
                    break;
                }
            }
        } else {
            p[i] = remaining[idx++];
        }
    }

    
    vector<int> seen(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
        ensuref(1 <= p[i] && p[i] <= n, "Permutation out of range");
        ensuref(!seen[p[i]], "Duplicate value in permutation");
        seen[p[i]] = 1;
    }

    vector<int> out(n);
    for (int i = 1; i <= n; ++i) out[i - 1] = p[i];
    return out;
}

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

    string mode = opt<string>("mode", "non");
    int nOpt = opt<int>("n", -1);
    string placeMode = opt<string>("place", "mix");
    int patternOpt = opt<int>("pattern", -1);

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

    int n = 0;
    vector<int> perm;

    if (seed == 1) {
        n = (nOpt != -1 ? nOpt : 6);
        ensuref(n % 2 == 0 && n >= 6 && n <= 100, "n must be even and in [6,100]");
        
        
        perm = {6, 3, 1, 5, 4, 2};
        if ((int)perm.size() != n) {
            perm = makePermutation(n, seed, 0, "rand");
        }
    } else if (seed == 2) {
        n = (nOpt != -1 ? nOpt : 100);
        ensuref(n % 2 == 0 && n >= 6 && n <= 100, "n must be even and in [6,100]");
        
        perm = makePermutation(n, seed, 0, "ends");
    } else if (seed == 3) {
        n = (nOpt != -1 ? nOpt : 10);
        ensuref(n % 2 == 0 && n >= 6 && n <= 100, "n must be even and in [6,100]");
        
        perm = makePermutation(n, seed, 2, "adj");
    } else {
        if (nOpt != -1) {
            n = nOpt;
            ensuref(n % 2 == 0 && n >= 6 && n <= 100, "n must be even and in [6,100]");
        } else {
            int half = rnd.next(3, 50);
            n = half * 2;
        }
        int pattern = patternOpt;
        if (pattern == -1) {
            int r = rnd.next(1, 100);
            if (r <= 45) pattern = 0;
            else if (r <= 70) pattern = 1;
            else if (r <= 85) pattern = 2;
            else pattern = 3;
        }
        perm = makePermutation(n, seed, pattern, placeMode);
    }

    
    cout << 1 << "\n";
    if (mode == "non") {
        cout << n << "\n";
        for (int i = 0; i < n; ++i) {
            if (i) cout << ' ';
            cout << perm[i];
        }
        cout << "\n";
    } else if (mode == "adp") {
        
        cout << n << "\n";
    } else {
        quitf(_fail, "Unknown mode: %s", mode.c_str());
    }

    return 0;
}