The graph is called tree if it is connected and has no cycles. Suppose the tree is rooted at some vertex. Then tree is called to be perfect $k$-ary tree if each vertex is either a leaf (has no children) or has exactly $k$ children. Also, in perfect $k$-ary tree all leafs must have same depth.

For example, the picture below illustrates perfect binary tree with $15$ vertices:

![](https://espresso.codeforces.com/469a96d5903beaaabeb929d8ba57ebf7a548b61d.png)

There is a perfect $k$-ary tree with $n$ nodes. The nodes are labeled with distinct integers from $1$ to $n$, however you don't know how nodes are labelled. Still, you want to find the label of the root of the tree.

You are allowed to make at most $60 \cdot n$ queries of the following type:

* "? $a$ $b$ $c$", the query returns "Yes" if node with label $b$ lies on the path from $a$ to $c$ and "No" otherwise.

Both $a$ and $c$ are considered to be lying on the path from $a$ to $c$.

When you are ready to report the root of the tree, print

* "! $s$", where $s$ is the label of the root of the tree.

It is possible to report the root only once and this query is not counted towards limit of $60 \cdot n$ queries.

Interaction

The first line of the standard input stream contains two integers $n$ and $k$ ($3 \le n \le 1500$, $2 \le k < n$) — the number of nodes in the tree and the value of $k$.

It is guaranteed that $n$ is such that the tree forms a perfect $k$-ary tree.

You can ask at most $60 \cdot n$ queries. To ask a query, print a line of form "? $a$ $b$ $c$", where $1 \le a, b, c \le n$. After that you should read a single line containing "Yes" or "No" depending on the answer of the query.

The tree is fixed for each test and it doesn't depend on your queries.

When you are ready to print the answer, print a line of the form "! $s$", where $s$ is the label of the root vertex and then terminate your program.

After printing each query do not forget to print end of line and flush the output. Otherwise you may 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 documentation for other languages.

In case your program will make more than $60 \cdot n$ queries, but in other aspects would follow the interaction protocol and terminate coorectly, it will get verdict «Wrong Answer».

Example

Input

```
3 2

No

Yes
```

Output

```
? 1 3 2

? 1 2 3

! 2
```

Note

The tree in the example is as follows:

![](https://espresso.codeforces.com/855f62ae639f7f71a390a75d5202929cafd40070.png)

The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity).

#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 isDigits(const string& s) {
    if (s.empty()) return false;
    for (char ch : s) if (ch < '0' || ch > '9') return false;
    return true;
}

static int readIntTokenInRange(int n, const char* what) {
    string tok;
    if (!(cin >> tok)) finish(_pe, string("Unexpected EOF while reading ") + what);
    if (!isDigits(tok)) finish(_pe, string("Non-integer token for ") + what);
    long long v = 0;
    try {
        v = stoll(tok);
    } catch (...) {
        finish(_pe, string("Bad integer token for ") + what);
    }
    if (v < 1 || v > n) finish(_pe, string("Out of range value for ") + what);
    return (int)v;
}

struct LCA {
    int n = 0, LOG = 0;
    vector<int> depth;
    vector<vector<int>> up;

    LCA() {}
    LCA(int n_, const vector<int>& parent, const vector<int>& depth_) { init(n_, parent, depth_); }

    void init(int n_, const vector<int>& parent, const vector<int>& depth_) {
        n = n_;
        depth = depth_;
        LOG = 1;
        while ((1 << LOG) <= n) LOG++;
        up.assign(LOG, vector<int>(n + 1, 0));
        for (int v = 1; v <= n; v++) up[0][v] = parent[v];
        for (int j = 1; j < LOG; j++) {
            for (int v = 1; v <= n; v++) {
                int mid = up[j - 1][v];
                up[j][v] = (mid == 0 ? 0 : up[j - 1][mid]);
            }
        }
    }

    int lift(int v, int dist) const {
        for (int j = 0; j < LOG; j++) {
            if (dist & (1 << j)) v = up[j][v];
            if (v == 0) break;
        }
        return v;
    }

    int lca(int a, int b) const {
        if (a == 0 || b == 0) return 0;
        if (depth[a] < depth[b]) swap(a, b);
        a = lift(a, depth[a] - depth[b]);
        if (a == b) return a;
        for (int j = LOG - 1; j >= 0; j--) {
            if (up[j][a] != up[j][b]) {
                a = up[j][a];
                b = up[j][b];
            }
        }
        return up[0][a];
    }

    int dist(int a, int b) const {
        int c = lca(a, b);
        if (c == 0) return 0;
        return depth[a] + depth[b] - 2 * depth[c];
    }
};

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

    int n = inf.readInt(3, 1500, "n");
    int k = inf.readInt(2, n - 1, "k");

    vector<int> perm(n + 1, 0);
    bool hasPerm = true;
    for (int i = 1; i <= n; i++) {
        if (inf.eof()) { hasPerm = false; break; }
        perm[i] = inf.readInt(1, n, "perm[i]");
    }
    if (!hasPerm) {
        for (int i = 1; i <= n; i++) perm[i] = i;
    }

    vector<int> inv(n + 1, 0);
    vector<int> seen(n + 1, 0);
    for (int v = 1; v <= n; v++) {
        int lab = perm[v];
        if (lab < 1 || lab > n) finish(_pe, "Hidden permutation out of range");
        if (seen[lab]) finish(_pe, "Hidden permutation not a permutation");
        seen[lab] = 1;
        inv[lab] = v;
    }

    vector<int> parent(n + 1, 0), depth(n + 1, 0);
    parent[1] = 0;
    depth[1] = 0;
    for (int v = 2; v <= n; v++) {
        parent[v] = (v - 2) / k + 1;
        depth[v] = depth[parent[v]] + 1;
    }
    LCA lca(n, parent, depth);

    int rootLabel = perm[1];

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

    log_metrics();

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

    auto on_path = [&](int aLab, int bLab, int cLab) -> bool {
        int a = inv[aLab];
        int b = inv[bLab];
        int c = inv[cLab];
        int dab = lca.dist(a, b);
        int dbc = lca.dist(b, c);
        int dac = lca.dist(a, c);
        return dac == dab + dbc;
    };

    while (true) {
        string cmd;
        if (!(cin >> cmd)) finish(_pe, "Unexpected EOF (no final answer)");

        if (cmd == "?") {
            int a = readIntTokenInRange(n, "a");
            int b = readIntTokenInRange(n, "b");
            int c = readIntTokenInRange(n, "c");

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

            bool ans = on_path(a, b, c);
            cout << (ans ? "Yes" : "No") << "\n";
            cout.flush();
        } else if (cmd == "!") {
            int s = readIntTokenInRange(n, "s");
            if (s == rootLabel) finish(_ok, "Correct");
            finish(_wa, "Wrong root");
        } else {
            finish(_pe, "Invalid command token");
        }
    }
}

#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());
}

struct Tree {
    int n = 0, k = 0;
    int LOG = 0;
    vector<int> parent, depth;
    vector<vector<int>> up;

    void build(int n_, int k_) {
        n = n_;
        k = k_;
        parent.assign(n + 1, 0);
        depth.assign(n + 1, 0);

        for (int v = 2; v <= n; v++) parent[v] = (v - 2) / k + 1;
        depth[1] = 0;
        for (int v = 2; v <= n; v++) depth[v] = depth[parent[v]] + 1;

        LOG = 1;
        while ((1 << LOG) <= n) LOG++;
        up.assign(LOG, vector<int>(n + 1, 0));
        for (int v = 1; v <= n; v++) up[0][v] = parent[v];
        for (int j = 1; j < LOG; j++) {
            for (int v = 1; v <= n; v++) {
                int mid = up[j - 1][v];
                up[j][v] = (mid ? up[j - 1][mid] : 0);
            }
        }
    }

    int lca(int a, int b) const {
        if (a == 0 || b == 0) return a ^ b;
        if (depth[a] < depth[b]) swap(a, b);
        int diff = depth[a] - depth[b];
        for (int j = 0; j < LOG; j++) if (diff & (1 << j)) a = up[j][a];
        if (a == b) return a;
        for (int j = LOG - 1; j >= 0; j--) {
            if (up[j][a] != up[j][b]) {
                a = up[j][a];
                b = up[j][b];
            }
        }
        return parent[a];
    }

    int dist(int a, int b) const {
        int c = lca(a, b);
        return depth[a] + depth[b] - 2 * depth[c];
    }

    bool onPath(int a, int b, int c) const {

        return dist(a, c) == dist(a, b) + dist(b, c);
    }
};

struct AdaptiveLabeling {
    int n = 0;
    Tree* tr = nullptr;

    vector<int> labelToNode;
    vector<int> nodeToLabel;
    vector<int> assignOrderNodes;
    int ptr = 0;

    void init(int n_, Tree* tr_) {
        n = n_;
        tr = tr_;
        labelToNode.assign(n + 1, 0);
        nodeToLabel.assign(n + 1, 0);
        ptr = 0;

        vector<int> nodes;
        nodes.reserve(n);
        for (int v = 2; v <= n; v++) nodes.push_back(v);
        sort(nodes.begin(), nodes.end(), [&](int a, int b) {
            if (tr->depth[a] != tr->depth[b]) return tr->depth[a] > tr->depth[b];
            return a > b;
        });
        assignOrderNodes = nodes;
        assignOrderNodes.push_back(1);
    }

    int nextUnusedNode() {
        while (ptr < (int)assignOrderNodes.size() && nodeToLabel[assignOrderNodes[ptr]] != 0) ptr++;
        if (ptr >= (int)assignOrderNodes.size()) return 0;
        int v = assignOrderNodes[ptr++];
        if (nodeToLabel[v] != 0) return nextUnusedNode();
        return v;
    }

    void ensureAssigned(int label) {
        if (label < 1 || label > n) finish(_pe, "label out of range");
        if (labelToNode[label] != 0) return;
        int v = nextUnusedNode();
        if (v == 0) finish(_pe, "no unused nodes left for assignment");
        labelToNode[label] = v;
        nodeToLabel[v] = label;
    }

    void completeAllLabelsDeterministically() {
        for (int lab = 1; lab <= n; lab++) ensureAssigned(lab);

        for (int v = 1; v <= n; v++) {
            if (nodeToLabel[v] == 0) finish(_pe, "completion failed (unused node remains)");
        }
    }

