You are playing a strange game with Li Chen. You have a tree with $n$ nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices from $1$ to $n$. Neither of you know the other's labelling of the tree.

You and Li Chen each chose a subtree (i.e., a connected subgraph) in that tree. Your subtree consists of the vertices labeled $x_1, x_2, \ldots, x_{k_1}$ in your labeling, Li Chen's subtree consists of the vertices labeled $y_1, y_2, \ldots, y_{k_2}$ in his labeling. The values of $x_1, x_2, \ldots, x_{k_1}$ and $y_1, y_2, \ldots, y_{k_2}$ are known to both of you.

 ![](https://espresso.codeforces.com/0defd3cbb822fdde342d5b0b173f9a1de22eaafc.png)  The picture shows two labelings of a possible tree: yours on the left and Li Chen's on the right. The selected trees are highlighted. There are two common nodes.

You want to determine whether your subtrees have at least one common vertex. Luckily, your friend Andrew knows both labelings of the tree. You can ask Andrew at most $5$ questions, each of which is in one of the following two forms:

* A x: Andrew will look at vertex $x$ in your labeling and tell you the number of this vertex in Li Chen's labeling.
* B y: Andrew will look at vertex $y$ in Li Chen's labeling and tell you the number of this vertex in your labeling.

Determine whether the two subtrees have at least one common vertex after asking some questions. If there is at least one common vertex, determine one of your labels for any of the common vertices.

Interaction

Each test consists of several test cases.

The first line of input contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases.

For each testcase, your program should interact in the following format.

The first line contains a single integer $n$ ($1 \leq n \leq 1\,000$) — the number of nodes in the tree.

Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ ($1\leq a_i, b_i\leq n$) — the edges of the tree, indicating an edge between node $a_i$ and $b_i$ according to your labeling of the nodes.

The next line contains a single integer $k_1$ ($1 \leq k_1 \leq n$) — the number of nodes in your subtree.

The next line contains $k_1$ distinct integers $x_1,x_2,\ldots,x_{k_1}$ ($1 \leq x_i \leq n$) — the indices of the nodes in your subtree, according to your labeling. It is guaranteed that these vertices form a subtree.

The next line contains a single integer $k_2$ ($1 \leq k_2 \leq n$) — the number of nodes in Li Chen's subtree.

The next line contains $k_2$ distinct integers $y_1, y_2, \ldots, y_{k_2}$ ($1 \leq y_i \leq n$) — the indices (according to Li Chen's labeling) of the nodes in Li Chen's subtree. It is guaranteed that these vertices form a subtree according to Li Chen's labelling of the tree's nodes.

Test cases will be provided one by one, so you must complete interacting with the previous test (i.e. by printing out a common node or $-1$ if there is not such node) to start receiving the next one.

You can ask the Andrew two different types of questions.

* You can print "A x" ($1 \leq x \leq n$). Andrew will look at vertex $x$ in your labeling and respond to you with the number of this vertex in Li Chen's labeling.
* You can print "B y" ($1 \leq y \leq n$). Andrew will look at vertex $y$ in Li Chen's labeling and respond to you with the number of this vertex in your labeling.

You may only ask at most $5$ questions per tree.

When you are ready to answer, print "C s", where $s$ is your label of a vertex that is common to both subtrees, or $-1$, if no such vertex exists. Printing the answer does not count as a question. Remember to flush your answer to start receiving the next test case.

After printing a question do not forget to print end of 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 documentation for other languages.

If the judge responds with $-1$, it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive Wrong Answer; it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.

Examples

Input

```
1
3
1 2
2 3
1
1
1
2
2
1
```

Output

```
A 1
B 2
C 1
```

Input

```
2
6
1 2
1 3
1 4
4 5
4 6
4
1 3 4 5
3
3 5 2
3
6
1 2
1 3
1 4
4 5
4 6
3
1 2 3
3
4 1 6
5
```

Output

```
B 2
C 1
A 1
C -1
```

Note

For the first sample, Li Chen's hidden permutation is $[2, 3, 1]$, and for the second, his hidden permutation is $[5, 3, 2, 4, 1, 6]$ for both cases.

In the first sample, there is a tree with three nodes in a line. On the top, is how you labeled the tree and the subtree you chose, and the bottom is how Li Chen labeled the tree and the subtree he chose:

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

In the first question, you ask Andrew to look at node $1$ in your labelling and tell you the label of it in Li Chen's labelling. Andrew responds with $2$. At this point, you know that both of your subtrees contain the same node (i.e. node $1$ according to your labeling), so you can output "C 1" and finish. However, you can also ask Andrew to look at node $2$ in Li Chen's labelling and tell you the label of it in your labelling. Andrew responds with $1$ (this step was given with the only reason — to show you how to ask questions).

For the second sample, there are two test cases. The first looks is the one from the statement:

 ![](https://espresso.codeforces.com/6e657baf0e89b8da496fe553b22e3f133bb24d85.png)

We first ask "B 2", and Andrew will tell us $3$. In this case, we know $3$ is a common vertex, and moreover, any subtree with size $3$ that contains node $3$ must contain node $1$ as well, so we can output either "C 1" or "C 3" as our answer.

In the second case in the second sample, the situation looks as follows:

 ![](https://espresso.codeforces.com/4caafc9d13cebf999fafcd0415c5a1acc5f3c908.png)

In this case, you know that the only subtree of size $3$ that doesn't contain node $1$ is subtree $4,5,6$. You ask Andrew for the label of node $1$ in Li Chen's labelling and Andrew says $5$. In this case, you know that Li Chen's subtree doesn't contain node $1$, so his subtree must be consist of the nodes $4,5,6$ (in your labelling), thus the two subtrees have no common nodes.

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

static int max_queries = 0;
static int queries_limit = 5;

static void log_metrics() {
    try {
        tout << "queries=" << max_queries << endl;
        tout << "query_limit=" << queries_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 test_idx = 1; test_idx <= t; ++test_idx) {

        int n = inf.readInt();
        vector<pair<int, int>> edges;
        for (int i = 0; i < n - 1; ++i) {
            int u = inf.readInt();
            int v = inf.readInt();
            edges.push_back({u, v});
        }

        int k1 = inf.readInt();
        vector<int> s1;
        for (int i = 0; i < k1; ++i) s1.push_back(inf.readInt());

        int k2 = inf.readInt();
        vector<int> s2;
        for (int i = 0; i < k2; ++i) s2.push_back(inf.readInt());

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

        cout << n << endl;
        for (auto &e : edges) cout << e.first << " " << e.second << endl;

        cout << k1 << endl;
        for (int i = 0; i < k1; ++i) cout << s1[i] << (i == k1 - 1 ? "" : " ");
        cout << endl;

        cout << k2 << endl;
        for (int i = 0; i < k2; ++i) cout << s2[i] << (i == k2 - 1 ? "" : " ");
        cout << endl;
        cout.flush();

        log_metrics();

        int current_queries = 0;

        while (true) {
            string type;
            if (!(cin >> type)) {
                finish(_wa, "Unexpected EOF (contestant output ended early)");
            }

            if (type == "?" || type == "!") {
                if (!(cin >> type)) finish(_wa, "Unexpected EOF after prefix");
            }

            if (type == "A") {

                if (current_queries >= 5) {
                    cout << -1 << endl;
                    finish(_wa, "Query limit exceeded (5)");
                }

                int x;
                if (!(cin >> x)) finish(_wa, "Expected integer after A");

                if (x < 1 || x > n) {
                    cout << -1 << endl;
                    finish(_wa, format("Invalid query A %d (n=%d)", x, n));
                }

                current_queries++;
                if (current_queries > max_queries) {
                    max_queries = current_queries;
                    log_metrics();
                }

                cout << p[x - 1] << endl;
                cout.flush();

            } else if (type == "B") {

                if (current_queries >= 5) {
                    cout << -1 << endl;
                    finish(_wa, "Query limit exceeded (5)");
                }

                int y;
                if (!(cin >> y)) finish(_wa, "Expected integer after B");

                if (y < 1 || y > n) {
                    cout << -1 << endl;
                    finish(_wa, format("Invalid query B %d (n=%d)", y, n));
                }

                current_queries++;
                if (current_queries > max_queries) {
                    max_queries = current_queries;
                    log_metrics();
                }

                cout << inv_p[y] << endl;
                cout.flush();

            } else if (type == "C") {

                int s;
                if (!(cin >> s)) finish(_wa, "Expected integer after C");

                set<int> set_s2(s2.begin(), s2.end());
                bool intersection_exists = false;
                for (int u : s1) {

                    if (set_s2.count(p[u - 1])) {
                        intersection_exists = true;
                        break;
                    }
                }

                if (s == -1) {
                    if (intersection_exists) {
                        finish(_wa, "Contestant claimed disjoint (-1), but intersection exists");
                    }
                } else {

                    bool in_s1 = false;
                    for (int u : s1) if (u == s) in_s1 = true;

                    if (!in_s1) {
                        finish(_wa, format("Output vertex %d is not in your subtree S1", s));
                    }

                    if (s < 1 || s > n) {
                        finish(_wa, format("Output vertex %d out of range", s));
                    }

                    if (!set_s2.count(p[s - 1])) {
                        finish(_wa, format("Vertex %d maps to %d (opponent), which is not in S2", s, p[s - 1]));
                    }
                }

                break;

            } else {
                finish(_wa, "Unknown query type: " + type);
            }
        }
    }

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

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

using namespace std;

static int max_queries_seen = 0;
static const int QUERY_LIMIT_VAL = 5;

static void log_metrics() {

    try {
        tout << "queries=" << max_queries_seen << endl;
        tout << "query_limit=" << QUERY_LIMIT_VAL << 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;

    while (t--) {

        int n = inf.readInt();
        vector<pair<int, int>> edges;
        for (int i = 0; i < n - 1; ++i) {
            int u = inf.readInt();
            int v = inf.readInt();
            edges.push_back({u, v});
        }

        int k1 = inf.readInt();
        vector<int> s1(k1);
        for (int i = 0; i < k1; ++i) s1[i] = inf.readInt();

        int k2 = inf.readInt();
        vector<int> s2(k2);
        for (int i = 0; i < k2; ++i) s2[i] = inf.readInt();

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

        cout << n << endl;
        for (const auto& e : edges) {
            cout << e.first << " " << e.second << endl;
        }

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

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

        cout.flush();

        int queries_this_case = 0;
        set<int> set1(s1.begin(), s1.end());
        set<int> set2(s2.begin(), s2.end());

        if (max_queries_seen == 0) log_metrics();

        bool case_active = true;
        while (case_active) {
            string type;

            if (!(cin >> type)) {
                finish(_pe, "Unexpected EOF while waiting for query/answer");
            }

            if (type == "A") {
                int x;
                if (!(cin >> x)) finish(_pe, "Expected integer after A");

                queries_this_case++;
                max_queries_seen = max(max_queries_seen, queries_this_case);

                if (queries_this_case > 200) {
                    finish(_pe, "Hard query limit exceeded (>200)");
                }

                if (x < 1 || x > n) {
                    finish(_wa, "Query A: index out of range");
                }

                cout << p[x - 1] << endl;
            }
            else if (type == "B") {
                int y;
                if (!(cin >> y)) finish(_pe, "Expected integer after B");

                queries_this_case++;
                max_queries_seen = max(max_queries_seen, queries_this_case);

                if (queries_this_case > 200) {
                    finish(_pe, "Hard query limit exceeded (>200)");
                }

                if (y < 1 || y > n) {
                    finish(_wa, "Query B: index out of range");
                }

                cout << inv_p[y] << endl;
            }
            else if (type == "C") {
                int s;
                if (!(cin >> s)) finish(_pe, "Expected integer after C");

                bool intersection_exists = false;
                for (int u : s1) {

                    if (set2.count(p[u - 1])) {
                        intersection_exists = true;
                        break;
                    }
                }

                if (s == -1) {
                    if (intersection_exists) {
                        finish(_wa, "Contestant answered -1 but intersection exists");
                    }
                } else {
                    if (!intersection_exists) {
                        finish(_wa, "Contestant returned node but no intersection exists");
                    }
                    if (s < 1 || s > n) {
                        finish(_wa, "Answer index out of range");
                    }
                    if (!set1.count(s)) {
                        finish(_wa, "Answer node not in your subtree (S1)");
                    }
                    if (!set2.count(p[s - 1])) {
                        finish(_wa, "Answer node corresponds to node not in Li Chen's subtree (S2)");
                    }
                }

                case_active = false;
            }
            else {
                finish(_pe, "Invalid query type: " + type);
            }
        }
    }

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

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

using namespace std;

vector<pair<int, int>> genTree(int n) {
    vector<pair<int, int>> edges;
    if (n == 1) return edges;

    int type = rnd.next(1, 10);

    if (type <= 6) {

        vector<pair<int, int>> temp_edges;
        for (int i = 2; i <= n; i++) {
            temp_edges.push_back({rnd.next(1, i - 1), i});
        }

        vector<int> p(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());

        for (auto &e : temp_edges) {
            edges.push_back({p[e.first - 1], p[e.second - 1]});
        }
    } else if (type <= 8) {

        vector<int> p(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());
        for (int i = 0; i < n - 1; ++i) {
            edges.push_back({p[i], p[i + 1]});
        }
    } else {

        vector<int> p(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());
        int center = p[0];
        for (int i = 1; i < n; ++i) {
            edges.push_back({p[i], center});
        }
    }
    return edges;
}

vector<int> genSubtree(int n, const vector<vector<int>>& adj, int size, const vector<int>& restriction = {}) {
    if (size == n && restriction.empty()) {
        vector<int> res(n);
        iota(res.begin(), res.end(), 1);
        return res;
    }

    vector<bool> allowed(n + 1, true);
    if (!restriction.empty()) {
        fill(allowed.begin(), allowed.end(), false);
        for (int x : restriction) allowed[x] = true;
    }

    vector<int> candidates;
    for (int i = 1; i <= n; i++) if (allowed[i]) candidates.push_back(i);

    if (candidates.empty()) return {};

    int root = candidates[rnd.next(0, (int)candidates.size() - 1)];
    vector<int> sub;
    sub.push_back(root);

    vector<bool> inSub(n + 1, false);
    inSub[root] = true;

    set<int> boundary;
    for (int v : adj[root]) {
        if (allowed[v]) boundary.insert(v);
    }

    while ((int)sub.size() < size && !boundary.empty()) {
        int idx = rnd.next(0, (int)boundary.size() - 1);
        auto it = boundary.begin();
        advance(it, idx);
        int v = *it;
        boundary.erase(it);

        inSub[v] = true;
        sub.push_back(v);

        for (int u : adj[v]) {
            if (allowed[u] && !inSub[u]) {
                boundary.insert(u);
            }
        }
    }
    return sub;
}

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

    string mode = opt<string>("mode", "non");
    int tOpt = opt<int>("t", -1);
    int budgetOpt = opt<int>("sum_n", -1);

    int seed = atoi(argv[1]);

    int t;
    if (tOpt != -1) {
        t = tOpt;
    } else if (mode == "adp") {

        t = (seed % 3 == 0) ? 25 : rnd.next(5, 15);
    } else {
        if (seed == 1) t = 1;
        else if (seed == 2) t = 2;
        else t = rnd.next(3, 10);
    }
    t = max(1, min(100, t));

    int sum_budget;
    if (budgetOpt != -1) sum_budget = budgetOpt;
    else sum_budget = (mode == "adp" ? 9000 : 6000);
    sum_budget = max(2 * t, sum_budget);

    vector<int> ns;
    ns.reserve(t);
    int remaining = sum_budget;
    for (int i = 0; i < t; i++) {
        int min_n = 2;
        if (seed == 1 && i == 0) min_n = 1;
        int min_left = (t - i - 1) * 2;
        int hi = min(1000, remaining - min_left);
        if (hi < min_n) hi = min_n;

        int n;
        if (mode == "adp") {

            int lo = min(hi, max(min_n, 700));
            n = rnd.next(lo, hi);
        } else {
            int type = rnd.next(1, 100);
            if (type <= 10) n = rnd.next(min_n, min(hi, 10));
            else if (type <= 30) n = rnd.next(min_n, min(hi, 100));
            else n = rnd.next(min_n, hi);
        }
        ns.push_back(n);
        remaining -= n;
    }

    cout << t << endl;

    for (int test_idx = 0; test_idx < t; test_idx++) {
        int n = ns[test_idx];

        vector<pair<int, int>> edges = genTree(n);
        vector<vector<int>> adj(n + 1);
        for (auto e : edges) {
            adj[e.first].push_back(e.second);
            adj[e.second].push_back(e.first);
        }

        cout << n << endl;
        for (auto e : edges) {
            cout << e.first << " " << e.second << endl;
        }

        int k1;
        if (n == 1) k1 = 1;
        else if (mode == "adp") k1 = rnd.next(1, max(1, n / 2));
        else k1 = rnd.next(1, n);

        vector<int> s1 = genSubtree(n, adj, k1);
        cout << (int)s1.size() << endl;
        for (int i = 0; i < (int)s1.size(); i++) cout << s1[i] << (i == (int)s1.size() - 1 ? "" : " ");
        cout << endl;

        vector<int> s2_p1;

        int rel = rnd.next(1, 100);
        bool disjoint = false;
        if (mode == "adp") {

            if (rel <= 60) disjoint = true;
        } else {
            if (rel <= 40) disjoint = true;
        }

        vector<bool> inS1(n + 1, false);
        for (int x : s1) inS1[x] = true;

        if (disjoint) {

            vector<int> outside;
            for (int i = 1; i <= n; i++) if (!inS1[i]) outside.push_back(i);

            if (outside.empty()) {

                s2_p1 = genSubtree(n, adj, rnd.next(1, n));
            } else {
                int max_k2 = (int)outside.size();
                int k2 = rnd.next(1, max_k2);
                s2_p1 = genSubtree(n, adj, k2, outside);
            }
        } else {

            int choice = rnd.next(1, 2);
            if (choice == 1) {

                s2_p1 = genSubtree(n, adj, rnd.next(1, n));
            } else {

                int root = s1[rnd.next(0, (int)s1.size() - 1)];
                int k2 = rnd.next(1, n);

                s2_p1.push_back(root);
                vector<bool> inS2(n + 1, false);
                inS2[root] = true;
                set<int> boundary;
                for (int v : adj[root]) boundary.insert(v);

                while ((int)s2_p1.size() < k2 && !boundary.empty()) {
                    int idx = rnd.next(0, (int)boundary.size() - 1);
                    auto it = boundary.begin();
                    advance(it, idx);
                    int v = *it;
                    boundary.erase(it);
                    inS2[v] = true;
                    s2_p1.push_back(v);
                    for (int u : adj[v]) if (!inS2[u]) boundary.insert(u);
                }
            }
        }

        vector<int> p = rnd.perm(n, 1);

        vector<int> s2_p2;
        for (int x : s2_p1) s2_p2.push_back(p[x - 1]);

        cout << (int)s2_p2.size() << endl;
        for (int i = 0; i < (int)s2_p2.size(); i++) cout << s2_p2[i] << (i == (int)s2_p2.size() - 1 ? "" : " ");
        cout << endl;

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

    return 0;
}


This is an interactive problem.

There was a tournament consisting of $2^n$ contestants. The $1$-st contestant competed with the $2$-nd, the $3$-rd competed with the $4$-th, and so on. After that, the winner of the first match competed with the winner of second match, etc. The tournament ended when there was only one contestant left, who was declared the winner of the tournament. Such a tournament scheme is known as the single-elimination tournament.

You don't know the results, but you want to find the winner of the tournament. In one query, you select two integers $a$ and $b$, which are the indices of two contestants. The jury will return $1$ if $a$ won more matches than $b$, $2$ if $b$ won more matches than $a$, or $0$ if their number of wins was equal.

Find the winner in no more than $\left \lceil \frac{1}{3} \cdot 2^{n + 1} \right \rceil$ queries. Here $\lceil x \rceil$ denotes the value of $x$ rounded up to the nearest integer.

Note that the tournament is long over, meaning that the results are fixed and do not depend on your queries.

Input

The first line contains a single integer $t$ ($1 \leq t \leq 2^{14}$) — the number of test cases.

The only line of input contains a single integer $n$ ($1 \leq n \leq 17$).

It is guaranteed that the sum of $2^n$ over all test cases does not exceed $2^{17}$.

Interaction

The interaction for each test case begins by reading the integer $n$.

To make a query, output "? a b" ($1 \leq a, b \leq 2^n$) without quotes. Afterwards, you should read one single integer — the answer for your query. You can make at most $\left \lceil \frac{1}{3} \cdot 2^{n + 1} \right \rceil$ such queries in each test case.

If you receive the integer $-1$ instead of an answer or a valid value of $n$, it means your program has made an invalid query, has exceed the limit of queries, or has given incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

When you are ready to give the final answer, output "! x" ($1 \leq x \leq 2^n$) without quotes — the winner of the tournament. Giving this answer does not count towards the limit of queries. After solving a test case, your program should move to the next one immediately. After solving all test cases, your program should be terminated immediately.

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

Example

Input

```
1
3

2

0

2
```

Output

```
? 1 4

? 1 6

? 5 7

! 7
```

Note

The tournament in the first test case is shown below. The number of wins is $[1,0,0,2,0,1,3,0]$.

 ![](https://espresso.codeforces.com/676d8080959faeceabb866efe8792b034ebdadf5.png) In this example, the winner is the $7$-th contestant.

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

using namespace std;

static long long global_queries = 0;
static long long global_limit = 0;

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

    }
}

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

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

    setName("Interactive judge for CF1713D (Non-Adaptive)");
    registerInteraction(argc, argv);

    atexit(log_metrics);

    int t = inf.readInt();

    cout << t << endl;

    for (int tc = 1; tc <= t; ++tc) {
        int n = inf.readInt();
        int m = 1 << n;

        vector<int> strength(m + 1);
        for (int i = 1; i <= m; ++i) {
            strength[i] = inf.readInt();
        }

        vector<int> wins(m + 1, 0);
        vector<int> current_round(m);
        iota(current_round.begin(), current_round.end(), 1);

        for (int r = 0; r < n; ++r) {
            vector<int> next_round;

            for (size_t i = 0; i < current_round.size(); i += 2) {
                int u = current_round[i];
                int v = current_round[i+1];

                int winner = (strength[u] > strength[v]) ? u : v;
                wins[winner]++;
                next_round.push_back(winner);
            }
            current_round = next_round;
        }
        int tournament_winner = current_round[0];

        long long num = 1LL << (n + 1);
        long long local_limit = (num + 2) / 3;
        global_limit += local_limit;

        log_metrics();

        cout << n << endl;

        int local_queries = 0;

        while (true) {
            string token;

            if (!(cin >> token)) {
                safe_finish(_wa, "Unexpected EOF while reading token");
            }

            if (token == "!") {

                int ans;
                if (!(cin >> ans)) {
                    safe_finish(_wa, "Expected answer after '!'");
                }

                if (ans == tournament_winner) {
                    break;
                } else {

                    cout << -1 << endl;
                    safe_finish(_wa, format("Wrong answer: expected %d, got %d", tournament_winner, ans));
                }
            } else {

                int a, b;
                if (token == "?") {
                    if (!(cin >> a)) safe_finish(_wa, "Expected 1st index after '?'");
                } else {

                    try {
                        a = stoi(token);
                    } catch (...) {
                        safe_finish(_pe, format("Invalid token '%s'", token.c_str()));
                    }
                }
                if (!(cin >> b)) safe_finish(_wa, "Expected 2nd index");

                if (a < 1 || a > m || b < 1 || b > m) {
                    cout << -1 << endl;
                    safe_finish(_wa, format("Index out of range: %d %d", a, b));
                }

                local_queries++;
                global_queries++;

                if (local_queries > local_limit) {
                    cout << -1 << endl;
                    safe_finish(_wa, format("Query limit exceeded (used %d, limit %lld)", local_queries, local_limit));
                }

                int res = 0;
                if (wins[a] > wins[b]) res = 1;
                else if (wins[b] > wins[a]) res = 2;
                else res = 0;

                cout << res << endl;
            }
        }
    }

    safe_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;

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

    }
}

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

mt19937 rng(58723);

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) {

        int n = inf.readInt();
        int num_players = 1 << n;

        long long current_limit = ((1LL << (n + 1)) + 2) / 3;
        total_query_limit += current_limit;

        log_metrics();

        cout << n << endl;

        vector<int> p(num_players);
        string token = inf.readToken();

        if (token == "adaptive") {
            int target = inf.readInt();
            int target_idx = target - 1;

            p[target_idx] = num_players;

            vector<int> others;
            others.reserve(num_players - 1);
            for (int v = 1; v < num_players; ++v) {
                others.push_back(v);
            }

            shuffle(others.begin(), others.end(), rng);

            int ptr = 0;
            for (int j = 0; j < num_players; ++j) {
                if (j == target_idx) continue;
                p[j] = others[ptr++];
            }
        } else {

            p[0] = stoi(token);
            for (int j = 1; j < num_players; ++j) {
                p[j] = inf.readInt();
            }
        }

        vector<int> wins(num_players, 0);
        vector<int> current_round(num_players);
        iota(current_round.begin(), current_round.end(), 0);

        while (current_round.size() > 1) {
            vector<int> next_round;
            for (size_t k = 0; k < current_round.size(); k += 2) {
                int u = current_round[k];
                int v = current_round[k+1];

                if (p[u] > p[v]) {
                    wins[u]++;
                    next_round.push_back(u);
                } else {
                    wins[v]++;
                    next_round.push_back(v);
                }
            }
            current_round = next_round;
        }

        int winner_idx = current_round[0];

        int queries_this_case = 0;
        bool case_active = true;

        while (case_active) {
            string type;
            if (!(cin >> type)) {
                finish_verdict(_wa, "Unexpected EOF from solution");
            }

            if (type == "!") {
                int ans;
                if (!(cin >> ans)) {
                    finish_verdict(_wa, "Unexpected EOF reading answer");
                }

                if (ans != winner_idx + 1) {
                    finish_verdict(_wa, format("Wrong answer on case %d. Expected %d, found %d", i + 1, winner_idx + 1, ans));
                }

                case_active = false;
            }
            else if (type == "?") {
                int u, v;
                if (!(cin >> u >> v)) {
                    finish_verdict(_wa, "Unexpected EOF reading query indices");
                }

                queries_this_case++;
                total_queries++;

                if (queries_this_case > current_limit) {

                    cout << -1 << endl;
                    finish_verdict(_wa, format("Query limit exceeded on case %d", i + 1));
                }

                if (u < 1 || u > num_players || v < 1 || v > num_players) {
                    cout << -1 << endl;
                    finish_verdict(_wa, format("Invalid indices on case %d: %d %d", i + 1, u, v));
                }

                int resp;
                if (u == v) {
                    resp = 0;
                } else {
                    int w1 = wins[u - 1];
                    int w2 = wins[v - 1];

                    if (w1 > w2) resp = 1;
                    else if (w2 > w1) resp = 2;
                    else resp = 0;
                }

                cout << resp << endl;
            }
            else {
                finish_verdict(_wa, format("Invalid query type '%s' on case %d", type.c_str(), i + 1));
            }

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

    finish_verdict(_ok, "Passed all tests");
    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");

    const int BUDGET = 131072;

    const int MAX_T = 16384;

    vector<int> ns;
    int seed = atoi(argv[1]);

    if (seed == 1) {

        ns.push_back(17);
    }
    else if (seed == 2) {

        ns.assign(MAX_T, 1);
    }
    else if (seed == 3) {

        for (int i = 1; i <= 16; ++i) {
            ns.push_back(i);
        }
    }
    else {

        int current_sum = 0;
        int dtype = rnd.next(1, 3);

        if (dtype == 1) {

            while (current_sum < BUDGET && ns.size() < MAX_T) {
                int rem = BUDGET - current_sum;
                int max_n = 0;
                while ((1 << (max_n + 1)) <= rem) max_n++;
                max_n = min(max_n, 17);
                if (max_n == 0) break;

                int n = rnd.next(max(1, max_n - 4), max_n);
                ns.push_back(n);
                current_sum += (1 << n);
            }
        }
        else if (dtype == 2) {

            while (current_sum < BUDGET && ns.size() < MAX_T) {
                int rem = BUDGET - current_sum;
                int max_n = 0;
                while ((1 << (max_n + 1)) <= rem) max_n++;
                max_n = min(max_n, 17);
                if (max_n == 0) break;

                int n = rnd.next(1, min(max_n, 10));

                if (rnd.next(10) == 0) n = rnd.next(1, max_n);

                ns.push_back(n);
                current_sum += (1 << n);
            }
        }
        else {

            while (current_sum < BUDGET && ns.size() < MAX_T) {
                int rem = BUDGET - current_sum;
                int max_n = 0;
                while ((1 << (max_n + 1)) <= rem) max_n++;
                max_n = min(max_n, 17);
                if (max_n == 0) break;

                int n = rnd.next(1, max_n);
                ns.push_back(n);
                current_sum += (1 << n);
            }
        }

        shuffle(ns.begin(), ns.end());
    }

    println(ns.size());

    for (int n : ns) {
        println(n);
        int count = 1 << n;

        if (mode == "non") {

            vector<int> p(count);
            iota(p.begin(), p.end(), 1);
            shuffle(p.begin(), p.end());
            println(p);
        }
        else if (mode == "adp") {

            int target = rnd.next(1, count);
            cout << "adaptive " << target << endl;
        }
    }

    return 0;
}


This is an interactive problem.

The jury has a hidden tree with $n$ vertices, numbered from 1 to $n$ . The tree has $n - 1$ edges, the $i$ -th edge connects vertices $s _ { i }$ and $f _ { i }$ . You do not know the edges, but you can ask jury questions about the tree.

Each query you make must contain one vertex $v$ and a binary string $b$ of length $n - 1$ . The jury does the following things for the query:

• The tree is considered as a rooted tree with vertex $v$ being the root.
• Each edge of the tree becomes directed. More specifically, if $b _ { i } = 1$ , then the $i$ -th edge becomes directed from $s _ { i }$ to $f _ { i }$ . Otherwise, it is directed from $f _ { i }$ to $s _ { i }$ .
• The answer to the query is the number of edges that are directed towards the root. In other words, the answer to the query is the number of tree edges for which the distance between its start to $\boldsymbol { v }$ is larger than the distance between its end and $v$ . Here the distance between two vertices is the number of edges on the simple undirected path between these vertices.

Your task is to guess all $s _ { i }$ and $f _ { i }$ in no more than 10 000 queries.

# Interaction Protocol

The first line contains the only integer $n$ ( $2 \leq n \leq 5 0 0$ ) — the number of vertices in the tree.

After reading $n$ , you can make no more than 10 000 queries of form $^ { 6 6 } ?$ $v$ $b ^ { \prime }$ , where $v$ is the vertex number ( $1 \leq v \leq n ,$ ), and $b$ is a binary string of length $n - 1$ .

For each query, the jury responds with a line containing a single integer, denoting the number of edges that are directed towards $v$ .

When you are ready to tell all $s _ { i }$ and $f _ { i }$ , print “! $ ! s _ { 1 } f _ { 1 } s _ { 2 } f _ { 2 } \ldots s _ { n - 1 } f _ { n - 1 } ? .$ .

You may assume that the tree itself and all $s _ { i }$ and $f _ { i }$ are defined before the start of interaction (i.e. tha the interactor is not adaptive).

# Example

standard input	standard output

3	？101

2

2	？200

	？311

1	！2321

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

using namespace std;

static int queries = 0;
static constexpr int QUERY_LIMIT = 10000;

static void log_queries() {
    tout << "queries=" << queries << '\n';
    tout << "query_limit=" << QUERY_LIMIT << '\n';
}

[[noreturn]] static void quit_with(TResult verdict, const string& msg) {
    log_queries();
    quitf(verdict, "%s", msg.c_str());
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);
    atexit(log_queries);

    const int n = inf.readInt(2, 500, "n");
    vector<int> s(n), f(n);
    vector<vector<int>> g(n + 1);
    for (int i = 1; i <= n - 1; i++) {
        s[i] = inf.readInt(1, n, "s");
        inf.readSpace();
        f[i] = inf.readInt(1, n, "f");
        inf.readEoln();
        g[s[i]].push_back(f[i]);
        g[f[i]].push_back(s[i]);
    }

    vector<vector<int>> dist(n + 1, vector<int>(n + 1, -1));
    for (int root = 1; root <= n; root++) {
        queue<int> q;
        dist[root][root] = 0;
        q.push(root);
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int v : g[u]) {
                if (dist[root][v] != -1) {
                    continue;
                }
                dist[root][v] = dist[root][u] + 1;
                q.push(v);
            }
        }
    }

    cout << n << endl;

    while (true) {
        string cmd;
        if (!(cin >> cmd)) {
            quit_with(_pe, "Unexpected EOF");
        }

        if (cmd == "?") {
            int v = 0;
            string b;
            if (!(cin >> v >> b)) {
                quit_with(_pe, "Invalid query");
            }
            if (v < 1 || v > n) {
                quit_with(_pe, "Query root out of range");
            }
            if ((int)b.size() != n - 1) {
                quit_with(_pe, "Bad query string length");
            }
            for (char ch : b) {
                if (ch != '0' && ch != '1') {
                    quit_with(_pe, "Bad query string");
                }
            }

            queries++;
            if (queries > QUERY_LIMIT) {
                quit_with(_wa, "Too many queries");
            }

            int toward = 0;
            for (int i = 1; i <= n - 1; i++) {
                const bool dir = (b[i - 1] == '1');
                const int start = dir ? s[i] : f[i];
                const int end = dir ? f[i] : s[i];
                if (dist[v][start] > dist[v][end]) {
                    toward++;
                }
            }

            cout << toward << endl;
            continue;
        }

        if (cmd == "!") {
            vector<int> gs(n), gf(n);
            for (int i = 1; i <= n - 1; i++) {
                if (!(cin >> gs[i] >> gf[i])) {
                    quit_with(_pe, "Invalid answer");
                }
                if (gs[i] < 1 || gs[i] > n || gf[i] < 1 || gf[i] > n) {
                    quit_with(_pe, "Answer out of range");
                }
            }
            for (int i = 1; i <= n - 1; i++) {
                if (gs[i] != s[i] || gf[i] != f[i]) {
                    quit_with(_wa, "Wrong answer");
                }
            }
            quit_with(_ok, "Correct");
        }

        quit_with(_pe, "Unknown command: " + cmd);
    }
}

#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");

    long long seed_val = 0;
    try {
        seed_val = stoll(argv[1]);
    } catch (...) {
        seed_val = 100;
    }

    int n;
    int type = -1;

    if (seed_val == 1) {
        n = 2;
        type = 0;
    } else if (seed_val == 2) {
        n = 3;
        type = 1;
    } else if (seed_val == 3) {
        n = 3;
        type = 2;
    } else {

        int p = rnd.next(1, 100);
        if (p <= 5) n = rnd.next(2, 20);
        else if (p <= 20) n = rnd.next(21, 100);
        else n = rnd.next(450, 500);

        type = rnd.next(0, 5);
    }

    int n_override = opt<int>("n", 0);
    if (n_override > 0) {
        n = n_override;
    }

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

    vector<pair<int,int>> edges;
    auto add_edge = [&](int u, int v) {
        edges.push_back({u, v});
    };

    if (type == 0) {

        for (int i = 2; i <= n; ++i)
            add_edge(rnd.next(1, i - 1), i);
    } else if (type == 1) {

        for (int i = 1; i < n; ++i)
            add_edge(i, i + 1);
    } else if (type == 2) {

        for (int i = 2; i <= n; ++i)
            add_edge(1, i);
    } else if (type == 3) {

        int spine = rnd.next(2, n);
        for (int i = 1; i < spine; ++i)
            add_edge(i, i + 1);
        for (int i = spine + 1; i <= n; ++i)
            add_edge(rnd.next(1, spine), i);
    } else if (type == 4) {

        for (int i = 2; i <= n; ++i)
            add_edge(i / 2, i);
    } else {

        int h = rnd.next(2, max(2, n - 2));
        for (int i = 1; i < h; ++i)
            add_edge(i, i + 1);
        for (int i = h + 1; i <= n; ++i)
            add_edge(h, i);
    }

    vector<int> p(n + 1);
    iota(p.begin(), p.end(), 0);
    shuffle(p.begin() + 1, p.end());

    vector<pair<int,int>> final_edges;
    for (auto& e : edges) {
        int u = p[e.first];
        int v = p[e.second];

        if (rnd.next(2)) swap(u, v);
        final_edges.push_back({u, v});
    }

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

    cout << n << endl;
    for (const auto& e : final_edges) {
        cout << e.first << " " << e.second << endl;
    }

    return 0;
}


