Problem: cf1896_g
Difficulty: medium
Categories: greedy, data structures
Time limit: 5000 ms, memory limit: 256 MB

This is an interactive problem.

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

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

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

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

Input

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

The only line of each test case contains a single integer $n$ ($2 \le n \le 20$) — the number of pepes in one race.

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

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

Interaction

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

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

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

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

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

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

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

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

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

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

Example

Input

```
1
2

2

4

4

3

2
```

Output

```
? 1 2

? 3 4

? 2 4

? 2 3

? 2 1

! 4 3 2
```

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




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


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


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



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


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

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

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

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

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

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

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

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

        
        update_metrics(queries, limit);
        
        if (tc == 1) log_metrics();

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

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

            if (token == "!") {
                
                vector<int> p(ans_len);
                set<int> distinct_check;
                
                for (int i = 0; i < ans_len; ++i) {
                    if (!(cin >> p[i])) {
                        force_metrics(queries, limit);
                        finish(_pe, "Unexpected EOF reading answer");
                    }
                    
                    if (p[i] < 1 || p[i] > n * n) {
                        force_metrics(queries, limit);
                        finish(_pe, format("Answer label out of range: %d", p[i]));
                    }
                    if (distinct_check.count(p[i])) {
                        force_metrics(queries, limit);
                        finish(_pe, format("Duplicate label in answer: %d", p[i]));
                    }
                    distinct_check.insert(p[i]);
                }

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

                
                case_active = false;

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

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

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

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

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

                
                cout << max_label << endl; 

                queries++;
                update_metrics(queries, limit);
                
                
                if (queries % 50 == 0) log_metrics();
            }
        }
    }

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


Adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


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





static const long long HARD_CAP = 2000000; 


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


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


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

void solve_test_case() {
    
    int n = inf.readInt();
    
    
    cout << n << endl;
    
    
    
    long long limit_for_case = 2LL * n * n - 2LL * n + 1;
    total_query_limit += limit_for_case;
    
    
    log_metrics();
    
    
    
    vector<int> p(n * n);
    
    
    
    
    
    int first_val = inf.readInt();
    
    if (first_val == 0) {
        
        iota(p.begin(), p.end(), 1);
        myshuffle(p);
    } else {
        
        p[0] = first_val;
        for (int i = 1; i < n * n; ++i) {
            p[i] = inf.readInt();
        }
    }
    
    
    
    
    
    vector<int> speed_of(n * n + 1);
    for (int i = 0; i < n * n; ++i) {
        speed_of[p[i]] = (n * n) - i;
    }
    
    
    while (true) {
        
        char type;
        if (!(cin >> type)) {
            finish(_pe, "Unexpected EOF: expected query type ('?' or '!')");
        }
        
        if (type == '?') {
            
            total_queries++;
            if (total_queries > HARD_CAP) {
                finish(_pe, "Hard query limit exceeded (infinite loop protection)");
            }
            
            
            vector<int> race(n);
            vector<int> check_distinct;
            int winner = -1;
            int max_speed = -1;
            
            for (int i = 0; i < n; ++i) {
                if (!(cin >> race[i])) {
                    finish(_pe, "Unexpected EOF reading race participants");
                }
                
                if (race[i] < 1 || race[i] > n * n) {
                    finish(_pe, "Invalid pepe label: %d", race[i]);
                }
                check_distinct.push_back(race[i]);
                
                
                int s = speed_of[race[i]];
                if (s > max_speed) {
                    max_speed = s;
                    winner = race[i];
                }
            }
            
            
            sort(check_distinct.begin(), check_distinct.end());
            if (unique(check_distinct.begin(), check_distinct.end()) != check_distinct.end()) {
                finish(_pe, "Race participants must be distinct");
            }
            
            
            cout << winner << endl;
            
            
            if (total_queries % 100 == 0) log_metrics();
            
        } else if (type == '!') {
            
            
            int k = n * n - n + 1;
            vector<int> ans(k);
            for (int i = 0; i < k; ++i) {
                if (!(cin >> ans[i])) {
                    finish(_pe, "Unexpected EOF reading answer");
                }
            }
            
            
            
            vector<int> correct_ans;
            for (int i = 0; i < k; ++i) {
                correct_ans.push_back(p[i]);
            }
            
            if (ans != correct_ans) {
                finish(_wa, "Incorrect order of fastest pepes");
            }
            
            
            break;
            
        } else {
            finish(_pe, "Invalid query type: '%c'", type);
        }
    }
}

int main(int argc, char* argv[]) {
    
    registerInteraction(argc, argv);
    atexit(log_metrics);
    
    
    int t = inf.readInt();
    
    
    cout << t << endl;
    
    
    for (int i = 0; i < t; ++i) {
        solve_test_case();
    }
    
    
    finish(_ok, "All test cases passed");
}


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

using namespace std;

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

    string mode = opt<string>("mode", "non");
    int n_opt = opt<int>("n", -1);
    int t_opt = opt<int>("t", -1);

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

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

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

    
    if (n_opt != -1) {
        
        int count = (t_opt == -1) ? 1 : t_opt;
        for (int i = 0; i < count; ++i) {
            if (!add_n(n_opt)) break;
        }
    } else {
        
        long long seed_val = 0;
        try {
            
            string s(argv[1]);
            if (s.find_first_not_of("0123456789") == string::npos) {
                seed_val = stoll(s);
            }
        } catch (...) {}

        if (t_opt != -1) {
            
            for (int i = 0; i < t_opt; ++i) {
                if (!add_n(rnd.next(2, 20))) break;
            }
        } else if (seed_val == 1) {
            
            
            while (add_n(20));
        } else if (seed_val == 2) {
            
            
            for (int i = 0; i < 100; ++i) add_n(2);
        } else if (seed_val == 3) {
            
            for (int i = 2; i <= 20; ++i) add_n(i);
        } else {
            
            int t_rnd = rnd.next(10, 50);
            for (int i = 0; i < t_rnd; ++i) {
                if (!add_n(rnd.next(2, 20))) break;
            }
        }
    }

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

    
    println(ns.size());
    for (int n : ns) {
        println(n);
        if (mode == "non") {
            
            vector<int> p(n * n);
            iota(p.begin(), p.end(), 1);
            shuffle(p.begin(), p.end());
            println(p);
        } else {
            
            
            println(0);
        }
    }

    return 0;
}