    int rootLabel() const {
        return nodeToLabel[1];
    }
};

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

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

    int n = 0, k = 0;
    try {
        n = inf.readInt();
        k = inf.readInt();
    } catch (...) {
        finish(_pe, "failed to read n,k from inf");
    }
    if (n < 3 || n > 1500) finish(_pe, "n out of range");
    if (k < 2 || k >= n) finish(_pe, "k out of range");

    query_limit = 60LL * n;
    log_metrics();

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

    Tree tr;
    tr.build(n, k);

    AdaptiveLabeling labeling;
    labeling.init(n, &tr);

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

    while (true) {
        string tok;
        if (!(cin >> tok)) finish(_pe, "unexpected EOF (no final answer)");

        if (tok == "?") {
            long long aLL, bLL, cLL;
            if (!readLongLongSafe(aLL) || !readLongLongSafe(bLL) || !readLongLongSafe(cLL)) {
                finish(_pe, "malformed query arguments");
            }
            if (aLL < 1 || aLL > n || bLL < 1 || bLL > n || cLL < 1 || cLL > n) {
                finish(_pe, "query label out of range");
            }
            int a = (int)aLL, b = (int)bLL, c = (int)cLL;

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

            labeling.ensureAssigned(a);
            labeling.ensureAssigned(b);
            labeling.ensureAssigned(c);

            int na = labeling.labelToNode[a];
            int nb = labeling.labelToNode[b];
            int nc = labeling.labelToNode[c];

            bool ans = tr.onPath(na, nb, nc);

            cout << (ans ? "Yes" : "No") << "\n";
            cout.flush();
            continue;
        }

        if (tok == "!") {
            long long sLL;
            if (!readLongLongSafe(sLL)) finish(_pe, "malformed final answer");
            if (sLL < 1 || sLL > n) finish(_pe, "final answer out of range");
            int s = (int)sLL;

            labeling.ensureAssigned(s);
            labeling.completeAllLabelsDeterministically();

            int r = labeling.rootLabel();
            if (r == 0) finish(_pe, "root label not assigned after completion");

            if (s == r) finish(_ok, "correct");
            finish(_wa, "wrong root");
        }

        finish(_pe, "unknown command token");
    }
}

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

struct Cand {
    int n;
    int k;
    int h;
    long long score;
};

static int computeHeightIfPerfect(int n, int k) {
    long long total = 1;
    long long level = 1;
    int h = 0;
    while (total < n) {
        if (level > (LLONG_MAX / k)) return -1;
        level *= k;
        total += level;
        h++;
        if (h > 60) return -1;
    }
    if (total != n) return -1;
    return h;
}

static vector<Cand> allCandidatesUpTo(int maxn) {
    vector<Cand> cands;
    for (int k = 2; k <= maxn - 1; k++) {
        long long total = 1;
        long long level = 1;
        int h = 0;
        while (true) {
            if (total >= 3 && total <= maxn) {
                int n = (int)total;
                if (k < n) {
                    long long score = 1000000LL * n + 1000LL * h - k;
                    cands.push_back({n, k, h, score});
                }
            }
            if (total > maxn) break;
            if (level > (LLONG_MAX / k)) break;
            level *= k;
            total += level;
            h++;
            if (total > maxn) break;
            if (h > 60) break;
        }
    }
    sort(cands.begin(), cands.end(), [](const Cand& a, const Cand& b) {
        if (a.score != b.score) return a.score > b.score;
        if (a.h != b.h) return a.h > b.h;
        if (a.n != b.n) return a.n > b.n;
        return a.k < b.k;
    });
    return cands;
}

static vector<int> buildAdversarialPermutationLabelsByNode(int n, int k, int seed) {
    vector<int> parent(n + 1, 0), depth(n + 1, 0);
    for (int v = 2; v <= n; v++) parent[v] = (v - 2) / k + 1;
    int height = 0;
    for (int v = 2; v <= n; v++) {
        depth[v] = depth[parent[v]] + 1;
        height = max(height, depth[v]);
    }

    vector<int> rootChild(n + 1, 1);
    for (int v = 2; v <= n; v++) {
        int u = v;
        while (parent[u] != 1) u = parent[u];
        rootChild[v] = u;
    }

    vector<vector<vector<int>>> buckets;
    buckets.assign(k, vector<vector<int>>(height + 1));
    for (int v = 2; v <= n; v++) {
        int rc = rootChild[v];
        int idx = rc - 2;
        if (0 <= idx && idx < k) buckets[idx][depth[v]].push_back(v);
    }
    for (int i = 0; i < k; i++) {
        for (int d = 1; d <= height; d++) sort(buckets[i][d].begin(), buckets[i][d].end());
    }

    vector<int> order;
    order.reserve(n - 1);

    for (int d = height; d >= 1; d--) {
        bool takeBack = ((d + seed) & 1) == 0;
        while (true) {
            bool any = false;
            for (int idx = 0; idx < k; idx++) {
                auto &vec = buckets[idx][d];
                if (!vec.empty()) {
                    any = true;
                    int v;
                    if (takeBack) {
                        v = vec.back();
                        vec.pop_back();
                    } else {
                        v = vec.front();
                        vec.erase(vec.begin());
                    }
                    order.push_back(v);
                    if ((int)order.size() == n - 1) break;
                }
            }
            if (!any || (int)order.size() == n - 1) break;
        }
        if ((int)order.size() == n - 1) break;
    }

    if ((int)order.size() != n - 1) {
        vector<char> used(n + 1, 0);
        for (int v : order) used[v] = 1;
        for (int v = 2; v <= n; v++) if (!used[v]) order.push_back(v);
    }

    int rootLabel = n;
    vector<int> labelForNode(n + 1, 0);
    labelForNode[1] = rootLabel;

    vector<int> avail;
    avail.reserve(n - 1);
    for (int x = 1; x <= n; x++) if (x != rootLabel) avail.push_back(x);

    for (int i = 0; i < n - 1; i++) labelForNode[order[i]] = avail[i];

    vector<int> perm(n + 1, 0);
    for (int i = 1; i <= n; i++) perm[i] = labelForNode[i];
    return perm;
}

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

    string mode = opt<string>("mode", "non");
    int maxn = opt<int>("maxn", 1500);
    int nOpt = opt<int>("n", 0);
    int kOpt = opt<int>("k", 0);

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

    int n = 0, k = 0;

    if (nOpt > 0 && kOpt > 0) {
        n = nOpt;
        k = kOpt;
        ensuref(3 <= n && n <= maxn, "n out of range");
        ensuref(2 <= k && k < n, "k out of range");
        int h = computeHeightIfPerfect(n, k);
        ensuref(h >= 1, "n is not a perfect k-ary tree size for given k");
    } else {
        if (seed == 1) {
            n = 3; k = 2;
        } else if (seed == 2) {
            n = min(maxn, 1500);
            k = n - 1;
            ensuref(k >= 2, "maxn too small");
        } else if (seed == 3) {
            n = 1023; k = 2;
            if (n > maxn) {
                auto cands = allCandidatesUpTo(maxn);
                ensuref(!cands.empty(), "no valid candidates for maxn");
                for (auto &c : cands) {
                    if (c.k == 2) { n = c.n; k = c.k; break; }
                }
                if (n == 0) { n = cands[0].n; k = cands[0].k; }
            }
        } else {
            auto cands = allCandidatesUpTo(maxn);
            ensuref(!cands.empty(), "no valid candidates for maxn");
            int pickType = rnd.next(0, 99);
            if (pickType < 75) {
                int top = min<int>(60, (int)cands.size());
                int idx = rnd.next(0, top - 1);
                n = cands[idx].n;
                k = cands[idx].k;
            } else {
                int idx = rnd.next(0, (int)cands.size() - 1);
                n = cands[idx].n;
                k = cands[idx].k;
            }
        }
    }

    ensuref(3 <= n && n <= maxn, "internal n out of range");
    ensuref(2 <= k && k < n, "internal k out of range");
    ensuref(computeHeightIfPerfect(n, k) >= 1, "internal n,k not perfect");

    if (mode == "adp") {
        cout << n << " " << k << "\n";
        return 0;
    }

    ensuref(mode == "non", "unknown mode (use --mode=non or --mode=adp)");

    vector<int> perm;
    if (seed == 1 && n == 3 && k == 2) {

        perm.assign(n + 1, 0);
        perm[1] = 2;
        perm[2] = 1;
        perm[3] = 3;
    } else if (seed == 2 && k == n - 1) {
        perm.assign(n + 1, 0);
        perm[1] = n;
        for (int v = 2; v <= n; v++) perm[v] = v - 1;
    } else {
        perm = buildAdversarialPermutationLabelsByNode(n, k, seed);
    }

    vector<int> seen(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        ensuref(1 <= perm[i] && perm[i] <= n, "perm out of range");
        ensuref(!seen[perm[i]], "perm not a permutation");
        seen[perm[i]] = 1;
    }

    cout << n << " " << k << "\n";
    for (int i = 1; i <= n; i++) {
        if (i > 1) cout << " ";
        cout << perm[i];
    }
    cout << "\n";
    return 0;
}


This is the easy version of the problem. The only difference is the limit on the number of queries.

This is an interactive problem.

You are given a tree of $n$ nodes with node $1$ as its root node.

There is a hidden mole in one of the nodes. To find its position, you can pick an integer $x$ ($1 \le x \le n$) to make an inquiry to the jury. Next, the jury will return $1$ when the mole is in subtree $x$. Otherwise, the judge will return $0$. If the judge returns $0$ and the mole is not in root node $1$, the mole will move to the parent node of the node it is currently on.

Use at most $300$ operations to find the current node where the mole is located.

Input

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

Interaction

The first line of each test case contains one integer $n$ ($2 \le n \le 5000$).

The following $n-1$ lines describe the edges of the tree. Each line contains two space-separated integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$), indicating an edge between nodes $u_i$ and $v_i$.

It is guaranteed that the input data represents a tree.

The interactor in this task is not adaptive. In other words, the node where the mole is located at first is fixed in every test case and does not change during the interaction.

To ask a query, you need to pick a vertex $x$ ($1 \le x \le n$) and print the line of the following form:

* "? x"

After that, you receive:

* $0$ if the mole is not in subtree $x$;
* $1$ if the mole is in subtree $x$.

You can make at most $300$ queries of this form for each test case.

Next, if your program has found the current node where the mole is located, print the line of the following form:

* "! x"

Note that this line is not considered a query and is not taken into account when counting the number of queries asked.

After this, proceed to the next test case.

If you make more than $300$ queries during an interaction, your program must terminate immediately, and you will receive the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query or the answer for a test case, do not forget to output the end of line and flush the output. Otherwise, you will get the verdict 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.

Example

Input

```
2
2
1 2

1

6
1 2
1 3
1 4
4 5
5 6

0

0

1
```

Output

```
? 2

! 2

? 2

? 6

? 4

! 4
```

Note

In the first test case, the mole is in node $2$ initially.

For the query "? 2", the jury returns $1$ because the mole is in subtree $2$. After this query, the mole does not move.

The answer $2$ is the current node where the mole is located, so the answer is considered correct.

In the second test case, the mole is in node $6$ initially.

For the query "? 2", the jury returns $0$ because the mole is not in subtree $2$. After this query, the mole moves from node $6$ to node $5$.

For the query "? 6", the jury returns $0$ because the mole is not in subtree $6$. After this query, the mole moves from node $5$ to node $4$.

For the query "? 4", the jury returns $1$ because the mole is in subtree $4$. After this query, the mole does not move.

The answer $4$ is the current node where the mole is located, so the answer is considered correct.

Please note that the example is only for understanding the statement, and the queries in the example do not guarantee to determine the unique position of the mole.

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static long long global_max_queries = 0;
static const long long QUERY_LIMIT_PER_CASE = 300;

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

    }
}

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

