Problem: cf1370_f1
Difficulty: easy
Categories: graph, search
Time limit: 2000 ms, memory limit: 256 MB


This is an interactive problem.

You are given a tree consisting of $n$ nodes numbered with integers from $1$ to $n$. Ayush and Ashish chose two secret distinct nodes in the tree. You need to find out both the nodes. You can make the following query:

* Provide a list of nodes and you will receive a node from that list whose sum of distances to both the hidden nodes is minimal (if there are multiple such nodes in the list, you will receive any one of them). You will also get the sum of distances of that node to the hidden nodes.

Recall that a tree is a connected graph without cycles. The distance between two nodes is defined as the number of edges in the simple path between them.

More formally, let's define two hidden nodes as $s$ and $f$. In one query you can provide the set of nodes $\{a_1, a_2, \ldots, a_c\}$ of the tree. As a result, you will get two numbers $a_i$ and $dist(a_i, s) + dist(a_i, f)$. The node $a_i$ is any node from the provided set, for which the number $dist(a_i, s) + dist(a_i, f)$ is minimal.

You can ask no more than $14$ queries.

Input

The first line contains a single integer $t$ $(1 \leq t \leq 10)$ — the number of test cases. Please note, how the interaction process is organized.

The first line of each test case consists of a single integer $n$ $(2 \le n \le 1000)$ — the number of nodes in the tree.

The next $n - 1$ lines consist of two integers $u$, $v$ $(1 \le u, v \le n, u \ne v)$ — the edges of the tree.

Interaction

To ask a query print a single line:

* In the beginning print "? c " (without quotes) where $c$ $(1 \leq c \leq n)$ denotes the number of nodes being queried, followed by $c$ distinct integers in the range $[1, n]$  — the indices of nodes from the list.

For each query, you will receive two integers $x$, $d$ — the node (among the queried nodes) with the minimum sum of distances to the hidden nodes and the sum of distances from that node to the hidden nodes. If the subset of nodes queried is invalid or you exceeded the number of queries then you will get $x = d = -1$. In this case, you should terminate the program immediately.

When you have guessed the hidden nodes, print a single line "! " (without quotes), followed by two integers in the range $[1, n]$  — the hidden nodes. You can output the hidden nodes in any order.

After this, you should read a string. If you guess the nodes correctly, you will receive the string "Correct". In this case, you should continue solving the remaining test cases or terminate the program, if all test cases were solved. Otherwise, you will receive the string "Incorrect". In this case, you should terminate the program immediately.

Guessing the hidden nodes does not count towards the number of queries asked.

The interactor is not adaptive. The hidden nodes do not change with queries.

Do not forget to read the string "Correct" / "Incorrect" after guessing the hidden nodes.

You need to solve each test case before receiving the input for the next test case.

The limit of $14$ queries applies to each test case and not to the entire input.

After printing a query do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

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

Example

Input

```
1
3
1 2
1 3

1 1

2 3

3 1

3 1

Correct
```

Output

```
? 1 1

? 1 2

? 1 3

? 2 2 3

! 1 3
```

Note

The tree from the first test is shown below, and the hidden nodes are $1$ and $3$.

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

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

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

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

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