You found an antique box in your warehouse and decided to open it. The moment you open the box, it traps you into a cursed game where you are playing against a demon. The game consists of 333 rounds and you have to win all rounds in order to escape. The demon also gives you 999 coins that you can use throughout all rounds.

Note that in this problem, denote cell $( r , c )$ of a grid as the cell in row $r$ and column $c$ of the grid.

Before the start of each round, the demon will prepare a secret paper, which can be represented as a grid with 3 rows and 3 columns, both numbered from 1 to 3. The demon will secretly put a hole in one or more cells, and you are not aware which cells have a hole in it. Then, the round starts with the demon giving you an odd integer $N$ $3 \leq N \leq 3 3$ ).

Within each round, you can ask the demon several queries, each costing you one coin. For each query, you are allowed to give the demon your paper, which can be represented as a grid with $N$ rows and $N$ columns, both numbered from 1 to $N$ . Each cell is coloured either black or white by you.

For each of your query, the demon will calculate a binary result grid with $N - 2$ rows and $N - 2$ columns, both numbered from 1 to $N - 2$ . The value on cell $( r , c )$ of the result grid is filled as follows.

• The demon will put the secret paper on top of your paper such that the cell $( r + i - 1 , c + j - 1 )$ of your paper aligns with the cell $( i , j )$ of the secret paper, for $1 \leq i , j \leq 3$ .
• The demon can only see the colour of the cell in your paper if the corresponding cell in the secret paper has a hole in it.
• The value on cell $( r , c )$ of the result grid is 1 if there is an odd number of black cells that it can see through the holes, or 0 otherwise.