int n;
vector<vector<int>> adj;
vector<int> tin_time, tout_time, parent;
int timer_dfs;
int mole;

void dfs(int u, int p) {
    tin_time[u] = ++timer_dfs;
    parent[u] = p;
    for (int v : adj[u]) {
        if (v != p) dfs(v, u);
    }
    tout_time[u] = timer_dfs;
}

void solve_test_case() {

    n = inf.readInt(2, 5000, "n");

    adj.assign(n + 1, vector<int>());
    vector<pair<int,int>> edges_list;

    for (int i = 0; i < n - 1; ++i) {
        int u = inf.readInt(1, n, "u");
        int v = inf.readInt(1, n, "v");
        adj[u].push_back(v);
        adj[v].push_back(u);
        edges_list.push_back({u, v});
    }

    mole = inf.readInt(1, n, "mole");

    cout << n << endl;
    for (auto& p : edges_list) {
        cout << p.first << " " << p.second << endl;
    }
    cout.flush();

    tin_time.assign(n + 1, 0);
    tout_time.assign(n + 1, 0);
    parent.assign(n + 1, 0);
    timer_dfs = 0;
    dfs(1, 0);

    int queries_this_case = 0;

    while (true) {

        if (queries_this_case > 0 && queries_this_case % 10 == 0) {
            log_metrics();
        }

        while (true) {
            int c = cin.peek();
            if (c == EOF) break;
            if (isspace(c)) {
                cin.get();
            } else {
                break;
            }
        }

        if (cin.peek() == EOF) {
            finish(_wa, "Unexpected EOF (expected query or answer)");
        }

        string token;
        cin >> token;

        if (token == "!") {

            int ans;
            if (!(cin >> ans)) {
                finish(_wa, "Failed to read answer index after '!'");
            }
            if (ans != mole) {
                finish(_wa, format("Incorrect answer. Expected %d, got %d", mole, ans));
            }

            return;
        } else {

            int x;
            if (token == "?") {
                if (!(cin >> x)) {
                     finish(_wa, "Failed to read query index after '?'");
                }
            } else {

                try {
                    size_t processed = 0;
                    x = stoi(token, &processed);
                    if (processed != token.size()) {
                        finish(_pe, "Invalid token format (expected integer or '?'/'!'): " + token);
                    }
                } catch (...) {
                    finish(_pe, "Invalid token format (expected integer or '?'/'!'): " + token);
                }
            }

            if (x < 1 || x > n) {
                finish(_pe, format("Query index out of range: %d", x));
            }

            queries_this_case++;
            if (queries_this_case > global_max_queries) {
                global_max_queries = queries_this_case;
            }

            if (queries_this_case > QUERY_LIMIT_PER_CASE) {
                finish(_wa, "Too many queries used in a single test case");
            }

            bool in_subtree = (tin_time[x] <= tin_time[mole] && tout_time[mole] <= tout_time[x]);

            cout << (in_subtree ? 1 : 0) << endl;

            if (!in_subtree) {
                if (mole != 1) {
                    mole = parent[mole];
                }
            }
        }
    }
}

int main(int argc, char** argv) {

    registerInteraction(argc, argv);

    atexit(log_metrics);
    log_metrics();

    int t = inf.readInt(1, 100, "t");
    cout << t << endl;

    for (int i = 0; i < t; ++i) {
        solve_test_case();
    }

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

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

vector<pair<int,int>> gen_tree(int n, string type) {
    vector<pair<int,int>> edges;
    if (type == "line") {
        for (int i = 2; i <= n; ++i) edges.push_back({i - 1, i});
    } else if (type == "star") {
        for (int i = 2; i <= n; ++i) edges.push_back({1, i});
    } else if (type == "random") {
        for (int i = 2; i <= n; ++i) edges.push_back({rnd.next(1, i - 1), i});
    } else if (type == "binary") {
        for (int i = 2; i <= n; ++i) edges.push_back({i / 2, i});
    } else if (type == "caterpillar") {
        int len = rnd.next(max(2, n / 2), n);

        for (int i = 2; i <= len; ++i) edges.push_back({i - 1, i});

        for (int i = len + 1; i <= n; ++i) edges.push_back({rnd.next(1, len), i});
    } else if (type == "broom") {
        int len = rnd.next(max(1, n / 3), 2 * n / 3);
        if (len < 1) len = 1;
        for (int i = 2; i <= len; ++i) edges.push_back({i - 1, i});

        for (int i = len + 1; i <= n; ++i) edges.push_back({len, i});
    }

    vector<int> p(n + 1);
    p[1] = 1;
    vector<int> others;
    for (int i = 2; i <= n; ++i) others.push_back(i);
    shuffle(others.begin(), others.end());
    for (int i = 0; i < (int)others.size(); ++i) p[others[i]] = i + 2;

    vector<pair<int,int>> final_edges;
    for (auto e : edges) {
        final_edges.push_back({p[e.first], p[e.second]});
    }
    return final_edges;
}

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);
    string mode = opt<string>("mode", "non");

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

    if (seed_val == 1) {
        int t = 4;
        println(t);

        println(2);
        println(1, 2);
        if (mode == "non") println(2);

        println(3);
        println(1, 2);
        println(1, 3);
        if (mode == "non") println(3);

        println(4);
        println(1, 2); println(2, 3); println(3, 4);
        if (mode == "non") println(4);

        println(5);
        println(1, 2); println(1, 3); println(1, 4); println(4, 5);
        if (mode == "non") println(5);

        return 0;
    }

    string forced_type = "";
    if (seed_val == 2) forced_type = "line";
    else if (seed_val == 3) forced_type = "star";
    else if (seed_val == 4) forced_type = "binary";

    int t = rnd.next(5, 15);
    int total_n = 5000;

    auto ns_ll = rnd.partition(t, (long long)total_n, 2LL);
    vector<int> ns;
    for(auto val : ns_ll) ns.push_back((int)val);

    println(t);
    for (int n : ns) {
        string type = forced_type;
        if (type.empty()) {
            int r = rnd.next(1, 100);
            if (r <= 30) type = "random";
            else if (r <= 50) type = "line";
            else if (r <= 60) type = "star";
            else if (r <= 75) type = "binary";
            else if (r <= 90) type = "caterpillar";
            else type = "broom";
        }

        vector<pair<int,int>> edges = gen_tree(n, type);
        println(n);
        for (auto e : edges) println(e.first, e.second);

        if (mode == "non") {

            vector<vector<int>> adj(n + 1);
            for (auto e : edges) {
                adj[e.first].push_back(e.second);
                adj[e.second].push_back(e.first);
            }
            vector<int> depth(n + 1, 0);
            vector<int> q;
            q.reserve(n);
            q.push_back(1);
            vector<bool> vis(n + 1, false);
            vis[1] = true;

            int head = 0;
            while(head < (int)q.size()){
                int u = q[head++];
                for(int v : adj[u]){
                    if(!vis[v]){
                        vis[v] = true;
                        depth[v] = depth[u] + 1;
                        q.push_back(v);
                    }
                }
            }

            int max_d = 0;
            for(int i=1; i<=n; ++i) max_d = max(max_d, depth[i]);

            vector<int> candidates;
            for(int i=1; i<=n; ++i) {
                if(depth[i] >= max(1, max_d / 2)) candidates.push_back(i);
            }
            if(candidates.empty()) candidates.push_back(n);

            int mole = candidates[rnd.next(0, (int)candidates.size() - 1)];
            println(mole);
        }
    }

    return 0;
}


This is an interactive problem.

There are $n^2$ pepes labeled $1, 2, \ldots, n^2$ with pairwise distinct speeds. You would like to set up some races to find out the relative speed of these pepes.

In one race, you can choose exactly $n$ distinct pepes and make them race against each other. After each race, you will only know the fastest pepe of these $n$ pepes.

Can you order the $n^2-n+1$ fastest pepes in at most $2n^2 - 2n + 1$ races? Note that the slowest $n - 1$ pepes are indistinguishable from each other.

Note that the interactor is adaptive. That is, the relative speeds of the pepes are not fixed in the beginning and may depend on your queries. But it is guaranteed that at any moment there is at least one initial configuration of pepes such that all the answers to the queries are consistent.

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 20$) — the number of pepes in one race.

After reading the integer $n$ for each test case, you should begin the interaction.

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

Interaction

To set up a race, print a line with the following format:

* $\mathtt{?}\,x_1\,x_2 \ldots x_n$ ($1 \le x_i \le n^2$, $x_i$ are pairwise distinct) — the labels of the pepes in the race.

After each race, you should read a line containing a single integer $p$ ($1\le p\le n^2$) — the label of the fastest pepe in the race.

When you know the $n^2-n+1$ fastest pepes, print one line in the following format:

* $\mathtt{!}\,p_1\,p_2 \ldots p_{n^2 - n + 1}$ ($1 \le p_i \le n^2$, $p_i$ are pairwise distinct)

where $p$ is the sequence of these pepe's labels in descending order of speed.

After that, move on to the next test case, or terminate the program if no more test cases are remaining.

If your program performs more than $2n^2 - 2n + 1$ races for one test case or makes an invalid race, you may receive a Wrong Answer verdict.

After printing a query 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.

Example

Input

```
1
2

2

4

4

3

2
```

Output

```
? 1 2

? 3 4

? 2 4

? 2 3

? 2 1

! 4 3 2
```

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static long long reported_queries = 0;
static long long reported_query_limit = 1;

void update_metrics(long long curr_q, long long curr_lim) {
    if (curr_q * reported_query_limit > reported_queries * curr_lim) {
        reported_queries = curr_q;
        reported_query_limit = curr_lim;
    }
}

void force_metrics(long long curr_q, long long curr_lim) {
    reported_queries = curr_q;
    reported_query_limit = curr_lim;
}

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

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