static vector<int> bfs_dist(int n, const vector<vector<int>>& g, int src) {
    vector<int> dist(n + 1, -1);
    queue<int> q;
    dist[src] = 0;
    q.push(src);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int to : g[v]) {
            if (dist[to] != -1) continue;
            dist[to] = dist[v] + 1;
            q.push(to);
        }
    }
    return dist;
}

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

    try {
        int t = inf.readInt(1, 10, "t");
        query_limit = 14;
        const long long hard_cap = max(200000LL, 10LL * query_limit);

        log_metrics(); 

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

        for (int tc = 1; tc <= t; tc++) {
            int n = inf.readInt(2, 1000, "n");

            vector<pair<int, int>> edges;
            edges.reserve(max(0, n - 1));

            vector<vector<int>> g(n + 1);
            for (int i = 0; i < n - 1; i++) {
                int u = inf.readInt(1, n, "u");
                int v = inf.readInt(1, n, "v");
                if (u == v) finish(_pe, "Hidden input invalid: self-loop");
                edges.push_back({u, v});
                g[u].push_back(v);
                g[v].push_back(u);
            }

            int s = inf.readInt(1, n, "s");
            int f = inf.readInt(1, n, "f");
            if (s == f) finish(_pe, "Hidden input invalid: s==f");

            vector<int> distS = bfs_dist(n, g, s);
            vector<int> distF = bfs_dist(n, g, f);
            for (int i = 1; i <= n; i++) {
                if (distS[i] < 0 || distF[i] < 0) finish(_pe, "Hidden input invalid: graph not connected");
            }

            cout << n << "\n";
            for (auto [u, v] : edges) cout << u << " " << v << "\n";
            cout.flush();

            while (true) {
                string cmd;
                if (!(cin >> cmd)) finish(_pe, "Unexpected EOF from contestant");

                if (cmd == "?") {
                    long long c_ll;
                    if (!(cin >> c_ll)) finish(_pe, "Malformed query: missing c");
                    if (c_ll < 1 || c_ll > n) {
                        cout << "-1 -1\n";
                        cout.flush();
                        finish(_pe, "Invalid query: c out of range");
                    }
                    int c = (int)c_ll;

                    vector<int> seen(n + 1, 0);
                    vector<int> nodes;
                    nodes.reserve(c);
                    for (int i = 0; i < c; i++) {
                        long long x_ll;
                        if (!(cin >> x_ll)) finish(_pe, "Malformed query: missing node");
                        if (x_ll < 1 || x_ll > n) {
                            cout << "-1 -1\n";
                            cout.flush();
                            finish(_pe, "Invalid query: node out of range");
                        }
                        int x = (int)x_ll;
                        if (seen[x]) {
                            cout << "-1 -1\n";
                            cout.flush();
                            finish(_pe, "Invalid query: duplicate node");
                        }
                        seen[x] = 1;
                        nodes.push_back(x);
                    }

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

                    int best = nodes[0];
                    int bestVal = distS[best] + distF[best];
                    for (int v : nodes) {
                        int val = distS[v] + distF[v];
                        if (val < bestVal || (val == bestVal && v < best)) {
                            bestVal = val;
                            best = v;
                        }
                    }

                    cout << best << " " << bestVal << "\n";
                    cout.flush();
                } else if (cmd == "!") {
                    long long a_ll, b_ll;
                    if (!(cin >> a_ll >> b_ll)) finish(_pe, "Malformed answer: expected two integers");
                    if (a_ll < 1 || a_ll > n || b_ll < 1 || b_ll > n || a_ll == b_ll) {
                        cout << "Incorrect\n";
                        cout.flush();
                        finish(_wa, "Invalid final answer format/range");
                    }
                    int a = (int)a_ll, b = (int)b_ll;

                    bool ok = (a == s && b == f) || (a == f && b == s);
                    if (!ok) {
                        cout << "Incorrect\n";
                        cout.flush();
                        finish(_wa, "Wrong hidden nodes guessed");
                    }

                    cout << "Correct\n";
                    cout.flush();
                    break;
                } else {
                    cout << "-1 -1\n";
                    cout.flush();
                    finish(_pe, "Unknown command token");
                }
            }
        }

        finish(_ok, "All test cases solved");
    } catch (const std::exception& e) {
        finish(_pe, string("Exception: ") + e.what());
    } catch (...) {
        finish(_pe, "Unknown exception");
    }
}


Adaptive interactor (C++):

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

static pair<int, vector<int>> bfs_farthest(int n, const vector<vector<int>>& g, int src) {
    vector<int> dist(n + 1, -1);
    queue<int> q;
    dist[src] = 0;
    q.push(src);
    int far = src;
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        if (dist[v] > dist[far]) far = v;
        for (int to : g[v]) {
            if (dist[to] != -1) continue;
            dist[to] = dist[v] + 1;
            q.push(to);
        }
    }
    return {far, dist};
}