You win the round if the result grid consists of only the values 1. Otherwise, the demon will give you the result grid as a feedback, and the round continues.

If you have spent all the coins and still did not win all the rounds, then you will be trapped forever. Escape the cursed game!

# Interaction

Each round is started with an odd integer $N$ $3 \leq N \leq 3 3$ ) which can be read through standard input.

Then, for each query that you give to the demon, you can output $N$ lines to standard output. Each of the $N$ lines consists of $N$ characters. The $c ^ { \mathrm { t h } }$ character of the $r ^ { \mathrm { { t h } } }$ line represents the colour of cell $( r , c )$ in your paper. The character should be 1 if $( r , c )$ is coloured black, or 0 if coloured white.

The demon will reply with one line consisting of a string, which can be read through standard input.

• If the string is CORRECT, then you win the current round and the next round (if exists) will immediately start.
• If the string is INCORRECT, then the demon will give you $N - 2$ more lines which can be read through standard input. Each of the $N - 2$ lines consists of $N - 2$ characters, representing the binary result grid as explained in the description.

The demon prepares the secret paper before the start of each round. In other words, the grader is NOT adaptive. There will be at least one hole in the secret paper.

The total number of queries across all 333 rounds should not exceed 999. If you exceed the maximum number of queries, you should terminate your program with 0 to receive the verdict Wrong Answer. If you do not terminate, the judging result would be undefined because your program is reading from a closed stream.