int main(int argc, char** argv) {

    registerInteraction(argc, argv);
    atexit(log_metrics);

    int t = inf.readInt();
    cout << t << endl;

    for (int tc = 1; tc <= t; ++tc) {

        int n = inf.readInt();
        cout << n << endl;

        vector<int> speeds(n * n + 1);
        for (int i = 1; i <= n * n; ++i) {
            speeds[i] = inf.readInt();
        }

        vector<pair<int, int>> sorted_pepes;
        for (int i = 1; i <= n * n; ++i) {
            sorted_pepes.push_back({speeds[i], i});
        }
        sort(sorted_pepes.rbegin(), sorted_pepes.rend());

        int ans_len = n * n - n + 1;
        vector<int> correct_ans;
        for (int i = 0; i < ans_len; ++i) {
            correct_ans.push_back(sorted_pepes[i].second);
        }

        long long limit = 2LL * n * n - 2LL * n + 1;
        long long queries = 0;
        long long hard_cap = max(200000LL, 10 * limit);

        update_metrics(queries, limit);

        if (tc == 1) log_metrics();

        bool case_active = true;
        while (case_active) {

            if (queries > hard_cap) {
                force_metrics(queries, limit);
                finish(_pe, "Hard query limit exceeded (potential infinite loop)");
            }

            string token;
            if (!(cin >> token)) {
                force_metrics(queries, limit);
                finish(_pe, "Unexpected EOF while reading query/answer");
            }

            if (token == "!") {

                vector<int> p(ans_len);
                set<int> distinct_check;

                for (int i = 0; i < ans_len; ++i) {
                    if (!(cin >> p[i])) {
                        force_metrics(queries, limit);
                        finish(_pe, "Unexpected EOF reading answer");
                    }

                    if (p[i] < 1 || p[i] > n * n) {
                        force_metrics(queries, limit);
                        finish(_pe, format("Answer label out of range: %d", p[i]));
                    }
                    if (distinct_check.count(p[i])) {
                        force_metrics(queries, limit);
                        finish(_pe, format("Duplicate label in answer: %d", p[i]));
                    }
                    distinct_check.insert(p[i]);
                }

                if (p != correct_ans) {
                    force_metrics(queries, limit);
                    finish(_wa, "Incorrect answer order or labels");
                }

                case_active = false;

            } else {

                vector<int> q_indices;

                if (token != "?") {
                    try {
                        q_indices.push_back(stoi(token));
                    } catch (...) {
                         force_metrics(queries, limit);
                         finish(_pe, format("Invalid token: %s", token.c_str()));
                    }
                }

                while (q_indices.size() < (size_t)n) {
                    int val;
                    if (!(cin >> val)) {
                         force_metrics(queries, limit);
                         finish(_pe, "Unexpected EOF reading query");
                    }
                    q_indices.push_back(val);
                }

                set<int> distinct_check;
                int max_speed = -1;
                int max_label = -1;

                for (int x : q_indices) {
                    if (x < 1 || x > n * n) {
                        force_metrics(queries, limit);
                        finish(_pe, format("Query label out of range: %d", x));
                    }
                    if (distinct_check.count(x)) {
                        force_metrics(queries, limit);
                        finish(_pe, format("Duplicate label in query: %d", x));
                    }
                    distinct_check.insert(x);

                    if (speeds[x] > max_speed) {
                        max_speed = speeds[x];
                        max_label = x;
                    }
                }

                cout << max_label << endl;

                queries++;
                update_metrics(queries, limit);

                if (queries % 50 == 0) log_metrics();
            }
        }
    }

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

#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

static long long total_queries = 0;
static long long total_query_limit = 0;

static const long long HARD_CAP = 2000000;

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

    }
}

template <typename... Args>
static void finish(TResult verdict, const char* fmt, Args... args) {
    log_metrics();
    quitf(verdict, fmt, args...);
}

void myshuffle(vector<int>& p) {
    int sz = p.size();
    for (int i = 1; i < sz; ++i) {
        int j = rnd.next(0, i);
        swap(p[i], p[j]);
    }
}

void solve_test_case() {

    int n = inf.readInt();

    cout << n << endl;

    long long limit_for_case = 2LL * n * n - 2LL * n + 1;
    total_query_limit += limit_for_case;

    log_metrics();

    vector<int> p(n * n);

    int first_val = inf.readInt();

    if (first_val == 0) {

        iota(p.begin(), p.end(), 1);
        myshuffle(p);
    } else {

        p[0] = first_val;
        for (int i = 1; i < n * n; ++i) {
            p[i] = inf.readInt();
        }
    }

    vector<int> speed_of(n * n + 1);
    for (int i = 0; i < n * n; ++i) {
        speed_of[p[i]] = (n * n) - i;
    }

    while (true) {

        char type;
        if (!(cin >> type)) {
            finish(_pe, "Unexpected EOF: expected query type ('?' or '!')");
        }

        if (type == '?') {

            total_queries++;
            if (total_queries > HARD_CAP) {
                finish(_pe, "Hard query limit exceeded (infinite loop protection)");
            }

            vector<int> race(n);
            vector<int> check_distinct;
            int winner = -1;
            int max_speed = -1;

            for (int i = 0; i < n; ++i) {
                if (!(cin >> race[i])) {
                    finish(_pe, "Unexpected EOF reading race participants");
                }

                if (race[i] < 1 || race[i] > n * n) {
                    finish(_pe, "Invalid pepe label: %d", race[i]);
                }
                check_distinct.push_back(race[i]);

                int s = speed_of[race[i]];
                if (s > max_speed) {
                    max_speed = s;
                    winner = race[i];
                }
            }

            sort(check_distinct.begin(), check_distinct.end());
            if (unique(check_distinct.begin(), check_distinct.end()) != check_distinct.end()) {
                finish(_pe, "Race participants must be distinct");
            }

            cout << winner << endl;

            if (total_queries % 100 == 0) log_metrics();

        } else if (type == '!') {

            int k = n * n - n + 1;
            vector<int> ans(k);
            for (int i = 0; i < k; ++i) {
                if (!(cin >> ans[i])) {
                    finish(_pe, "Unexpected EOF reading answer");
                }
            }

            vector<int> correct_ans;
            for (int i = 0; i < k; ++i) {
                correct_ans.push_back(p[i]);
            }

            if (ans != correct_ans) {
                finish(_wa, "Incorrect order of fastest pepes");
            }

            break;

        } else {
            finish(_pe, "Invalid query type: '%c'", type);
        }
    }
}

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

    registerInteraction(argc, argv);
    atexit(log_metrics);

    int t = inf.readInt();

    cout << t << endl;

    for (int i = 0; i < t; ++i) {
        solve_test_case();
    }

    finish(_ok, "All test cases passed");
}

#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_opt = opt<int>("n", -1);
    int t_opt = opt<int>("t", -1);

    vector<int> ns;
    long long limit_n3 = 300000;
    long long current_n3 = 0;

    auto can_add = [&](int n) {
        return current_n3 + (long long)n * n * n <= limit_n3;
    };

    auto add_n = [&](int n) {
        if (can_add(n)) {
            ns.push_back(n);
            current_n3 += (long long)n * n * n;
            return true;
        }
        return false;
    };

    if (n_opt != -1) {

        int count = (t_opt == -1) ? 1 : t_opt;
        for (int i = 0; i < count; ++i) {
            if (!add_n(n_opt)) break;
        }
    } else {

        long long seed_val = 0;
        try {

            string s(argv[1]);
            if (s.find_first_not_of("0123456789") == string::npos) {
                seed_val = stoll(s);
            }
        } catch (...) {}

        if (t_opt != -1) {

            for (int i = 0; i < t_opt; ++i) {
                if (!add_n(rnd.next(2, 20))) break;
            }
        } else if (seed_val == 1) {

            while (add_n(20));
        } else if (seed_val == 2) {

            for (int i = 0; i < 100; ++i) add_n(2);
        } else if (seed_val == 3) {

            for (int i = 2; i <= 20; ++i) add_n(i);
        } else {

            int t_rnd = rnd.next(10, 50);
            for (int i = 0; i < t_rnd; ++i) {
                if (!add_n(rnd.next(2, 20))) break;
            }
        }
    }

    if (ns.empty()) {
        ns.push_back(n_opt != -1 ? n_opt : 2);
    }

    println(ns.size());
    for (int n : ns) {
        println(n);
        if (mode == "non") {

            vector<int> p(n * n);
            iota(p.begin(), p.end(), 1);
            shuffle(p.begin(), p.end());
            println(p);
        } else {

            println(0);
        }
    }

    return 0;
}


This is an interactive problem.

There is an unknown integer $x$ ($1\le x\le n$). You want to find $x$.

At first, you have a set of integers $\{1, 2, \ldots, n\}$. You can perform the following operations no more than $10000$ times:

* A $a$: find how many numbers are multiples of $a$ in the current set.
* B $a$: find how many numbers are multiples of $a$ in this set, and then delete all multiples of $a$, but $x$ will never be deleted (even if it is a multiple of $a$). In this operation, $a$ must be greater than $1$.
* C $a$: it means that you know that $x=a$. This operation can be only performed once.

Remember that in the operation of type B $a>1$ must hold.

Write a program, that will find the value of $x$.

Input

The first line contains one integer $n$ ($1\le n\le 10^5$). The remaining parts of the input will be given throughout the interaction process.

Interaction

In each round, your program needs to print a line containing one uppercase letter A, B or C and an integer $a$ ($1\le a\le n$ for operations A and C, $2\le a\le n$ for operation B). This line desribes operation you make.

If your operation has type C your program should terminate immediately.

Else your program should read one line containing a single integer, which is the answer to your operation.

After outputting each line, don't forget to flush the output. To do it use:

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

It is guaranteed, that the number $x$ is fixed and won't change during the interaction process.

Example

Input

```
10

2

4

0
```

Output

```
B 4

A 2

A 8

C 4
```

Note

Note that to make the sample more clear, we added extra empty lines. You shouldn't print any extra empty lines during the interaction process.

In the first test $n=10$ and $x=4$.

Initially the set is: $\{1,2,3,4,5,6,7,8,9,10\}$.

In the first operation, you ask how many numbers are multiples of $4$ and delete them. The answer is $2$ because there are two numbers divisible by $4$: $\{4,8\}$. $8$ will be deleted but $4$ won't, because the number $x$ will never be deleted. Now the set is $\{1,2,3,4,5,6,7,9,10\}$.

In the second operation, you ask how many numbers are multiples of $2$. The answer is $4$ because there are four numbers divisible by $2$: $\{2,4,6,10\}$.

In the third operation, you ask how many numbers are multiples of $8$. The answer is $0$ because there isn't any number divisible by $8$ in the current set.

In the fourth operation, you know that $x=4$, which is the right answer.