static pair<int, int> diameter_endpoints(int n, const vector<pair<int, int>>& edges) {
    vector<vector<int>> g(n + 1);
    g.reserve(n + 1);
    for (auto [u, v] : edges) {
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int a = bfs_farthest(n, g, 1).first;
    int b = bfs_farthest(n, g, a).first;
    return {a, b};
}

static vector<pair<int, int>> build_path(int n) {
    vector<pair<int, int>> edges;
    edges.reserve(n - 1);
    for (int i = 1; i < n; i++) edges.push_back({i, i + 1});
    return edges;
}

static vector<pair<int, int>> build_star(int n, int center = 1) {
    vector<pair<int, int>> edges;
    edges.reserve(n - 1);
    for (int i = 1; i <= n; i++) {
        if (i == center) continue;
        edges.push_back({center, i});
    }
    return edges;
}

static vector<pair<int, int>> build_binary_heap(int n) {
    vector<pair<int, int>> edges;
    edges.reserve(n - 1);
    for (int i = 1; i <= n; i++) {
        int l = 2 * i;
        int r = 2 * i + 1;
        if (l <= n) edges.push_back({i, l});
        if (r <= n) edges.push_back({i, r});
    }
    return edges;
}

static vector<pair<int, int>> build_broom(int n, int handle_len) {
    handle_len = max(2, min(handle_len, n));
    vector<pair<int, int>> edges;
    edges.reserve(n - 1);
    for (int i = 1; i < handle_len; i++) edges.push_back({i, i + 1});
    int hub = handle_len;
    for (int v = handle_len + 1; v <= n; v++) edges.push_back({hub, v});
    return edges;
}

static vector<pair<int, int>> build_caterpillar(int n, int spine_len) {
    spine_len = max(2, min(spine_len, n));
    vector<pair<int, int>> edges;
    edges.reserve(n - 1);

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

    int next_node = spine_len + 1;
    while (next_node <= n) {
        int attach = rnd.next(1, spine_len);
        edges.push_back({attach, next_node});
        next_node++;
    }
    return edges;
}

static vector<pair<int, int>> build_double_broom(int n, int path_len, int left_leaves, int right_leaves) {
    path_len = max(2, min(path_len, n));
    int used = path_len + left_leaves + right_leaves;
    if (used > n) {
        int overflow = used - n;
        int take_from_right = min(overflow, right_leaves);
        right_leaves -= take_from_right;
        overflow -= take_from_right;
        left_leaves = max(0, left_leaves - overflow);
    }

    vector<pair<int, int>> edges;
    edges.reserve(n - 1);

    for (int i = 1; i < path_len; i++) edges.push_back({i, i + 1});
    int left_hub = 1;
    int right_hub = path_len;

    int cur = path_len + 1;
    for (int i = 0; i < left_leaves && cur <= n; i++, cur++) edges.push_back({left_hub, cur});
    for (int i = 0; i < right_leaves && cur <= n; i++, cur++) edges.push_back({right_hub, cur});

    while (cur <= n) {
        int attach = rnd.next(1, path_len);
        edges.push_back({attach, cur});
        cur++;
    }
    return edges;
}

static vector<pair<int, int>> build_prufer_tree(int n) {
    vector<int> prufer(max(0, n - 2));
    for (int i = 0; i < (int)prufer.size(); i++) prufer[i] = rnd.next(1, n);

    vector<int> degree(n + 1, 1);
    for (int x : prufer) degree[x]++;

    priority_queue<int, vector<int>, greater<int>> pq;
    for (int i = 1; i <= n; i++) if (degree[i] == 1) pq.push(i);

    vector<pair<int, int>> edges;
    edges.reserve(n - 1);

    for (int x : prufer) {
        int leaf = pq.top();
        pq.pop();
        edges.push_back({leaf, x});
        degree[leaf]--;
        degree[x]--;
        if (degree[x] == 1) pq.push(x);
    }
    int u = pq.top(); pq.pop();
    int v = pq.top(); pq.pop();
    edges.push_back({u, v});
    return edges;
}

static void apply_permutation(int n, vector<pair<int, int>>& edges, int& s, int& f, bool permute_sf) {
    vector<int> perm(n + 1);
    for (int i = 1; i <= n; i++) perm[i] = i;
    for (int i = 1; i <= n; i++) {
        int j = rnd.next(i, n);
        swap(perm[i], perm[j]);
    }
    for (auto& e : edges) {
        e.first = perm[e.first];
        e.second = perm[e.second];
        if (e.first == e.second) {
            int a = e.first, b = e.second;
            (void)a; (void)b;
        }
    }
    if (permute_sf) {
        s = perm[s];
        f = perm[f];
        if (s == f) {
            f = (f % n) + 1;
            if (f == s) f = (f % n) + 1;
        }
    }
}

static pair<int, int> pick_hidden_pair_odd_diameter(int n, const vector<pair<int, int>>& edges) {
    vector<vector<int>> g(n + 1);
    for (auto [u, v] : edges) {
        g[u].push_back(v);
        g[v].push_back(u);
    }

    int a = bfs_farthest(n, g, 1).first;

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

    vector<int> path;
    for (int v = b; v != -1; v = par[v]) {
        path.push_back(v);
        if (v == a) break;
    }
    int L = (int)path.size() - 1;

    int s = a, f = b;
    if (L >= 2 && (L % 2 == 0)) {
        f = path[1];
        if (f == s) f = b;
    }
    if (s == f) {
        f = (s % n) + 1;
        if (f == s) f = (f % n) + 1;
    }
    return {s, f};
}

int main(int argc, char** argv) {
    registerGen(argc, argv, 1);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string mode = opt<string>("mode", "non");
    int n = opt<int>("n", 1000);
    n = max(2, min(1000, n));
    string shape = opt<string>("shape", "auto");

    long long seedVal = 0;
    if (argc >= 2) seedVal = atoll(argv[1]);

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

    vector<pair<int, int>> edges;
    int s = 1, f = 2;

    if (seedVal == 1) {
        n = 2;
        edges = {{1, 2}};
        s = 1; f = 2;
    } else if (seedVal == 2) {
        n = 1000;
        edges = build_star(n, 1);
        s = 2; f = 3;
    } else if (seedVal == 3) {
        n = 1000;
        edges = build_path(n);
        s = 250; f = 750;
    } else {
        if (shape == "auto") {
            int pick = (int)(seedVal % 5);
            if (pick == 0) shape = "binary";
            else if (pick == 1) shape = "broom";
            else if (pick == 2) shape = "prufer";
            else if (pick == 3) shape = "caterpillar";
            else shape = "doublebroom";
        }

        if (shape == "path") {
            edges = build_path(n);
        } else if (shape == "star") {
            edges = build_star(n, 1);
        } else if (shape == "binary") {
            edges = build_binary_heap(n);
        } else if (shape == "broom") {
            int handle_len = rnd.next((int)(n * 7 / 10), (int)(n * 9 / 10));
            edges = build_broom(n, handle_len);
        } else if (shape == "caterpillar") {
            int spine_len = rnd.next((int)(n * 2 / 3), (int)(n * 9 / 10));
            edges = build_caterpillar(n, spine_len);
        } else if (shape == "doublebroom") {
            int path_len = rnd.next((int)(n * 1 / 2), (int)(n * 4 / 5));
            int rem = n - path_len;
            int left_leaves = rem / 2;
            int right_leaves = rem - left_leaves;
            edges = build_double_broom(n, path_len, left_leaves, right_leaves);
        } else {
            edges = build_prufer_tree(n);
        }

        if (mode == "non") {
            auto pr = pick_hidden_pair_odd_diameter(n, edges);
            s = pr.first;
            f = pr.second;
        }

        int dummyS = 1, dummyF = 2;
        apply_permutation(n, edges, dummyS, dummyF, false);
        if (mode == "non") apply_permutation(n, edges, s, f, true);
    }

    cout << n << "\n";
    for (auto [u, v] : edges) cout << u << " " << v << "\n";
    if (mode == "non") {
        cout << s << " " << f << "\n";
    } else if (mode == "adp") {
        
    } else {
        quitf(_fail, "Unknown --mode (expected non/adp)");
    }

    return 0;
}