Do not forget to flush output buffers after each output. In C you can use fflush(stdout). In $\complement + +$ you can use fflush(stdout) or cout  flush. In Java you can use method flush for output stream, for example, System.out.flush(). In Python you can use stdout.flush().

# Sample Interaction #1

The following interaction only shows 2 rounds. The actual interaction stays until you win all 333 rounds or you run out of coins.

Standard Input	Standard Output

3

	111000111

INCORRECT0

	111111111

CORRECT

5

	1001110011000001101011011

INCORRECT111001111

	1101111011001001101111011

CORRECT

(start of round 3, con-tinue until 333 rounds)

![](images/c9281622a05b4a8ffdd68f3cca9b07b5c787e5e65db68caccb8b173d3d2caef9.jpg)

# Explanation for the sample interaction #1

For the first round, the following illustration shows how the demon finds the value in cell $( 1 , 1 )$ of the result grid for the first and second queries. The greycolored square represents the secret paper, and the circles represent the holes. In the first query, there are 4 black cells that can be seen through the holes, therefore the value in cell $( 1 , 1 )$ of the result grid is 0. In the second query, there are 5 black cells that can be seen through the holes, therefore the value in cell $( 1 , 1 )$ of the result grid is 1. As the result grid consists of only 1, the first round ends.

![](images/bbf692d89f507f0f9c44a4e7a04fe0d9b6163d9a8bf7094dfcddc6180217bd52.jpg)

For the second round, the following illustration shows how the demon find the value in cell $( 2 , 1 )$ of the result grid for the first query. Since there are 2 black cells that can be seen through the holes, the value in cell $( 2 , 1 )$ is 0.

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

using namespace std;