#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 vector<int> build_spf(int n) {
    vector<int> spf(n + 1, 0), primes;
    if (n >= 1) spf[1] = 1;
    for (int i = 2; i <= n; i++) {
        if (spf[i] == 0) {
            spf[i] = i;
            primes.push_back(i);
        }
        for (int p : primes) {
            long long v = 1LL * i * p;
            if (v > n) break;
            spf[(int)v] = p;
            if (p == spf[i]) break;
        }
    }
    return spf;
}

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

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n = inf.readInt(1, 100000, "n");

    query_limit = 10000;
    log_metrics();

    if (inf.seekEof()) {
        finish(_pe, "Hidden input schema mismatch: expected 'n x' for non-adaptive interactor.");
    }
    int x = inf.readInt(1, n, "x");

    cout << n << endl;

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

    int S = min(450, n);
    vector<long long> smallCount(S + 1, 0);
    for (int d = 1; d <= S; d++) smallCount[d] = n / d;

    vector<char> alive(n + 1, 1);
    alive[0] = 0;

    vector<char> cleaned(n + 1, 0);

    vector<int> spf = build_spf(n);

    static vector<int> divs, tmp;
    auto decSmallCountsForDeleted = [&](int y) {

        int t = y;
        vector<pair<int,int>> fac;
        while (t > 1) {
            int p = spf[t];
            int c = 0;
            while (t % p == 0) {
                t /= p;
                c++;
            }
            fac.push_back({p, c});
        }
        divs.clear();
        divs.push_back(1);
        for (auto [p, c] : fac) {
            tmp.clear();
            size_t need = divs.size() * (size_t)(c + 1);
            if (tmp.capacity() < need) tmp.reserve(need);
            for (int d : divs) {
                long long mul = 1;
                for (int k = 0; k <= c; k++) {
                    tmp.push_back((int)(d * mul));
                    mul *= p;
                }
            }
            divs.swap(tmp);
        }
        for (int d : divs) {
            if (d <= S) smallCount[d]--;
        }
    };

    auto getCount = [&](int a) -> long long {
        if (a <= S) return smallCount[a];
        long long ans = 0;
        for (int m = a; m <= n; m += a) ans += (alive[m] != 0);
        return ans;
    };

    while (true) {
        string cmd;
        long long a_ll;

        if (!(cin >> cmd)) {
            finish(_pe, "Unexpected EOF: contestant terminated without 'C a'.");
        }
        if (!(cin >> a_ll)) {
            finish(_pe, "Protocol error: expected integer after command.");
        }

        if (cmd != "A" && cmd != "B" && cmd != "C") {
            finish(_pe, "Protocol error: command must be one of A, B, C.");
        }

        if (a_ll < LLONG_MIN / 2 || a_ll > LLONG_MAX / 2) {
            finish(_pe, "Protocol error: integer out of range.");
        }
        if (a_ll < 1 || a_ll > n) {
            finish(_pe, "Protocol error: parameter a out of allowed range.");
        }
        int a = (int)a_ll;

        queries++;
        if (queries > hard_cap) {
            finish(_pe, "Hard cap exceeded (too many operations).");
        }

        if (cmd == "C") {
            if (a == x) finish(_ok, "Correct.");
            finish(_wa, "Incorrect guess.");
        } else if (cmd == "A") {
            long long ans = getCount(a);
            cout << ans << endl;
        } else {
            if (a <= 1) {
                finish(_pe, "Protocol error: operation B requires a > 1.");
            }
            long long ans = getCount(a);
            cout << ans << endl;

            if (!cleaned[a]) {
                cleaned[a] = 1;
                for (int m = a; m <= n; m += a) {
                    if (m == x) continue;
                    if (!alive[m]) continue;
                    alive[m] = 0;
                    decSmallCountsForDeleted(m);
                }
            }
        }
    }
}

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static long long queries = 0;
static long long query_limit = 10000;

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 vector<int> build_spf(int n) {
    vector<int> spf(n + 1, 0), primes;
    if (n >= 1) spf[1] = 1;
    for (int i = 2; i <= n; i++) {
        if (spf[i] == 0) {
            spf[i] = i;
            primes.push_back(i);
        }
        for (int p : primes) {
            long long v = 1LL * i * p;
            if (v > n) break;
            spf[(int)v] = p;
            if (p == spf[i]) break;
        }
    }
    return spf;
}

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

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n = inf.readInt(1, 100000, "n");
    int x = 0;
    if (!inf.seekEof()) {
        x = inf.readInt(1, n, "x");
    } else {
        x = n;
    }

    log_metrics();

    cout << n << endl;

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

    int S = min(450, n);
    vector<long long> smallCount(S + 1, 0);
    for (int d = 1; d <= S; d++) smallCount[d] = n / d;

    vector<char> alive(n + 1, 1);
    alive[0] = 0;

    vector<char> cleaned(n + 1, 0);

    vector<int> spf = build_spf(n);

    static vector<int> divs, tmp;
    auto decSmallCountsForDeleted = [&](int y) {

        int t = y;
        vector<pair<int, int>> fac;
        while (t > 1) {
            int p = spf[t];
            int c = 0;
            while (t % p == 0) {
                t /= p;
                c++;
            }
            fac.push_back({p, c});
        }
        divs.clear();
        divs.push_back(1);
        for (auto [p, c] : fac) {
            tmp.clear();
            tmp.reserve(divs.size() * (size_t)(c + 1));
            for (int d : divs) {
                long long mul = 1;
                for (int k = 0; k <= c; k++) {
                    tmp.push_back((int)(d * mul));
                    mul *= p;
                }
            }
            divs.swap(tmp);
        }
        for (int d : divs) {
            if (d <= S) smallCount[d]--;
        }
    };

    auto getCount = [&](int a) -> long long {
        if (a <= S) return smallCount[a];
        long long ans = 0;
        for (int m = a; m <= n; m += a) ans += (alive[m] != 0);
        return ans;
    };

    while (true) {
        string cmd;
        long long a_ll;

        if (!(cin >> cmd)) {
            finish(_pe, "Unexpected EOF: contestant terminated without 'C a'.");
        }
        if (!(cin >> a_ll)) {
            finish(_pe, "Protocol error: expected integer after command.");
        }

        if (cmd != "A" && cmd != "B" && cmd != "C") {
            finish(_pe, "Protocol error: command must be one of A, B, C.");
        }
        if (a_ll < 1 || a_ll > n) {
            finish(_pe, "Protocol error: parameter a out of allowed range.");
        }
        int a = (int)a_ll;

        queries++;
        if (queries > hard_cap) {
            finish(_pe, "Hard cap exceeded (too many operations).");
        }

        if (cmd == "C") {
            if (a == x) finish(_ok, "Correct.");
            finish(_wa, "Incorrect guess.");
        } else if (cmd == "A") {
            long long ans = getCount(a);
            cout << ans << endl;
        } else {
            if (a <= 1) {
                finish(_pe, "Protocol error: operation B requires a > 1.");
            }
            long long ans = getCount(a);
            cout << ans << endl;

            if (!cleaned[a]) {
                cleaned[a] = 1;
                for (int m = a; m <= n; m += a) {
                    if (m == x) continue;
                    if (!alive[m]) continue;
                    alive[m] = 0;
                    decSmallCountsForDeleted(m);
                }
            }
        }
    }
}

#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 = opt<int>("n", 100000);

    int seed = 1;
    if (argc >= 2) {

        try { seed = stoi(argv[1]); } catch (...) { seed = 1; }
    }

    n = max(1, min(n, 100000));

    auto isPrime = [&](int x)->bool{
        if (x < 2) return false;
        for (int i = 2; 1LL*i*i <= x; i++) if (x % i == 0) return false;
        return true;
    };

    auto prevPrimeLE = [&](int x)->int{
        for (int v = x; v >= 2; --v) if (isPrime(v)) return v;
        return 2;
    };

    auto nextPrimeGE = [&](int x)->int{
        for (int v = x; v <= n; ++v) if (isPrime(v)) return v;
        return prevPrimeLE(n);
    };

    int x = 1;

    if (seed == 1) {

        x = 1;
    } else if (seed == 2) {

        x = n;
    } else if (seed == 3) {

        x = prevPrimeLE(n);
    } else {

        int pick = rnd.next(1, 100);

        if (pick <= 45) {

            vector<int> primes = {2,3,5,7,11,13,17,19,23,29,31};
            long long val = 1;
            for (int p : primes) {
                int emax = 0;
                long long t = val;
                while (t * p <= n) { t *= p; emax++; }
                if (emax == 0) continue;

                int e = rnd.next(0, emax);
                for (int i = 0; i < e; i++) {
                    if (val * p > n) break;
                    val *= p;
                }
                if (val == n) break;
            }
            if (val < 2) val = 2;
            x = (int)val;

            if (x <= 3 || isPrime(x)) {
                int p = 2;
                while (1LL*x*p <= n) x *= p;
                if (x < 2) x = 4;
            }
        } else if (pick <= 75) {

            int p;
            if (rnd.next(0, 1) == 0) p = 2;
            else if (rnd.next(0, 1) == 0) p = 3;
            else p = nextPrimeGE(max(2, n/1000));
            long long val = p;
            while (val * p <= n) val *= p;
            x = (int)val;
            if (x < 2) x = min(n, 4);
        } else {

            int s = (int)sqrt((double)n);
            int p = prevPrimeLE(max(2, s + rnd.next(-2000, 2000)));
            int q = prevPrimeLE(max(2, (n / p)));

            if (p < 2) p = 2;
            if (q < 2) q = 2;
            long long val = 1LL*p*q;
            if (val > n) {
                q = prevPrimeLE(max(2, (int)(n / p)));
                val = 1LL*p*q;
            }
            if (val < 2) val = 2;
            x = (int)val;

            if (isPrime(x) && x >= 4) x = (x/2)*2;
            if (x < 2) x = 2;
            if (x > n) x = n;
        }

        if (x < 1) x = 1;
        if (x > n) x = n;
    }

    if (mode == "adp") {

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

        cout << n << " " << x << "\n";
    } else {

        cout << n << " " << x << "\n";
    }

    return 0;
}


This is an interactive problem. You have to use a flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output) and in Python — sys.stdout.flush().

Mr. Chanek wants to buy a flamingo to accompany his chickens on his farm. Before going to the pet shop, Mr. Chanek stops at an animal festival to have fun. It turns out there is a carnival game with a flamingo as the prize.

There are $N$ mysterious cages, which are numbered from $1$ to $N$. Cage $i$ has $A_i$ $(0 \le A_i \le 10^3)$ flamingoes inside $(1 \le i \le N)$. However, the game master keeps the number of flamingoes inside a secret. To win the flamingo, Mr. Chanek must guess the number of flamingoes in each cage.

Coincidentally, Mr. Chanek has $N$ coins. Each coin can be used to ask once, what is the total number of flamingoes inside cages numbered $L$ to $R$ inclusive? With $L < R$.

Input

Use standard input to read the responses of your questions.

Initially, the judge will give an integer $N$ $(3 \le N \le 10^3)$, the number of cages, and the number of coins Mr. Chanek has.

For each of your questions, the jury will give an integer that denotes the number of flamingoes from cage $L$ to $R$ inclusive.

If your program does not guess the flamingoes or ask other questions, you will get "Wrong Answer". Of course, if your program asks more questions than the allowed number, your program will get "Wrong Answer".

Output

To ask questions, your program must use standard output.

Then, you can ask at most $N$ questions. Questions are asked in the format "? L R", ($1 \le L < R \le N$).

To guess the flamingoes, print a line that starts with "!" followed by $N$ integers where the $i$-th integer denotes the number of flamingo in cage $i$. After answering, your program must terminate or will receive the "idle limit exceeded" verdict. You can only guess the flamingoes once.

Examples

Input

```
6

5

15

10

```

Output

```

? 1 2

? 5 6

? 3 4

! 1 4 4 6 7 8
```

Note

In the sample input, the correct flamingoes amount is $[1, 4, 4, 6, 7, 8]$.

