Problem: cf1370_f2
Difficulty: hard
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 $11$ 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 $11$ 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 <algorithm>
#include <csignal>
#include <iostream>
#include <queue>
#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());
}

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

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

    const int t = inf.readInt();
    cout << t << "\n" << flush;

    const long long per_case_limit = 11;
    query_limit = per_case_limit * (long long)t;
    
    log_queries();

    for (int tc = 0; tc < t; tc++) {
        const int n = inf.readInt();
        vector<pair<int, int>> edges;
        edges.reserve(n - 1);
        vector<vector<int>> g(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int u = inf.readInt();
            int v = inf.readInt();
            edges.push_back({u, v});
            g[u].push_back(v);
            g[v].push_back(u);
        }
        const int s = inf.readInt();
        const int f = inf.readInt();
        if (s == f) {
            quit_with(_fail, "Invalid hidden nodes");
        }

        vector<int> dist_s = bfs_dist(g, s);
        vector<int> dist_f = bfs_dist(g, f);

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

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

            if (cmd == "?") {
                used++;
                queries++;
                if (used > per_case_limit) {
                    cout << "-1 -1\n" << flush;
                    quit_with(_pe, "Too many queries");
                }

                int c;
                if (!(cin >> c)) {
                    cout << "-1 -1\n" << flush;
                    quit_with(_pe, "Invalid query");
                }
                if (c < 1 || c > n) {
                    cout << "-1 -1\n" << flush;
                    quit_with(_pe, "Invalid c");
                }

                vector<int> nodes(c);
                vector<char> seen(n + 1, 0);
                for (int i = 0; i < c; i++) {
                    if (!(cin >> nodes[i])) {
                        cout << "-1 -1\n" << flush;
                        quit_with(_pe, "Invalid node list");
                    }
                    int v = nodes[i];
                    if (v < 1 || v > n || seen[v]) {
                        cout << "-1 -1\n" << flush;
                        quit_with(_pe, "Invalid node list");
                    }
                    seen[v] = 1;
                }

                int best = nodes[0];
                int best_d = dist_s[best] + dist_f[best];
                for (int v : nodes) {
                    int d = dist_s[v] + dist_f[v];
                    if (d < best_d || (d == best_d && v < best)) {
                        best = v;
                        best_d = d;
                    }
                }
                cout << best << " " << best_d << "\n" << flush;
                continue;
            }

            if (cmd == "!") {
                int a, b;
                if (!(cin >> a >> b)) {
                    quit_with(_pe, "Invalid answer format");
                }
                bool ok = (a == s && b == f) || (a == f && b == s);
                cout << (ok ? "Correct" : "Incorrect") << "\n" << flush;
                if (!ok) {
                    quit_with(_wa, "Wrong answer");
                }
                break;
            }

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

    quit_with(_ok, "All tests correct");
}


Adaptive interactor (C++):

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

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

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

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

static pair<int, int> diameter_endpoints(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);
    }

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

    int a = bfs_far(1).first;
    int b = bfs_far(a).first;
    if (a == b) {
        b = (a % n) + 1;
    }
    return {a, b};
}

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

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

    int t = 3;
    if (seed == 1) t = 1;
    else if (seed == 2) t = 2;
    else if (seed <= 10) t = 3;
    else t = 5;
    t = max(1, min(10, t));

    cout << t << "\n";
    for (int tc = 0; tc < t; tc++) {
        int n = 1000;
        if (seed == 1) n = 2;
        else if (seed == 2) n = 3;
        else if (seed <= 10) n = 50 + (int)seed * 20;
        n = max(2, min(1000, n));

        vector<pair<int, int>> edges;
        int kind = (int)((seed + tc) % 3);
        if (kind == 0) edges = gen_path(n);
        else if (kind == 1) edges = gen_balanced(n);
        else edges = gen_random_tree(n);

        auto [s, f] = diameter_endpoints(n, edges);

        cout << n << "\n";
        for (auto [u, v] : edges) cout << u << " " << v << "\n";
        cout << s << "\n" << f << "\n";
    }
    return 0;
}