static long long queries = 0;
static long long query_limit = 999;
static const long long HARD_CAP = 20000;

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

    }
}

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

    log_metrics();

    int t = inf.readInt();

    int secret[40][40];
    int contestant[40][40];

    for (int round_idx = 0; round_idx < t; round_idx++) {

        int n = inf.readInt();

        for (int i = 0; i < 3; i++) {
            string row_s = inf.readToken();
            if ((int)row_s.size() != 3) {
                finish(_fail, "Invalid secret mask format in input file");
            }
            for (int j = 0; j < 3; j++) {
                if (row_s[j] == '1') secret[i][j] = 1;
                else secret[i][j] = 0;
            }
        }

        cout << n << endl;

        while (true) {
            queries++;

            log_metrics();

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

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    char c;

                    if (!(cin >> c)) {
                        finish(_wa, "Unexpected EOF (contestant output too short or stream closed)");
                    }
                    if (c == '0') contestant[i][j] = 0;
                    else if (c == '1') contestant[i][j] = 1;
                    else {
                        string msg = "Invalid character in grid output: '";
                        msg += c;
                        msg += "'";
                        finish(_wa, msg);
                    }
                }
            }

            bool win = true;
            vector<string> result_rows;
            int res_dim = n - 2;

            for (int r = 0; r < res_dim; r++) {
                string row_out = "";
                for (int c = 0; c < res_dim; c++) {

                    int bits = 0;
                    for (int si = 0; si < 3; si++) {
                        for (int sj = 0; sj < 3; sj++) {

                            if (contestant[r + si][c + sj] == 1 && secret[si][sj] == 1) {
                                bits++;
                            }
                        }
                    }

                    if (bits % 2 != 0) {
                        row_out += '1';
                    } else {
                        row_out += '0';
                        win = false;
                    }
                }
                result_rows.push_back(row_out);
            }

            if (win) {
                cout << "CORRECT" << endl;
                break;
            } else {
                cout << "INCORRECT" << endl;
                for (const string& s : result_rows) {
                    cout << s << endl;
                }
            }
        }
    }

    finish(_ok, "All rounds solved");
    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");

    string seed_str = (argc > 1 ? string(argv[1]) : "0");
    int strategy = 0;
    if (seed_str == "1") strategy = 1;
    else if (seed_str == "2") strategy = 2;
    else if (seed_str == "3") strategy = 3;
    else if (seed_str == "4") strategy = 4;
    else if (seed_str == "5") strategy = 5;

    int t = 333;
    cout << t << "\n";

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

        if (strategy == 1) {
            n = 3;
        } else if (strategy == 2) {
            n = 33;
        } else if (strategy == 3) {
            n = 3 + (i % 16) * 2;
        } else {

            n = 3 + rnd.next(16) * 2;
        }

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

            int mask;
            if (strategy == 4) {

                mask = (1 << rnd.next(9));
            } else if (strategy == 5) {

                if (rnd.next(5) == 0) mask = 511;
                else mask = 511 ^ (1 << rnd.next(9));
            } else {

                mask = rnd.next(1, 511);
            }

            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    if ((mask >> (r * 3 + c)) & 1) cout << '1';
                    else cout << '0';
                }
                cout << "\n";
            }
        } else {

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

    return 0;
}


All problems in this contest share the same statement, the only difference is the test your solution runs on. For further information on scoring please refer to "Scoring" section of the statement.

This is an interactive problem.

Imagine you are a treasure hunter, a very skillful one. One day you came across an ancient map which could help you to become rich. The map shows multiple forestry roads, and at each junction there is a treasure. So, you start your journey hoping to retrieve all the hidden treasures, but you don't know yet that there is a wicked wizard standing against you and craving to tangle up these roads and impede your achievements.

The treasure map is represented as an undirected graph in which vertices correspond to junctions and edges correspond to roads. Your path begins at a certain fixed vertex with a label known to you. Every time you come to a vertex that you have not been to before, you dig up a treasure chest and put a flag in this vertex. At the initial vertex you'll find a treasure chest immediately and, consequently, you'll put a flag there immediately as well.

When you are standing at the junction you can see for each of the adjacent vertices its degree and if there is a flag there. There are no other things you can see from there. Besides, the power of the wicked wizard is so great that he is able to change the location of the roads and junctions on the map without changing the graph structure. Therefore, the sequence of the roads coming from the junction $v$ might be different each time you come in the junction $v$. However, keep in mind that the set of adjacent crossroads does not change, and you are well aware of previously dug treasures at each adjacent to $v$ vertex.

Your goal is to collect treasures from all vertices of the graph as fast as you can. Good luck in hunting!

Interaction

On the first line the interactor prints an integer $t$ ($1 \leq t \leq 5$) — the number of maps for which you need to solve the problem.

Then for each of the $t$ maps interactor firstly prints the graph description and after that interaction on this map starts. The map description is given in the following format.

The first line contains four integers $n, m, start, base\_move\_count$ ($2 \leq n \leq 300$, $1 \leq m \leq min(\frac{n(n-1)}{2}, 25n)$, $1 \leq start \leq n$, $1000 \leq base\_move\_count \leq 30000$) — the number of vertices and edges in the graph, the label of the vertex where you start the journey and some base move count, on which the score for the map depends. It's guaranteed that the jury has a solution traversing the map using not more than $base\_move\_count$ moves with high probability.

The next $m$ lines contain descriptions of the edges of the graph $u, v$ ($1 \leq u, v \leq n$, $u \neq v$). It is guaranteed that the graph is connected, does not contain multiple edges and the degrees of all vertices do not exceed 50.

You can find map descriptions for all tests in the archive 'map\_descriptions.zip' which you can find in any of the archives 'problem-X-materials.zip' in the section "Contest materials" — they are all the same.

After that the interaction begins. The interactor prints vertex descriptions in the following format:

R~d~deg\_1~flag\_1~deg\_2~flag\_2 \ldots deg\_d~flag\_d, where $d$ is the degree of the current vertex, $deg_i$ is the degree of the $i$-th vertex adjacent to the current one, and $flag_i$ (0 or 1) is an indicator if the $i$-th adjacent vertex contains a flag. The order of neighbors in the vertex description is chosen by the interactor uniformly at random independently each time. Please keep in mind that you are not given the actual labels of adajacent vertices.

In response to the description of the vertex you should print a single integer $i$ ($1 \leq i \leq d$), which means that you have chosen the vertex with description $deg_i~flag_i$. Remember to use the flush operation after each output. If your output is invalid, you will get a "Wrong Answer" verdict.

When you have visited all the vertices of the graph at least once, the interactor prints the string "AC" instead of the vertex description. If you use more than $2 \cdot base\_move\_count$ moves, the interactor prints the string "F". In both cases, you have to either start reading the description of the next map, or terminate the program, if it has been the last map in the test.

Below the example of interaction is presented. Note that the test from the example is not the same that your solution will be tested on.

```
|  |  |
| --- | --- |
| Interactor | Solution |
| 1 |  |
| 3 3 1 1000 |  |
| 1 2 |  |
| 2 3 |  |
| 3 1 |  |
| R 2 2 0 2 0 |  |
|  | 1 |
| R 2 2 0 2 1 |  |
|  | 2 |
| R 2 2 0 2 1 |  |
|  | 1 |
| AC |  |
```

Scoring

All problems of this contest share the same statement and differ only in the test. Each problem contains one test. Map descriptions for the test are available to look at in the archive.

Each test consists of several maps.

The solution score for the problem will be $0$ in case your solution for some map failed to get an answer "AC" or "F". If the solution correctly interacted with the interactor on all $t$ maps, the score for the task will be equal to the sum of the scores for each of the maps.

If you have successfully passed all vertices of a graph with $n$ vertices using $moves$ moves, the score for the map is calculated as follows. Denote

base\\_fraction=\frac{base\\_move\\_count + 1}{n},sol\\_fraction=\frac{moves+1}{n},c=\frac{90}{\sqrt{base\\_fraction - 1}}.

Then:

* if $moves \leq base\_move\_count$, you get $100-c \sqrt{sol\_fraction - 1}$ points.
* if $base\_move\_count < moves \leq 2 \cdot base\_move\_count$, you get $20 - \frac{10\cdot(moves + 1)}{base\_move\_count + 1}$ points.

If you use more than $2 \cdot base\_move\_count$ moves, you get 0 points for the map.

For each problem the solution with the highest score is chosen. Please note that the maximum is chosen for the entire test as a whole, not for each separate map.

The final result of the participant is the sum of points for each of the problems.

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

static long long queries = 0;
static long long query_limit = 0;
static std::mt19937 rng(1337);

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

struct MapCase {
    int n, m, start, bmc;
    vector<pair<int,int>> edges;
};

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

    int T = inf.readInt();
    vector<MapCase> maps(T);
    long long total_limit = 0;

    for(int i = 0; i < T; ++i) {
        maps[i].n = inf.readInt();
        maps[i].m = inf.readInt();
        maps[i].start = inf.readInt();
        maps[i].bmc = inf.readInt();

        total_limit += (2LL * maps[i].bmc);
        for(int j = 0; j < maps[i].m; ++j) {
            int u = inf.readInt();
            int v = inf.readInt();
            maps[i].edges.push_back({u, v});
        }
    }

    query_limit = total_limit;
    log_metrics();

    cout << T << endl;

    for(int i = 0; i < T; ++i) {
        const auto& map_data = maps[i];
        int n = map_data.n;
        int m = map_data.m;
        int start_node = map_data.start;
        int bmc = map_data.bmc;

        vector<vector<int>> adj(n + 1);
        vector<int> deg(n + 1, 0);
        for(const auto& e : map_data.edges) {
            adj[e.first].push_back(e.second);
            adj[e.second].push_back(e.first);
            deg[e.first]++;
            deg[e.second]++;
        }

        cout << n << " " << m << " " << start_node << " " << bmc << endl;
        for(const auto& e : map_data.edges) {
            cout << e.first << " " << e.second << endl;
        }
        cout.flush();

        vector<bool> visited(n + 1, false);
        int visited_count = 0;
        int curr = start_node;

        if(!visited[curr]) {
            visited[curr] = true;
            visited_count++;
        }

        long long moves = 0;
        long long map_limit = 2LL * bmc;

        while(true) {

            if (visited_count == n) {
                cout << "AC" << endl;
                break;
            }

            if (moves > map_limit) {
                cout << "F" << endl;
                break;
            }

            int d = deg[curr];
            vector<int> neighbors = adj[curr];

            shuffle(neighbors.begin(), neighbors.end(), rng);

            cout << "R " << d;
            for(int v : neighbors) {
                cout << " " << deg[v] << " " << (visited[v] ? 1 : 0);
            }
            cout << endl;

            int choice;
            if (!(cin >> choice)) {
                finish(_wa, "Unexpected EOF while reading move");
            }

            if (choice < 1 || choice > d) {
                finish(_wa, "Invalid move index " + to_string(choice) + " (degree is " + to_string(d) + ")");
            }

            queries++;

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

            curr = neighbors[choice - 1];
            if (!visited[curr]) {
                visited[curr] = true;
                visited_count++;
            }
            moves++;
        }
    }

    finish(_ok, "All maps processed");
    return 0;
}

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

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