#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 readLL(long long &x) {
    if (!(cin >> x)) return false;
    return true;
}

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

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

    int N = 0;
    vector<int> A;
    vector<long long> pref;

    try {
        N = inf.readInt(3, 1000, "N");
        A.assign(N + 1, 0);
        for (int i = 1; i <= N; i++) A[i] = inf.readInt(0, 1000, ("A[" + to_string(i) + "]").c_str());

        pref.assign(N + 1, 0);
        for (int i = 1; i <= N; i++) pref[i] = pref[i - 1] + A[i];

        query_limit = N;
        log_metrics();

        cout << N << endl;
    } catch (const std::exception& e) {

        finish(_pe, string("Interactor failed to read hidden input: ") + e.what());
    } catch (...) {
        finish(_pe, "Interactor failed to read hidden input (unknown error)");
    }

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

    bool guessed = false;
    while (true) {
        string cmd;
        if (!readToken(cmd)) {
            if (!guessed) finish(_wa, "Contestant terminated without providing final answer");
            finish(_pe, "Unexpected EOF after final answer");
        }

        if (cmd == "?") {
            long long L, R;
            if (!readLL(L) || !readLL(R)) finish(_pe, "Malformed query: expected '? L R'");

            if (!(1 <= L && L < R && R <= N)) {
                finish(_wa, "Invalid query range");
            }

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

            long long ans = pref[(int)R] - pref[(int)L - 1];
            cout << ans << endl;
        } else if (cmd == "!") {
            if (guessed) finish(_pe, "Final answer given more than once");
            guessed = true;

            vector<long long> out(N + 1, 0);
            for (int i = 1; i <= N; i++) {
                long long v;
                if (!readLL(v)) finish(_pe, "Malformed final answer: expected N integers after '!'");
                out[i] = v;
            }

            for (int i = 1; i <= N; i++) {
                if (out[i] != A[i]) {
                    finish(_wa, "Wrong final answer");
                }
            }
            finish(_ok, "OK");
        } else {
            finish(_pe, "Unknown command (expected '?' or '!')");
        }
    }
}

#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 vector<int> buildDeterministicArray(int N, int Amin, int Amax) {
    vector<int> a(N + 1, 0);
    int range = Amax - Amin + 1;
    if (range <= 0) range = 1;

    if (range == 1) {
        for (int i = 1; i <= N; i++) a[i] = Amin;
        return a;
    }

    a[1] = Amin;
    if (N >= 2) a[2] = Amax;
    if (N >= 3) a[3] = min(Amax, Amin + 1);

    for (int i = 4; i <= N; i++) {
        long long x = i;
        long long v = (x * x * 37LL + x * 12345LL + 67890LL + 97LL * N) % range;
        a[i] = Amin + (int)v;
    }

    if (N >= 4) a[N] = Amax;
    if (N >= 5) a[N - 1] = Amin;

    return a;
}

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

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N = inf.readInt();
    if (N < 3 || N > 1000) finish(_pe, "Invalid N in input file");

    vector<int> rest;
    while (!inf.seekEof()) {
        rest.push_back(inf.readInt());
        if ((int)rest.size() > 1000000) finish(_pe, "Too many tokens in input file");
    }

    vector<int> A(N + 1, 0);
    if ((int)rest.size() >= N) {
        for (int i = 1; i <= N; i++) A[i] = rest[i - 1];
        for (int i = 1; i <= N; i++) {
            if (A[i] < 0 || A[i] > 1000) finish(_pe, "Ai out of [0,1000] in input file");
        }
    } else if ((int)rest.size() == 2) {
        int Amin = rest[0], Amax = rest[1];
        if (Amin < 0 || Amax > 1000 || Amin > Amax) finish(_pe, "Invalid Amin/Amax in input file");
        A = buildDeterministicArray(N, Amin, Amax);
    } else {
        finish(_pe, "Unrecognized input schema (expected N ints or Amin Amax)");
    }

    vector<long long> pref(N + 1, 0);
    for (int i = 1; i <= N; i++) pref[i] = pref[i - 1] + A[i];

    query_limit = N;
    cout << N << "\n" << flush;

    log_metrics();

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

    for (;;) {
        string t;
        if (!(cin >> t)) {
            finish(_wa, "Contestant terminated without final answer");
        }

        if (t == "?") {
            long long L, R;
            if (!(cin >> L >> R)) finish(_pe, "Malformed query: expected '? L R'");
            if (!(1 <= L && L < R && R <= N)) finish(_pe, "Query out of range or L>=R");

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

            long long ans = pref[(int)R] - pref[(int)L - 1];
            cout << ans << "\n" << flush;
        } else if (t == "!") {
            vector<long long> g(N + 1, 0);
            for (int i = 1; i <= N; i++) {
                if (!(cin >> g[i])) finish(_pe, "Malformed final answer: expected N integers after '!'");
            }

            for (int i = 1; i <= N; i++) {
                if (g[i] != A[i]) {
                    finish(_wa, "Wrong final answer");
                }
            }
            finish(_ok, "OK");
        } else {
            finish(_pe, "Unexpected token (expected '?' or '!')");
        }
    }
}

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static vector<int> makeCorner(int which, int N) {
    vector<int> a(N + 1, 0);
    if (which == 1) {

        for (int i = 1; i <= N; i++) a[i] = 0;
    } else if (which == 2) {

        for (int i = 1; i <= N; i++) a[i] = (i & 1) ? 1000 : 0;
    } else {

        for (int i = 1; i <= N; i++) {
            int v = (i % 7) * (i % 5);
            a[i] = min(1000, v * 37);
        }
    }
    return a;
}

static vector<int> makeHard(int N) {
    vector<int> a(N + 1, 0);

    int patternType = rnd.next(1, 5);
    if (patternType == 1) {

        for (int i = 1; i <= N; i++) a[i] = 0;
        int spikes = max(3, N / 20);
        for (int k = 0; k < spikes; k++) {
            int pos = rnd.next(1, N);
            a[pos] = rnd.next(850, 1000);
        }

        int L = rnd.next(1, max(1, N - 20));
        int len = rnd.next(5, min(20, N - L + 1));
        for (int i = L; i < L + len; i++) a[i] = max(a[i], rnd.next(700, 1000));
    } else if (patternType == 2) {

        for (int i = 1; i <= N; i++) {
            int base = (i & 1) ? 1000 : 0;
            int noise = rnd.next(0, 30);
            a[i] = (base == 1000) ? max(0, base - noise) : min(1000, base + noise);
        }
    } else if (patternType == 3) {

        int i = 1;
        while (i <= N) {
            int segLen = rnd.next(1, 40);
            int v;
            if (rnd.next(0, 9) == 0) v = rnd.next(0, 1000);
            else v = rnd.next(0, 200) + (rnd.next(0, 1) ? 0 : rnd.next(600, 800));
            for (int j = 0; j < segLen && i <= N; j++, i++) a[i] = min(1000, max(0, v));
        }
    } else if (patternType == 4) {

        for (int i = 1; i <= N; i++) {
            long long x = i;
            long long v = (x * x + 1234567LL) % 1001;
            v = (v + (i % 37) * 7) % 1001;
            v = (v + rnd.next(0, 50)) % 1001;
            a[i] = (int)v;
        }
    } else {

        int peak = rnd.next(max(2, N / 3), max(2, 2 * N / 3));
        int cur = rnd.next(0, 50);
        for (int i = 1; i <= N; i++) {
            int dir = (i <= peak) ? 1 : -1;
            int step = rnd.next(0, 25);
            if (rnd.next(0, 4) == 0) step = 0;
            cur += dir * step;
            cur = max(0, min(1000, cur));

            if (rnd.next(0, 49) == 0) cur = rnd.next(0, 1000);
            a[i] = cur;
        }
    }

    return a;
}

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

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

    int N;
    long long seed = 1;
    if (argc >= 2) {

        try {
            seed = stoll(string(argv[1]));
        } catch (...) {
            seed = 1;
        }
    }

    if (seed == 1) N = 3;
    else if (seed == 2) N = 1000;
    else if (seed == 3) N = 7;
    else {

        int roll = rnd.next(0, 99);
        if (roll < 70) N = rnd.next(900, 1000);
        else if (roll < 90) N = rnd.next(200, 900);
        else N = rnd.next(3, 200);
    }

    if (mode == "non") {
        vector<int> a;
        if (seed == 1) a = makeCorner(1, N);
        else if (seed == 2) a = makeCorner(2, N);
        else if (seed == 3) a = makeCorner(3, N);
        else a = makeHard(N);

        cout << N << "\n";
        for (int i = 1; i <= N; i++) {
            if (i > 1) cout << ' ';
            cout << a[i];
        }
        cout << "\n";
        return 0;
    } else if (mode == "adp") {

        int Amin = 0, Amax = 1000;

        if (seed == 1) { Amin = 0; Amax = 0; N = 3; }
        else if (seed == 2) { Amin = 0; Amax = 1000; N = 1000; }
        else if (seed == 3) { Amin = 0; Amax = 3; N = 7; }
        else {

            int t = rnd.next(0, 99);
            if (t < 20) { Amin = 0; Amax = 1; }
            else if (t < 45) { Amin = 0; Amax = 7; }
            else if (t < 70) { Amin = 0; Amax = 50; }
            else { Amin = 0; Amax = 1000; }
        }

        cout << N << "\n" << Amin << " " << Amax << "\n";
        return 0;
    } else {

        vector<int> a = makeHard(N);
        cout << N << "\n";
        for (int i = 1; i <= N; i++) {
            if (i > 1) cout << ' ';
            cout << a[i];
        }
        cout << "\n";
        return 0;
    }
}


This is an interactive problem.

We have hidden an integer $1 \le X \le 10^{9}$. You don't have to guess this number. You have to find the number of divisors of this number, and you don't even have to find the exact number: your answer will be considered correct if its absolute error is not greater than 7 or its relative error is not greater than $0.5$. More formally, let your answer be $ans$ and the number of divisors of $X$ be $d$, then your answer will be considered correct if at least one of the two following conditions is true:

* $| ans - d | \le 7$;
* $\frac{1}{2} \le \frac{ans}{d} \le 2$.

You can make at most $22$ queries. One query consists of one integer $1 \le Q \le 10^{18}$. In response, you will get $gcd(X, Q)$ — the greatest common divisor of $X$ and $Q$.

The number $X$ is fixed before all queries. In other words, interactor is not adaptive.

Let's call the process of guessing the number of divisors of number $X$ a game. In one test you will have to play $T$ independent games, that is, guess the number of divisors $T$ times for $T$ independent values of $X$.

Input

The first line of input contains one integer $T$ ($1 \le T \le 100$) — the number of games.

Interaction

To make a query print a line "? Q" ($1 \le Q \le 10^{18}$). After that read one integer $gcd(X, Q)$. You can make no more than $22$ such queries during one game.

If you are confident that you have figured out the number of divisors of $X$ with enough precision, you can print your answer in "! ans" format. $ans$ have to be an integer. If this was the last game, you have to terminate the program, otherwise you have to start the next game immediately. Note that the interactor doesn't print anything in response to you printing answer.

After printing a query do not forget to output end of line and flush the output. 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 documentation for other languages.

Example

Input

```
2

1

1

1

1024

1048576

4194304
```

Output

```
? 982306799268821872

? 230856864650023977

? 134690134760714371

! 5
? 1024

? 1048576

? 1073741824

! 42
```

Note

Why the limitation for number of queries is 22 exactly? Maybe the problem author is a Taylor Swift fan.