static mt19937 rng(4242);

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 t_cases = inf.readInt();

    cout << t_cases << endl;

    for (int t = 0; t < t_cases; ++t) {

        int n = inf.readInt();
        int m = inf.readInt();
        int start_node = inf.readInt();
        int base_move_count = inf.readInt();

        long long map_limit = 2LL * base_move_count;
        query_limit += map_limit;

        vector<vector<int>> adj(n + 1);
        vector<int> deg(n + 1, 0);
        vector<pair<int, int>> edges;
        edges.reserve(m);

        for (int i = 0; i < m; ++i) {
            int u = inf.readInt();
            int v = inf.readInt();
            edges.push_back({u, v});
            adj[u].push_back(v);
            adj[v].push_back(u);
            deg[u]++;
            deg[v]++;
        }

        cout << n << " " << m << " " << start_node << " " << base_move_count << endl;
        for (const auto& e : edges) {
            cout << e.first << " " << e.second << endl;
        }
        cout.flush();

        if (t == 0) log_metrics();

        int curr = start_node;
        vector<bool> visited(n + 1, false);
        visited[curr] = true;
        int visited_count = 1;
        long long moves_this_map = 0;

        while (true) {

            if (visited_count == n) {
                cout << "AC" << endl;
                break;
            }

            if (moves_this_map > map_limit) {
                cout << "F" << endl;
                break;
            }

            struct NeighborOpt {
                int real_u;
                int d;
                int f;
            };

            vector<NeighborOpt> options;
            options.reserve(adj[curr].size());
            for (int v : adj[curr]) {
                options.push_back({v, deg[v], visited[v] ? 1 : 0});
            }

            shuffle(options.begin(), options.end(), rng);

            cout << "R " << options.size();
            for (const auto& opt : options) {
                cout << " " << opt.d << " " << opt.f;
            }
            cout << endl;

            int choice_idx;
            if (!(cin >> choice_idx)) {
                finish(_pe, "Unexpected EOF or invalid format reading choice");
            }

            if (choice_idx < 1 || choice_idx > (int)options.size()) {
                finish(_wa, format("Invalid choice index %d, expected 1..%d", choice_idx, (int)options.size()));
            }

            int next_v = options[choice_idx - 1].real_u;

            curr = next_v;
            if (!visited[curr]) {
                visited[curr] = true;
                visited_count++;
            }
            moves_this_map++;
            queries++;

            if (queries % 200 == 0) log_metrics();

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

    finish(_ok, "All maps processed");
    return 0;
}

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

typedef pair<int,int> Edge;

bool hasEdge(int u, int v, const set<Edge>& edges) {
    if (u > v) swap(u, v);
    return edges.count({u, v});
}

bool tryAddEdge(int u, int v, set<Edge>& edges, vector<int>& deg) {
    if (u == v) return false;
    if (u > v) swap(u, v);
    if (edges.count({u, v})) return false;

    if (deg[u] >= 50 || deg[v] >= 50) return false;

    edges.insert({u, v});
    deg[u]++;
    deg[v]++;
    return true;
}

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

    string mode = opt<string>("mode", "non");
    long long seedVal = atoll(argv[1]);

    int T = 1;
    cout << T << endl;

    int n = 0;
    int start_node = 1;
    vector<Edge> final_edges;

    if (seedVal >= 1 && seedVal <= 5) {
        if (seedVal == 1) {
            n = 5;
            for(int i=1; i<n; ++i) final_edges.push_back({i, i+1});
        } else if (seedVal == 2) {
            n = 6;
            for(int i=2; i<=n; ++i) final_edges.push_back({1, i});
        } else if (seedVal == 3) {
            n = 10;
            for(int i=1; i<n; ++i) final_edges.push_back({i, i+1});
            final_edges.push_back({n, 1});
        } else if (seedVal == 4) {
            n = 7;
            for(int i=1; i<=n; ++i)
                for(int j=i+1; j<=n; ++j) final_edges.push_back({i, j});
        } else {
            n = 15;
            for(int i=2; i<=n; ++i) final_edges.push_back({i/2, i});
        }
        start_node = 1;
    } else {

        n = rnd.next(20, 300);

        enum Type { RANDOM, TREE, CATERPILLAR, REGULAR, GRID, DENSE_CORE };
        Type type;

        if (mode == "adp") {

            int r = rnd.next(1, 100);
            if (r <= 50) type = REGULAR;
            else if (r <= 80) type = GRID;
            else type = DENSE_CORE;
        } else {

            int r = rnd.next(1, 100);
            if (r <= 40) type = RANDOM;
            else if (r <= 70) type = TREE;
            else type = CATERPILLAR;
        }

        set<Edge> edges;
        vector<int> deg(n + 1, 0);

        if (type == RANDOM) {

            for (int i = 2; i <= n; ++i) {
                tryAddEdge(i, rnd.next(1, i - 1), edges, deg);
            }

            int limit = min((long long)25 * n, (long long)n * (n - 1) / 2);
            int extra = rnd.next(0, min(5 * n, limit - (int)edges.size()));
            for (int i = 0; i < extra; ++i) {
                tryAddEdge(rnd.next(1, n), rnd.next(1, n), edges, deg);
            }
        } else if (type == TREE) {
            for (int i = 2; i <= n; ++i) {
                int p = rnd.next(1, i - 1);

                int tries = 0;
                while (deg[p] >= 50 && tries < 50) {
                    p = rnd.next(1, i - 1);
                    tries++;
                }
                if (deg[p] < 50) tryAddEdge(i, p, edges, deg);
                else {

                    for(int k=1; k<i; ++k) if(deg[k] < 50) { tryAddEdge(i, k, edges, deg); break; }
                }
            }
        } else if (type == CATERPILLAR) {

            int len = rnd.next(n / 3, n - 2);
            for (int i = 1; i < len; ++i) tryAddEdge(i, i + 1, edges, deg);

            for (int i = len + 1; i <= n; ++i) {
                int p = rnd.next(1, len);
                if (deg[p] < 50) tryAddEdge(i, p, edges, deg);
                else {

                    for(int k=1; k<=len; ++k) if(deg[k] < 50) { tryAddEdge(i, k, edges, deg); break; }
                }
            }
        } else if (type == REGULAR) {

            int k = rnd.next(3, 10);
            if (k % 2 != 0 && n % 2 != 0) k++;

            vector<int> offsets;
            for (int i = 1; i <= k / 2; ++i) offsets.push_back(i);
            if (k % 2 != 0) offsets.push_back(n / 2);

            for (int i = 1; i <= n; ++i) {
                for (int off : offsets) {
                    int j = (i + off - 1) % n + 1;
                    tryAddEdge(i, j, edges, deg);
                }
            }

            vector<Edge> temp(edges.begin(), edges.end());
            int swaps = n * 5;
            for (int i = 0; i < swaps && temp.size() > 2; ++i) {
                int idx1 = rnd.next(0, (int)temp.size() - 1);
                int idx2 = rnd.next(0, (int)temp.size() - 1);
                if (idx1 == idx2) continue;
                int u1 = temp[idx1].first, v1 = temp[idx1].second;
                int u2 = temp[idx2].first, v2 = temp[idx2].second;

                if (deg[u1] > 50 || deg[u2] > 50 || deg[v1] > 50 || deg[v2] > 50) continue;

                if (hasEdge(u1, u2, edges) || hasEdge(v1, v2, edges)) continue;
                if (u1 == u2 || v1 == v2) continue;
                set<int> distinct = {u1, v1, u2, v2};
                if (distinct.size() != 4) continue;

                edges.erase({min(u1, v1), max(u1, v1)});
                edges.erase({min(u2, v2), max(u2, v2)});
                edges.insert({min(u1, u2), max(u1, u2)});
                edges.insert({min(v1, v2), max(v1, v2)});
                temp[idx1] = {min(u1, u2), max(u1, u2)};
                temp[idx2] = {min(v1, v2), max(v1, v2)};
            }
        } else if (type == GRID) {
            int w = (int)sqrt(n);
            if (w < 2) w = 2;
            int h = n / w;
            auto idx = [&](int r, int c) { return (r - 1) * w + c; };
            for (int r = 1; r <= h; ++r) {
                for (int c = 1; c <= w; ++c) {
                    int u = idx(r, c);
                    if (c < w) tryAddEdge(u, idx(r, c + 1), edges, deg);
                    if (r < h) tryAddEdge(u, idx(r + 1, c), edges, deg);
                }
            }

            int used = h * w;
            for (int i = used + 1; i <= n; ++i) {
                int p = rnd.next(1, used);
                if (deg[p] < 4) tryAddEdge(i, p, edges, deg);
                else {
                    for(int k=1; k<=used; ++k) if(deg[k]<50) { tryAddEdge(i, k, edges, deg); break; }
                }
                used++;
            }
        } else {

            for (int i = 1; i < n; ++i) tryAddEdge(i, i + 1, edges, deg);
            tryAddEdge(n, 1, edges, deg);
            int target_m = min((long long)n * 4, (long long)25 * n);
            int fails = 0;
            while ((int)edges.size() < target_m && fails < 200) {
                if (!tryAddEdge(rnd.next(1, n), rnd.next(1, n), edges, deg)) fails++;
            }
        }

        vector<int> p(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());

        for (auto e : edges) {
            final_edges.push_back({p[e.first - 1], p[e.second - 1]});
        }

        start_node = rnd.next(1, n);
    }

    if (final_edges.empty()) {

        final_edges.push_back({1, 2});
        if (n < 2) n = 2;
    }

    int m = final_edges.size();

    long long bmc_calc = 1000 + 4LL * n + m / 2;
    int bmc = (int)max(1000LL, min(30000LL, bmc_calc));

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

    cout << n << " " << m << " " << start_node << " " << bmc << endl;
    for (const auto& e : final_edges) {
        cout << e.first << " " << e.second << endl;
    }

    return 0;
}