Let's look at the example.

In the first game $X = 998\,244\,353$ is hidden. Would be hard to guess this, right? This number is prime, so the number of its divisors is 2. The solution has made several random queries, and all the responses turned out to be 1 (strange things, not even one of three random numbers is divisible by $998\,244\,353$). It's fare to assume that the hidden number doesn't have many divisors, so the solution has answered 5. Why not. This answer will be considered correct since $| 5 - 2 | = 3 \le 7$.

In the second game $X = 4\,194\,304 = 2^{22}$ is hidden, it has 23 divisors. The solution has made queries $1024 = 2^{10}$, $1\,048\,576 =2^{20}$, $1\,073\,741\,824 = 2^{30}$ and got responses $1024 = 2^{10}$, $1\,048\,576 =2^{20}$, $4\,194\,304 = 2^{22}$, respectively. Then the solution got completely confused and answered the answer to The Ultimate Question of Life, the Universe, and Everything. This answer will be considered correct since $\frac{1}{2} \le \frac{42}{23} \le 2$.

#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 parse_ll_strict(const string& s, long long& out) {
    if (s.empty()) return false;
    size_t pos = 0;
    try {
        long long v = stoll(s, &pos, 10);
        if (pos != s.size()) return false;
        out = v;
        return true;
    } catch (...) {
        return false;
    }
}

static long long count_divisors_1e9(long long x) {
    if (x <= 0) return 0;
    long long n = x;
    long long res = 1;
    for (long long p = 2; p * p <= n; p += (p == 2 ? 1 : 2)) {
        if (n % p == 0) {
            int e = 0;
            while (n % p == 0) {
                n /= p;
                ++e;
            }
            res *= (e + 1LL);
        }
    }
    if (n > 1) res *= 2LL;
    return res;
}

static bool read_token(string& tok) {
    return (cin >> tok) ? true : false;
}

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

    int T = 0;
    try {
        T = inf.readInt(1, 100, "T");
    } catch (...) {
        query_limit = 0;
        log_metrics();
        quitf(_fail, "Invalid hidden input (failed to read T)");
    }

    vector<long long> xs;
    xs.reserve(T);
    for (int i = 0; i < T; i++) {
        long long x = 0;
        try {
            x = inf.readLong(1LL, 1000000000LL, ("X[" + to_string(i) + "]").c_str());
        } catch (...) {
            query_limit = 0;
            log_metrics();
            quitf(_fail, "Invalid hidden input (failed to read X)");
        }
        xs.push_back(x);
    }

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

    log_metrics();

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

    for (int game = 0; game < T; game++) {
        const long long X = xs[game];
        const long long d = count_divisors_1e9(X);

        bool answered = false;
        while (!answered) {
            if (queries > hard_cap) finish(_pe, "Hard cap exceeded (too many queries)");

            string cmd;
            if (!read_token(cmd)) finish(_pe, "Unexpected EOF from contestant");

            if (cmd == "?") {
                string qtok;
                if (!read_token(qtok)) finish(_pe, "Query missing Q");
                long long q = 0;
                if (!parse_ll_strict(qtok, q)) finish(_pe, "Invalid Q token");
                if (q < 1LL || q > 1000000000000000000LL) finish(_pe, "Q out of range");

                long long g = std::gcd(X, q);
                ++queries;
                cout << g << "\n";
                cout.flush();
            } else if (cmd == "!") {
                string atok;
                if (!read_token(atok)) finish(_pe, "Answer missing ans");
                long long ans = 0;
                if (!parse_ll_strict(atok, ans)) finish(_pe, "Invalid ans token");
                if (ans < 0) finish(_pe, "Negative ans");

                bool ok_abs = llabs(ans - d) <= 7LL;
                bool ok_rel = false;
                if (d > 0) {
                    long double ratio = (long double)ans / (long double)d;
                    ok_rel = (ratio >= 0.5L - 1e-18L && ratio <= 2.0L + 1e-18L);
                }

                if (!(ok_abs || ok_rel)) {
                    finish(_wa, "Wrong answer");
                }

                answered = true;

            } else {
                finish(_pe, "Unknown command (expected '?' or '!')");
            }
        }
    }

    string extra;
    if (cin >> extra) finish(_pe, "Extra output after finishing all games");

    finish(_ok, "OK");
}

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static long long mul_mod(long long a, long long b, long long mod) {
    return (long long)((__int128)a * (__int128)b % mod);
}

static long long pow_mod(long long a, long long d, long long mod) {
    long long r = 1 % mod;
    long long x = a % mod;
    while (d > 0) {
        if (d & 1) r = mul_mod(r, x, mod);
        x = mul_mod(x, x, mod);
        d >>= 1;
    }
    return r;
}

static bool isPrime64(long long n) {
    if (n < 2) return false;
    for (long long p : {2LL, 3LL, 5LL, 7LL, 11LL, 13LL, 17LL, 19LL, 23LL, 29LL, 31LL, 37LL}) {
        if (n == p) return true;
        if (n % p == 0) return n == p;
    }
    long long d = n - 1, s = 0;
    while ((d & 1) == 0) d >>= 1, ++s;

    auto witness = [&](long long a) -> bool {
        if (a % n == 0) return false;
        long long x = pow_mod(a, d, n);
        if (x == 1 || x == n - 1) return false;
        for (long long i = 1; i < s; i++) {
            x = mul_mod(x, x, n);
            if (x == n - 1) return false;
        }
        return true;
    };

    for (long long a : {2LL, 325LL, 9375LL, 28178LL, 450775LL, 9780504LL, 1795265022LL}) {
        if (witness(a)) return false;
    }
    return true;
}

static long long nextPrimeAtLeast(long long x, long long hi) {
    if (x <= 2) return 2;
    if ((x & 1) == 0) x++;
    for (long long v = x; v <= hi; v += 2) {
        if (isPrime64(v)) return v;
    }
    return -1;
}

static long long prevPrimeAtMost(long long x, long long lo) {
    if (x < 2) return -1;
    if (x == 2) return 2;
    if ((x & 1) == 0) x--;
    for (long long v = x; v >= lo; v -= 2) {
        if (isPrime64(v)) return v;
    }
    return -1;
}

static long long primeNear(long long lo, long long hi) {
    lo = max(lo, 2LL);
    hi = max(hi, 2LL);
    if (lo > hi) swap(lo, hi);
    long long start = rnd.next(lo, hi);
    long long up = nextPrimeAtLeast(start, hi);
    if (up != -1) return up;
    long long down = prevPrimeAtMost(start, lo);
    if (down != -1) return down;

    for (long long v = lo; v <= hi; v++) if (isPrime64(v)) return v;
    return 2;
}

static long long pow_limit(long long base, int exp, long long lim) {
    __int128 v = 1;
    for (int i = 0; i < exp; i++) {
        v *= base;
        if (v > lim) return lim + 1;
    }
    return (long long)v;
}

struct Cand {
    long long val;
    long long divs;
};

static void shrinkTop(vector<Cand>& v, int keep) {
    sort(v.begin(), v.end(), [](const Cand& a, const Cand& b) {
        if (a.divs != b.divs) return a.divs > b.divs;
        return a.val > b.val;
    });
    if ((int)v.size() > keep) v.resize(keep);
}

static vector<long long> highlyCompositeList(long long maxX, int keep) {
    vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
    vector<Cand> best;
    best.reserve(keep * 4);

    function<void(int,int,long long,long long)> dfs = [&](int idx, int lastExp, long long curVal, long long curDivs) {
        best.push_back({curVal, curDivs});
        if ((int)best.size() > keep * 20) shrinkTop(best, keep * 10);

        if (idx >= (int)primes.size()) return;
        int p = primes[idx];
        __int128 v = curVal;
        for (int e = 1; e <= lastExp; e++) {
            v *= p;
            if (v > maxX) break;
            long long nv = (long long)v;
            long long nd = curDivs * (e + 1LL);
            dfs(idx + 1, e, nv, nd);
        }
    };

    dfs(0, 60, 1LL, 1LL);
    shrinkTop(best, keep);

    vector<long long> vals;
    vals.reserve(best.size());
    for (auto &c : best) vals.push_back(c.val);

    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    return vals;
}

static long long makePrimePower(long long maxX) {
    static vector<int> smallPr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
    int p = smallPr[rnd.next(0, (int)smallPr.size() - 1)];
    int maxExp = 1;
    while (pow_limit(p, maxExp + 1, maxX) <= maxX) maxExp++;
    int e = rnd.next(1, maxExp);
    long long v = pow_limit(p, e, maxX);
    if (v < 1 || v > maxX) v = p;
    return v;
}

static long long makeSemiprime(long long maxX) {
    long long s = (long long)sqrt((long double)maxX);
    long long off1 = rnd.next(0LL, 4000LL);
    long long off2 = rnd.next(0LL, 4000LL);
    long long p = prevPrimeAtMost(s - off1, 2);
    long long q = nextPrimeAtLeast(s + off2, 2LL * s + 100000);
    if (p == -1) p = 2;
    if (q == -1) q = 2;
    __int128 prod = (__int128)p * (__int128)q;
    if (prod <= maxX) return (long long)prod;

    long long q2 = prevPrimeAtMost((long long)(maxX / p), 2);
    if (q2 != -1) return p * q2;

    long long sp = 2;
    long long lp = prevPrimeAtMost(maxX / sp, 2);
    if (lp == -1) lp = 2;
    return sp * lp;
}

static long long makeSmooth(long long maxX) {
    vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};
    long long x = 1;
    int maxUsed = rnd.next(3, 7);
    int lastExp = 60;
    for (int i = 0; i < maxUsed; i++) {
        int p = primes[i];
        int expLimit = 0;
        while (expLimit + 1 <= lastExp && pow_limit(p, expLimit + 1, maxX / x) <= maxX / x) expLimit++;
        expLimit = min(expLimit, lastExp);
        int e = rnd.next(0, expLimit);
        if (e == 0) continue;
        long long pe = pow_limit(p, e, maxX);
        if (pe > maxX / x) break;
        x *= pe;
        lastExp = e;
    }
    if (x < 1) x = 1;
    if (x > maxX) x = maxX;
    return x;
}