This is an interactive problem.
Petya participates in such contest that the participants only learn results of judging their submissions after the end of the contest.
There are  $n$  problems numbered from 1 to  $n$  in the contest. For each problem he has made a submission. After the contest he was very eager to learn his points and his place. But the results would only be published at the closing ceremony, which is a few hours away.
Petya decided to find out his result before that: to get his points for each problem from the jury, he would ask weird questions. Let Petya's points be  $c_{1}, c_{2}, \ldots, c_{n}$  for problems  $1, 2, \ldots, n$ , respectively. Petya doesn't know these numbers and wants to find them out.
Petya chooses three distinct numbers of the problems and asks the jury to tell him the sum of the minimum and the maximum points he received for these three problems. Fortunately, the jury answers such queries. Formally, if he queries about  $i$ ,  $j$ ,  $k$ , he gets  $\min(c_i, c_j, c_k) + \max(c_i, c_j, c_k)$ .
Petya still doesn't know how to find out all his points. Help him find out  $c_{1}, c_{2}, \ldots, c_{n}$ , by making no more than  $4n$  queries to the jury.
# Interaction Protocol
First, your program must read a single integer  $n -$  the number of the problems in the contest (  $5 \leq n \leq 1000$  ).
Next, your program can make queries about the sum of minimum and maximum points among problems  $i$ ,  $j$ ,  $k$ . In order to do that, output a separate line «? i j k» ( $1 \leq i,j,k \leq n$ ,  $i \neq j$ ,  $j \neq k$ ,  $i \neq k$ ). As the answer to this query, you will get a single integer  $\min(c_i,c_j,c_k) + \max(c_i,c_j,c_k)$  in the input.
If your program makes more than  $4n$  queries or asks an invalid query, you get «Wrong Answer».
After finding Petya's points output  $\ll !c_{1}c_{2}\ldots c_{n}\gg$  - the points for each problem.
It is guaranteed that Petya's points for each problem are integer and satisfy  $0 \leq c_{i} \leq 10^{9}$  for all  $1 \leq i \leq n$ . Also, all numbers  $c_{i}$  are fixed in advance and won't change during the testing of your program.
# Example

standard input	standard output

5	? 1 2 3

2	? 1 3 5

4	? 5 4 3

4	? 1 4 2

1	! 1 0 2 1 3

# Note
Print new line character after each line. After each action your program must flush standard output. If you use "writeln" in Pascal, "cout << ... << endl" in C++, "System.out.println" in Java, "print" in Python, "Console.WriteLine" in C#, standard output is automatically flushed, no additional action is required. If you are using other way to output data, you should flush standard output. Note that even if you flush output, you absolutely must print new line character.
# Explanation
In this sample, there are 5 problems, and Petya got 1, 0, 2, 1, 3 points for the problems. If he queries about problems 1, 2, 3, his minimum points among them is 0 (for the 2-nd problem), and his maximum is 2 (for the 3-rd problem). Thus, the jury will answer  $2 = 0 + 2$  to that query.
Page 1 of 1

#include "testlib.h"
#include <csignal>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

static int queries = 0;
static long long query_limit = -1;

void log_queries() {
    tout << "queries=" << queries << "\n";
    tout << "query_limit=" << query_limit << "\n";
}

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

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);
    signal(SIGPIPE, SIG_IGN);
    atexit(log_queries);

    const int n = inf.readInt();
    vector<long long> c(n + 1);
    bool has_all = true;
    for (int i = 1; i <= n; i++) {
        if (inf.seekEof()) {
            has_all = false;
            break;
        }
        c[i] = inf.readLong();
    }
    if (!has_all) {

        for (int i = 1; i <= n; i++) {
            c[i] = i;
        }
    }

    query_limit = 4LL * n;
    const long long hard_cap = query_limit + 10;

    cout << n << "\n" << flush;

    while (true) {
        string cmd;
        if (!(cin >> cmd)) {
            quit_with(_pe, "Unexpected EOF");
        }

        if (cmd == "?") {
            queries++;
            if (queries > hard_cap) {
                quit_with(_pe, "Too many queries");
            }

            int i, j, k;
            if (!(cin >> i >> j >> k)) {
                quit_with(_pe, "Invalid query");
            }
            if (!(1 <= i && i <= n && 1 <= j && j <= n && 1 <= k && k <= n)) {
                quit_with(_pe, "Query index out of range");
            }
            if (i == j || j == k || i == k) {
                quit_with(_pe, "Query indices must be distinct");
            }

            long long mn = min(c[i], min(c[j], c[k]));
            long long mx = max(c[i], max(c[j], c[k]));
            cout << (mn + mx) << "\n" << flush;

        } else if (cmd == "!") {
            vector<long long> out(n + 1);
            for (int i = 1; i <= n; i++) {
                if (!(cin >> out[i])) {
                    quit_with(_pe, "Missing answer values");
                }
            }
            for (int i = 1; i <= n; i++) {
                if (out[i] != c[i]) {
                    quit_with(_wa, "Wrong answer");
                }
            }
            quit_with(_ok, "Correct");

        } else {
            quit_with(_pe, "Unknown command: " + cmd);
        }
    }
}

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

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

    const long long seed = (argc >= 2) ? atoll(argv[1]) : 1;

    int n = 1000;
    if (seed == 1) n = 5;
    else if (seed == 2) n = 6;
    else if (seed <= 10) n = 50 + (int)seed;
    n = max(5, min(1000, n));

    vector<long long> c(n + 1, 0);
    int mode = (int)(seed % 5);
    if (mode == 0) {

        long long base = 1000000000LL - n;
        for (int i = 1; i <= n; i++) c[i] = base + i;
    } else if (mode == 1) {

        for (int i = 1; i <= n; i++) {
            if (i % 37 == 0) c[i] = 1000000000LL;
            else if (i % 41 == 0) c[i] = 0;
            else c[i] = (i % 7) * 12345LL;
        }
    } else if (mode == 2) {

        for (int i = 1; i <= n; i++) c[i] = rnd.next(0LL, 1000000000LL);
    } else if (mode == 3) {

        vector<int> perm = rnd.perm(n, 1);
        for (int i = 1; i <= n; i++) c[i] = (long long)perm[i - 1] * 1000000LL;
    } else {

        for (int i = 1; i <= n; i++) c[i] = (i <= n / 2) ? 0 : 1000000000LL;
    }

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


This is the easy version of the problem. In this version, you can ask at most $n+69$ questions.

This is an interactive problem.

It is a tradition in Mexico's national IOI trainings to play the game "Asesino", which is similar to "Among Us" or "Mafia".

Today, $n$ players, numbered from $1$ to $n$, will play "Asesino" with the following three roles:

* Knight: a Knight is someone who always tells the truth.
* Knave: a Knave is someone who always lies.
* Impostor: an Impostor is someone everybody thinks is a Knight, but is secretly a Knave.

Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves.

As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor.

To determine the Impostor, you will ask some questions. In each question, you will pick two players $i$ and $j$ ($1 \leq i, j \leq n$; $i \neq j$) and ask if player $i$ thinks that player $j$ is a Knight. The results of the question is shown in the table below.

|  |  |  |  |
| --- | --- | --- | --- |
|  | Knight | Knave | Impostor |
| Knight | Yes | No | Yes |
| Knave | No | Yes | No |
| Impostor | No | Yes | — |

The response of the cell in row $a$ and column $b$ is the result of asking a question when $i$ has role $a$ and $j$ has row $b$. For example, the "Yes" in the top right cell belongs to row "Knight" and column "Impostor", so it is the response when $i$ is a Knight and $j$ is an Impostor.

Find the Impostor in at most $n + 69$ questions.

Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem.

Input

The first line of input contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($3 \le n \le 10^5$) — the number of people playing the game.

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

Interaction

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

* "? i j" ($1 \leq i,j \leq n$; $i \neq j$) — to ask player $i$ if they think player $j$ is a Knight.

The jury will output a "1" if player $i$ thinks player $j$ is a Knight, and "0" otherwise.

When you have determined which player the Impostor is, output a line in the following format:

* "! i" ($1 \leq i \leq n$) — the Impostor is player $i$.

Note that answering does not count to your limit of $n+69$ questions.

If you have made an invalid output, used more than $n+69$ questions or wrongly determined the Impostor, the jury will respond with "-1" and you will receive a Wrong Answer verdict. Upon receiving "-1", your program must terminate immediately. Otherwise, you may receive an arbitrary verdict because your solution might be reading from a closed stream.

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;
* sys.stdout.flush() in Python;
* std::io::stdout().flush() in Rust;
* see the documentation for other languages.

Example

Input

```
2
7

1

0

0

1

1

0

0

1

4

0

1

1

1
```

Output

```
? 1 3

? 7 6

? 2 5

? 6 2

? 4 5

? 4 6

? 1 4

? 2 4

! 4

? 1 2

? 2 3

? 3 4

? 4 1

! 3
```

Note

Note that the example test cases do not represent an optimal strategy for asking questions and are only shown for the sake of demonstrating the interaction format. Specifically, we cannot determine which player is the Impostor from the questions asked in the examples.

In the first test case of the example, players at indices $2$ and $6$ are Knights, players at indices $1$, $3$, $5$, and $7$ are Knaves, and the Impostor is at index $4$. The following is an explanation of the questions asked:

* In the first query, player $i$ is a Knave and player $j$ is a Knave. The answer is "yes" since Knaves always lie.
* In the second query, player $i$ is a Knave and player $j$ is a Knight. The answer is "no" since Knaves always lie.
* In the third query, player $i$ is a Knight and player $j$ is a Knave. The answer is "no" since Knights always tell the truth.
* In the fourth query, player $i$ is a Knight and player $j$ is a Knight. The answer is "yes" since Knights always tell the truth.
* In the fifth query, player $i$ is a Impostor and player $j$ is a Knave. The answer is "yes" since the Impostor always lies.
* In the sixth query, player $i$ is a Impostor and player $j$ is a Knight. The answer is "no" since the Impostor always lies.
* In the seventh query, player $i$ is a Knave and player $j$ is a Impostor. The answer is "no" since Knaves always lie and Knaves thinks that the Impostor is a Knight.
* In the eighth query, player $i$ is a Knight and player $j$ is a Impostor. The answer is "yes" since Knights always tell the truth and Knights think that the Impostor is a Knight.

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

const int KNIGHT = 0;
const int KNAVE = 1;
const int IMPOSTOR = 2;

static long long queries = 0;
static long long query_limit = 0;
static long long hard_cap = 200000;

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