static long long makeNearMax(long long maxX) {
    long long delta = rnd.next(0LL, 100000LL);
    long long x = maxX - delta;
    if (x < 1) x = 1;
    return x;
}

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

    string mode = opt<string>("mode", "non");
    long long maxX = opt<long long>("maxx", 1000000000LL);

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

    if (mode == "adp") {
        int T = opt<int>("t", 100);
        long long L = opt<long long>("l", 1LL);
        long long R = opt<long long>("r", maxX);
        if (L < 1) L = 1;
        if (R > maxX) R = maxX;
        if (L > R) swap(L, R);
        cout << T << "\n" << L << " " << R << "\n";
        return 0;
    }

    vector<long long> xs;

    auto pushX = [&](long long x) {
        if (x < 1) x = 1;
        if (x > maxX) x = maxX;
        xs.push_back(x);
    };

    if (seed == 1) {

        pushX(1);
        pushX(2);
        pushX(4);
        pushX(536870912LL);
        pushX(1000000000LL);
        pushX(999999937LL);
        pushX(73513440LL);

        long long s = (long long)sqrt((long double)maxX);
        long long p = prevPrimeAtMost(s, 2);
        long long q = nextPrimeAtLeast(s + 1, 2LL * s + 100000);
        __int128 prod = (__int128)p * (__int128)q;
        if (p != -1 && q != -1 && prod <= maxX) pushX((long long)prod);
        else pushX(makeSemiprime(maxX));
    } else if (seed == 2) {

        pushX(1);
        pushX(999999937LL);
        pushX(536870912LL);
        pushX(387420489LL);
        pushX(282475249LL);
        pushX(73513440LL);
        pushX(999999996LL);
        pushX(999999000LL);
        pushX(makeSemiprime(maxX));
        pushX(makePrimePower(maxX));
    } else if (seed == 3) {

        auto hc = highlyCompositeList(maxX, 60);
        pushX(1);
        pushX(maxX);
        pushX(999999937LL);
        pushX(536870912LL);
        pushX(387420489LL);
        for (int i = (int)hc.size() - 1; i >= 0 && (int)xs.size() < 20; i--) {
            pushX(hc[i]);
        }
        while ((int)xs.size() < 25) {
            pushX(makeSemiprime(maxX));
        }
    } else {
        int T = opt<int>("t", 100);
        auto hc = highlyCompositeList(maxX, 120);

        pushX(1);
        pushX(maxX);
        pushX(536870912LL);
        pushX(387420489LL);
        pushX(999999937LL);
        if (!hc.empty()) pushX(hc.back());

        while ((int)xs.size() < T) {
            int typ = rnd.next(0, 99);
            long long x = 1;

            if (typ < 25 && !hc.empty()) {
                int idx = rnd.next(0, (int)hc.size() - 1);
                x = hc[idx];
            } else if (typ < 40) {
                long long lo = max(2LL, maxX - 2000000LL);
                long long hi = maxX;
                x = primeNear(lo, hi);
            } else if (typ < 58) {
                x = makeSemiprime(maxX);
            } else if (typ < 72) {
                x = makePrimePower(maxX);
            } else if (typ < 88) {
                x = makeSmooth(maxX);
            } else {
                x = makeNearMax(maxX);
            }

            if (rnd.next(0, 9) == 0) {
                long long p = primeNear(max(2LL, maxX / 100000LL), maxX);
                long long m = makeSmooth(min(maxX, 100000LL));
                if (m < 1) m = 1;
                if ((__int128)p * m <= maxX) x = p * m;
            }

            pushX(x);
        }
    }

    int T = (int)xs.size();
    T = max(1, min(100, T));
    cout << T << "\n";
    for (int i = 0; i < T; i++) {
        cout << xs[i] << "\n";
    }
    return 0;
}


This task is interactive. After printing each line, you should flush the output buffer. You can use cout << flush in $\mathrm { C } + +$ , System.out.flush() in Java, and sys.stdout.flush() in Python. You must strictly follow the instructions in the Interaction section; otherwise you may receive verdicts like wrong answer, time limit exceeded, or others.

Because work in the mine is hard and time-consuming, the dwarves decided to seek help in automating some of their tasks. Their current task is to classify $N$ minerals (identified by numbers from 1 to $N$ ) into different classes. To assist with this, Dwarf the Worker has installed a new conveyor belt. The dwarves can add samples of minerals to the beginning of the belt and remove them from its end (i.e., remove the minerals that were added earliest).

Unfortunately, this conveyor belt has no knowledge base, so it cannot identify the class of any mineral on it. However, it has another useful feature: it displays how many distinct classes of minerals are currently on the belt. It turns out this is sufficient for the mine, as the dwarves do not need to know the class of each mineral: they just need to segregate the minerals into piles of the same class.

Can you help the dwarves solve this task using the conveyor belt?

# Interaction Protocol

The classes of minerals are fixed at the beginning and do not depend on the queries made.

First, read a line containing one integer $N$ , the number of minerals to classify. Then, you can perform operations of the following two types:

• + id — mineral with identifier $i d$ is added to the beginning of the conveyor belt, • - — the mineral at the end of the conveyor belt is removed. If the belt is empty, nothing happens.

After each operation, the conveyor belt outputs one line containing the number of distinct classes currently on it.

To output the answer, first print a line containing ! followed by $k$ being the number of distinct classes in the mine. Then print $k$ lines, each describing one class. Each line should contain a number $c _ { i }$ followed by $c _ { i }$ mineral identifiers of that class. Each of $N$ minerals must appear in exactly one class. You may output the classes and minerals within each class in any order.

Remember to flush the output after each operation and after writing the answer. Remember to put spaces between numbers and the $^ +$ , -, ! symbols. You must read all data from the interactor.

# Limits

$1 \le N \le 5 0 0$ , you can perform at most 35 000 operations.

# Sample interaction

The first sample test has $N = 3$ and the classes of minerals $1 , 2 , 3$ are $1 , 2 , 1$ respectively:

Input	Output	Status of the conveyor belt

3	+1	[1]

1	+2	[1，2]

2	+3	[1，2，1]

2	1	[2，1]

2	！2

213 12

# Local testing

In the Files section you can find A.zip containing sample tests and a grader grader.cpp. To test your solution, compile it, then pass the test name and your executable to the compiled ./grader:

./grader [test] [executable]

For example: ./grader 0b.in ./abc

The sample grader is not guaranteed to behave identically to the official one. However, neither is adaptive.

#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static long long queries = 0;
static long long query_limit = 35000;

static const long long HARD_CAP = 350000;

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());
}

int main(int argc, char** argv) {

    registerInteraction(argc, argv);
    atexit(log_metrics);

    int n = inf.readInt();

    vector<int> mineral_class(n + 1);
    set<int> all_true_classes;
    for (int i = 1; i <= n; i++) {
        mineral_class[i] = inf.readInt();
        all_true_classes.insert(mineral_class[i]);
    }

    cout << n << endl;

    deque<int> belt;
    map<int, int> class_counts;
    int distinct_on_belt = 0;

    while (true) {

        if (queries > HARD_CAP) {
            finish(_pe, "Hard query limit exceeded (spam protection)");
        }

        string token;

        if (!(cin >> token)) {
            finish(_pe, "Unexpected EOF: expected command or answer");
        }

        if (token == "?") {
            if (!(cin >> token)) {
                finish(_pe, "Unexpected EOF after '?'");
            }
        }

        if (token == "!") {

            int k;
            if (!(cin >> k)) {
                finish(_pe, "Expected integer k (number of classes) after '!'");
            }

            if (k != (int)all_true_classes.size()) {
                finish(_wa, format("Incorrect number of classes. Expected %d, found %d",
                                   (int)all_true_classes.size(), k));
            }

            set<int> used_minerals;
            set<int> used_classes_output;

            for (int i = 0; i < k; i++) {
                int c_size;
                if (!(cin >> c_size)) finish(_pe, "Expected class size");
                if (c_size <= 0) finish(_wa, "Class size must be positive");

                int group_class = -1;

                for (int j = 0; j < c_size; j++) {
                    int mid;
                    if (!(cin >> mid)) finish(_pe, "Expected mineral ID");

                    if (mid < 1 || mid > n) {
                        finish(_wa, format("Mineral ID %d out of bounds [1, %d]", mid, n));
                    }
                    if (used_minerals.count(mid)) {
                        finish(_wa, format("Mineral %d appears in multiple classes", mid));
                    }
                    used_minerals.insert(mid);

                    int actual_class = mineral_class[mid];
                    if (group_class == -1) {
                        group_class = actual_class;
                    } else {
                        if (actual_class != group_class) {
                            finish(_wa, format("Mineral %d belongs to class %d, but previous minerals in this group belonged to class %d",
                                               mid, actual_class, group_class));
                        }
                    }
                }

                if (used_classes_output.count(group_class)) {
                    finish(_wa, format("Class (internal ID %d) outputted as separate groups multiple times", group_class));
                }
                used_classes_output.insert(group_class);
            }

            if ((int)used_minerals.size() != n) {
                finish(_wa, format("Only %d out of %d minerals were classified", (int)used_minerals.size(), n));
            }

            finish(_ok, "Correct answer");
        }
        else if (token == "+") {

            int id;
            if (!(cin >> id)) finish(_pe, "Expected mineral ID after '+'");

            if (id < 1 || id > n) {
                finish(_pe, format("Mineral ID %d out of bounds [1, %d]", id, n));
            }

            belt.push_back(id);
            int c = mineral_class[id];
            if (class_counts[c] == 0) {
                distinct_on_belt++;
            }
            class_counts[c]++;

            queries++;
            cout << distinct_on_belt << endl;
        }
        else if (token == "-") {

            if (!belt.empty()) {
                int id = belt.front();
                belt.pop_front();
                int c = mineral_class[id];
                class_counts[c]--;
                if (class_counts[c] == 0) {
                    distinct_on_belt--;
                }
            }

            queries++;
            cout << distinct_on_belt << endl;
        }
        else {
            finish(_pe, format("Invalid token '%s'", token.c_str()));
        }

        if (queries % 100 == 0) {
            log_metrics();
        }
    }

    return 0;
}

#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 = opt<int>("n", -1);
    int k = opt<int>("k", -1);

    int seedVal = std::atoi(argv[1]);

    if (n == -1) {
        if (seedVal == 1) n = 1;
        else if (seedVal == 2) n = 2;
        else if (seedVal == 3) n = 3;
        else if (seedVal == 4) n = 500;
        else if (seedVal == 5) n = 500;
        else if (seedVal == 6) n = 500;
        else {

            int type = rnd.next(1, 10);
            if (type <= 2) n = rnd.next(1, 50);
            else if (type <= 4) n = rnd.next(50, 200);
            else n = rnd.next(400, 500);
        }
    }

    if (k == -1) {
        if (seedVal == 1) k = 1;
        else if (seedVal == 2) k = 1;
        else if (seedVal == 3) k = 2;
        else if (seedVal == 4) k = 1;
        else if (seedVal == 5) k = n;
        else if (seedVal == 6) k = 2;
        else {
            int type = rnd.next(1, 10);
            if (type == 1) k = 1;
            else if (type == 2) k = n;
            else if (type <= 5) k = rnd.next(1, min(n, 10));
            else if (type <= 7) k = rnd.next(max(1, n / 2), n);
            else k = rnd.next(1, n);
        }
    }

    if (k < 1) k = 1;
    if (k > n) k = n;

    if (mode == "adp") {

        cout << n << endl;
    } else {

        vector<int> counts = rnd.partition(k, n, 1);

        vector<int> classes;
        classes.reserve(n);

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < counts[i]; j++) {
                classes.push_back(i + 1);
            }
        }

        shuffle(classes.begin(), classes.end());

        cout << n << endl;
        for (int i = 0; i < n; i++) {
            cout << classes[i] << (i == n - 1 ? "" : " ");
        }
        cout << endl;
    }

    return 0;
}