bool is_number(const string& s) {
    if (s.empty()) return false;
    for (char c : s) {
        if (!isdigit(c)) return false;
    }
    return true;
}

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

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

    for (int case_idx = 0; case_idx < t; ++case_idx) {

        int n = inf.readInt();
        vector<int> roles(n + 1);
        int impostor_idx = -1;
        for (int i = 1; i <= n; ++i) {
            roles[i] = inf.readInt();
            if (roles[i] == IMPOSTOR) impostor_idx = i;
        }

        queries = 0;
        query_limit = n + 69;
        hard_cap = max((long long)200000, 10 * query_limit);

        log_metrics();

        cout << n << endl;

        bool active = true;
        while (active) {
            string token;

            if (!(cin >> token)) {
                finish(_pe, "Unexpected EOF reading token");
            }

            bool is_ans = false;
            int u = -1, v = -1;

            if (token == "!") {
                is_ans = true;
                if (!(cin >> u)) finish(_pe, "Unexpected EOF reading answer index");
            } else if (token == "?") {
                if (!(cin >> u >> v)) finish(_pe, "Unexpected EOF reading query indices");
            } else {

                if (!is_number(token)) {
                    cout << "-1" << endl;
                    finish(_pe, format("Invalid token: '%s' (expected '?', '!', or number)", token.c_str()));
                }
                try {
                    u = stoi(token);
                } catch (...) {
                    cout << "-1" << endl;
                    finish(_pe, "Token integer overflow");
                }
                if (!(cin >> v)) finish(_pe, "Unexpected EOF reading second index");
            }

            if (is_ans) {
                if (u == impostor_idx) {

                    active = false;
                } else {

                    cout << "-1" << endl;
                    finish(_wa, format("Wrong answer: guessed %d, expected %d", u, impostor_idx));
                }
            } else {

                if (u < 1 || u > n || v < 1 || v > n || u == v) {
                    cout << "-1" << endl;
                    finish(_wa, format("Invalid query indices: %d %d (N=%d)", u, v, n));
                }

                queries++;
                if (queries > hard_cap) {
                    finish(_pe, "Hard query limit exceeded");
                }

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

                bool asker_truthful = (roles[u] == KNIGHT);
                bool target_is_knight_like = (roles[v] != KNAVE);

                bool response = (asker_truthful == target_is_knight_like);

                cout << (response ? 1 : 0) << endl;
            }
        }
    }

    finish(_ok, "Passed all test cases");
    return 0;
}

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

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

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

    }
}

static void finish(TResult verdict, const string& msg) {

    if (verdict != _ok) {
        cout << "-1" << endl;
    }
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

const int TYPE_TRUE = 1;
const int TYPE_FALSE = 0;

struct State {
    int n;
    int imp;
    vector<int> roles;

    int get_type(int p) const {
        if (p == imp) return TYPE_FALSE;
        return roles[p];
    }

    int get_perc(int p) const {
        if (p == imp) return TYPE_TRUE;
        return roles[p];
    }

    int query(int i, int j) const {
        return (get_type(i) == get_perc(j)) ? 1 : 0;
    }
};

struct HistoryItem {
    int i, j, ans;
};
vector<HistoryItem> history;

vector<int> dsu_parent;
vector<int> dsu_xor;
vector<int> dsu_fixed;

void dsu_reset(int n) {
    if ((int)dsu_parent.size() <= n) {
        dsu_parent.resize(n + 1);
        dsu_xor.resize(n + 1);
        dsu_fixed.resize(n + 1);
    }
    for(int i=1; i<=n; ++i) {
        dsu_parent[i] = i;
        dsu_xor[i] = 0;
        dsu_fixed[i] = -1;
    }
}

pair<int, int> dsu_find(int i) {
    if (dsu_parent[i] == i) return {i, 0};
    pair<int, int> root = dsu_find(dsu_parent[i]);
    dsu_parent[i] = root.first;
    dsu_xor[i] = dsu_xor[i] ^ root.second;
    return {dsu_parent[i], dsu_xor[i]};
}

bool dsu_unite(int i, int j, int w) {
    auto r1 = dsu_find(i);
    auto r2 = dsu_find(j);
    int root1 = r1.first;
    int root2 = r2.first;
    int p1 = r1.second;
    int p2 = r2.second;

    int required = p1 ^ p2 ^ w;

    if (root1 != root2) {

        dsu_parent[root1] = root2;
        dsu_xor[root1] = required;

        if (dsu_fixed[root1] != -1) {
            int val_r2_implied = dsu_fixed[root1] ^ required;
            if (dsu_fixed[root2] != -1 && dsu_fixed[root2] != val_r2_implied) return false;
            dsu_fixed[root2] = val_r2_implied;
        }
        return true;
    } else {
        return (required == 0);
    }
}

bool dsu_fix(int i, int val) {
    auto r = dsu_find(i);
    int root = r.first;
    int current_xor = r.second;

    int required_root = val ^ current_xor;

    if (dsu_fixed[root] != -1 && dsu_fixed[root] != required_root) return false;
    dsu_fixed[root] = required_root;
    return true;
}

bool is_consistent(int n, int cand, const vector<HistoryItem>& hist) {
    dsu_reset(n);

    for (const auto& q : hist) {
        int i = q.i;
        int j = q.j;
        int ans = q.ans;

        if (i == cand) {

            if (!dsu_fix(j, 1 - ans)) return false;
        } else if (j == cand) {

            if (!dsu_fix(i, ans)) return false;
        } else {

            if (!dsu_unite(i, j, 1 - ans)) return false;
        }
    }
    return true;
}

struct TestCaseData {
    int n;
    bool has_roles;
    vector<int> roles;
    int imp_idx;
};

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

    vector<int> buffer;
    while (!inf.seekEof()) {
        buffer.push_back(inf.readInt());
    }

    int buf_pos = 0;
    int t = buffer[buf_pos++];
    vector<TestCaseData> cases;

    for (int k = 0; k < t; ++k) {
        TestCaseData tc;
        tc.n = buffer[buf_pos++];
        tc.has_roles = false;

        if (buf_pos < (int)buffer.size() && buffer[buf_pos] < 3) {
            tc.has_roles = true;
            tc.roles.resize(tc.n + 1);
            for (int i = 1; i <= tc.n; ++i) {
                tc.roles[i] = buffer[buf_pos++];
                if (tc.roles[i] == 2) tc.imp_idx = i;
            }
        }
        cases.push_back(tc);
    }

    cout << t << endl;

    for (int k = 0; k < t; ++k) {
        TestCaseData& tc = cases[k];

        queries = 0;
        query_limit = tc.n + 69;
        log_metrics();

        State current_state;
        current_state.n = tc.n;
        current_state.roles.assign(tc.n + 1, 0);

        if (tc.has_roles) {

            current_state.imp = tc.imp_idx;
            for(int i=1; i<=tc.n; ++i) {
                if (i != tc.imp_idx) {

                    if (tc.roles[i] == 0) current_state.roles[i] = TYPE_TRUE;
                    else current_state.roles[i] = TYPE_FALSE;
                }
            }
        } else {

            current_state.imp = rnd.next(1, tc.n);
            for(int i=1; i<=tc.n; ++i) {
                if (i == current_state.imp) continue;
                current_state.roles[i] = rnd.next(0, 1);
            }
        }

        cout << tc.n << endl;

        history.clear();
        bool active = true;

        while (active) {
            string type;
            if (!(cin >> type)) {

                finish(_wa, "Unexpected EOF (contestant output ended)");
            }

            if (type == "?") {
                int u, v;
                if (!(cin >> u >> v)) finish(_wa, "Invalid query format");

                if (u < 1 || u > tc.n || v < 1 || v > tc.n || u == v) {
                    finish(_wa, "Invalid query range or u==v");
                }

                queries++;

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

                if (queries > tc.n + 200) {
                    finish(_pe, "Too many queries (hard cap exceeded)");
                }

                int ans = current_state.query(u, v);
                history.push_back({u, v, ans});

                cout << ans << endl;

            } else if (type == "!") {
                int u;
                if (!(cin >> u)) finish(_wa, "Invalid answer format");

                if (u < 1 || u > tc.n) {
                    finish(_wa, "Invalid answer range");
                }

                bool correct = false;

                if (u == current_state.imp) {

                    if (!tc.has_roles) {

                        vector<int> candidates(tc.n);
                        iota(candidates.begin(), candidates.end(), 1);
                        shuffle(candidates.begin(), candidates.end());

                        bool found_alternative = false;
                        int checks = 0;
                        int max_checks = min(tc.n, 300);

                        for (int y : candidates) {
                            if (y == u) continue;
                            if (checks++ >= max_checks) break;

                            if (is_consistent(tc.n, y, history)) {
                                found_alternative = true;
                                break;
                            }
                        }

                        if (found_alternative) {

                            correct = false;
                        } else {

                            correct = true;
                        }
                    } else {

                        correct = true;
                    }
                } else {

                    correct = false;
                }

                if (correct) {

                    active = false;
                } else {
                    finish(_wa, "Wrong Answer: Impostor incorrect or ambiguous");
                }

            } else {
                finish(_wa, "Invalid query type");
            }
        }
    }

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

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

const int KNIGHT = 0;
const int KNAVE = 1;
const int IMPOSTOR = 2;

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

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

    const int MAX_SUM_N = 100000;
    const int MIN_N = 3;
    const int MAX_T = 1000;

    vector<int> ns;
    int rem = MAX_SUM_N;

    int strategy = rnd.next(4);

    while (rem >= MIN_N && (int)ns.size() < MAX_T) {

        if ((int)ns.size() == MAX_T - 1) {
            ns.push_back(rem);
            rem = 0;
            break;
        }

        int n_limit = rem;
        if (strategy == 0) n_limit = 20;
        else if (strategy == 1) n_limit = 1000;
        else if (strategy == 3) {
             ns.push_back(rem);
             rem = 0;
             break;
        }

        int upper = min(rem, n_limit);

        if (upper < MIN_N) {
            ns.push_back(rem);
            rem = 0;
            break;
        }

        int n = rnd.next(MIN_N, upper);

        if (rem - n < MIN_N && rem - n != 0) {
            n += (rem - n);
        }

        ns.push_back(n);
        rem -= n;
    }

    if (rem > 0 && !ns.empty()) {
        ns.back() += rem;
        rem = 0;
    }

    println(ns.size());

    for (int n : ns) {
        println(n);

        if (mode == "non") {

            vector<int> roles(n);

            int imp = rnd.next(n);
            roles[imp] = IMPOSTOR;

            int subtype = rnd.next(5);

            for (int i = 0; i < n; i++) {
                if (i == imp) continue;

                if (subtype == 0) roles[i] = KNIGHT;
                else if (subtype == 1) roles[i] = KNAVE;
                else if (subtype == 2) roles[i] = rnd.next(0, 1);
                else if (subtype == 3) roles[i] = rnd.wnext(2, -2);
                else roles[i] = rnd.wnext(2, 2);
            }

            println(roles);
        }

    }

    return 0;
}