Problem: cf1521_c
Difficulty: hard
Categories: math
Time limit: 3000 ms, memory limit: 256 MB

This is an interactive problem!

Nastia has a hidden permutation $p$ of length $n$ consisting of integers from $1$ to $n$. You, for some reason, want to figure out the permutation. To do that, you can give her an integer $t$ ($1 \le t \le 2$), two different indices $i$ and $j$ ($1 \le i, j \le n$, $i \neq j$), and an integer $x$ ($1 \le x \le n - 1$).

Depending on $t$, she will answer:

* $t = 1$: $\max{(\min{(x, p_i)}, \min{(x + 1, p_j)})}$;
* $t = 2$: $\min{(\max{(x, p_i)}, \max{(x + 1, p_j)})}$.

You can ask Nastia at most $\lfloor \frac {3 \cdot n} { 2} \rfloor + 30$ times. It is guaranteed that she will not change her permutation depending on your queries. Can you guess the permutation?

Input

The input consists of several test cases. In the beginning, you receive the integer $T$ ($1 \le T \le 10\,000$) — the number of test cases.

At the beginning of each test case, you receive an integer $n$ ($3 \le n \le 10^4$) — the length of the permutation $p$.

It's guaranteed that the permutation is fixed beforehand and that the sum of $n$ in one test doesn't exceed $2 \cdot 10^4$.

Interaction

To ask a question, print "? $t$ $i$ $j$ $x$" ($t = 1$ or $t = 2$, $1 \le i, j \le n$, $i \neq j$, $1 \le x \le n - 1$) Then, you should read the answer.

If we answer with $−1$ instead of a valid answer, that means you exceeded the number of queries or made an invalid query. Exit immediately after receiving $−1$ and you will see the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

To print the answer, print "! $p_1$ $p_2$ $\ldots$ $p_{n}$ (without quotes). Note that answering doesn't count as one of the $\lfloor \frac {3 \cdot n} {2} \rfloor + 30$ queries.

After printing a query or printing 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 the documentation for other languages.

Example

Input

```
2
4

3

2

5

3
```

Output

```
? 2 4 1 3

? 1 2 4 2

! 3 1 4 2

? 2 3 4 2

! 2 5 3 4 1
```

Note

Consider the first test case.

The hidden permutation is $[3, 1, 4, 2]$.

We print: "? $2$ $4$ $1$ $3$" and get back $\min{(\max{(3, p_4}), \max{(4, p_1)})} = 3$.

We print: "? $1$ $2$ $4$ $2$" and get back $\max{(\min{(2, p_2)}, \min{(3, p_4)})} = 2$.

Consider the second test case.

The hidden permutation is $[2, 5, 3, 4, 1]$.

We print: "? $2$ $3$ $4$ $2$" and get back $\min{(\max{(2, p_3}), \max{(3, p_4)})} = 3$.

Non-adaptive interactor (C++):
#include "testlib.h"

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

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

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

struct HiddenTest {
    int n = 0;
    vector<int> p;
};

static int compute_answer(int t, int x, int pi, int pj) {
    
    
    
    if (t == 1) {
        return max(min(x, pi), min(x + 1, pj));
    }
    if (t == 2) {
        return min(max(x, pi), max(x + 1, pj));
    }
    return -1;
}

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

    int T = inf.readInt();
    if (T < 1) {
        die(_fail, "Invalid hidden input: T < 1");
    }

    vector<HiddenTest> tests;
    tests.reserve(T);

    query_limit = 0;
    for (int tc = 0; tc < T; tc++) {
        int n = inf.readInt();
        if (n < 3) {
            die(_fail, "Invalid hidden input: n < 3");
        }
        vector<int> p;
        p.reserve(n);
        for (int i = 0; i < n; i++) {
            p.push_back(inf.readInt());
        }
        tests.push_back(HiddenTest{n, move(p)});
        query_limit += (3LL * n) / 2 + 30;
    }

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

    cout << T << endl;
    cout.flush();

    for (int tc = 0; tc < T; tc++) {
        const int n = tests[tc].n;
        const vector<int>& p = tests[tc].p;

        cout << n << endl;
        cout.flush();

        while (true) {
            string cmd;
            if (!(cin >> cmd)) {
                die(_wa, "Unexpected EOF from solution");
            }

            if (cmd == "?") {
                if (queries >= query_limit) {
                    cout << -1 << endl;
                    cout.flush();
                    die(_wa, "Query limit exceeded");
                }
                if (queries >= hard_cap) {
                    die(_pe, "Too many queries (hard cap)");
                }

                int t, i, j, x;
                if (!(cin >> t >> i >> j >> x)) {
                    cout << -1 << endl;
                    cout.flush();
                    die(_wa, "Invalid query format");
                }
                if (!(t == 1 || t == 2)) {
                    cout << -1 << endl;
                    cout.flush();
                    die(_wa, "Invalid t (must be 1 or 2)");
                }
                if (i < 1 || i > n || j < 1 || j > n || i == j) {
                    cout << -1 << endl;
                    cout.flush();
                    die(_wa, "Invalid i/j");
                }
                if (x < 1 || x > n - 1) {
                    cout << -1 << endl;
                    cout.flush();
                    die(_wa, "Invalid x");
                }

                queries++;
                log_metrics();

                const int pi = p[i - 1];
                const int pj = p[j - 1];
                const int ans = compute_answer(t, x, pi, pj);
                cout << ans << endl;
                cout.flush();
                continue;
            }

            if (cmd == "!") {
                vector<int> guess;
                guess.reserve(n);
                for (int i = 0; i < n; i++) {
                    int v;
                    if (!(cin >> v)) {
                        die(_wa, "Unexpected EOF in answer");
                    }
                    guess.push_back(v);
                }
                if (guess != p) {
                    die(_wa, "Wrong answer");
                }
                break;
            }

            die(_pe, "Unknown command (expected '?' or '!')");
        }
    }

    die(_ok, "OK");
}


Adaptive interactor (C++):

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

    const int MAX_SUM_N = 20000;
    const int MAX_N_PER_TEST = 10000;
    const int MIN_N = 3;

    struct TestCase {
        int n;
        vector<int> p;
    };
    vector<TestCase> tests;
    int remaining_sum = MAX_SUM_N;

    
    long long seed_val = -1;
    try {
        string s = argv[1];
        bool is_num = true;
        for (char c : s) if (!isdigit(c)) is_num = false;
        if (is_num && !s.empty()) seed_val = stoll(s);
    } catch (...) {}

    
    auto gen_perm = [&](int n, string type) {
        vector<int> p(n);
        iota(p.begin(), p.end(), 1);
        if (type == "random") {
            shuffle(p.begin(), p.end());
        } else if (type == "rev") {
            reverse(p.begin(), p.end());
        } else if (type == "shift") {
            int k = rnd.next(1, n - 1);
            rotate(p.begin(), p.begin() + k, p.end());
        } else if (type == "swap") {
            
            int k = rnd.next(1, max(1, (int)sqrt(n)));
            for(int i = 0; i < k; ++i) {
                int a = rnd.next(0, n - 1);
                int b = rnd.next(0, n - 1);
                swap(p[a], p[b]);
            }
        } else if (type == "interleave") {
            
            vector<int> q(n);
            int l = 1, r = n;
            for(int i = 0; i < n; ++i) {
                if (i % 2 == 0) q[i] = l++;
                else q[i] = r--;
            }
            p = q;
        }
        return p;
    };

    if (seed_val == 1) {
        
        int n = MAX_N_PER_TEST;
        tests.push_back({n, gen_perm(n, "random")});
    } else if (seed_val == 2) {
        
        int n = MAX_N_PER_TEST;
        tests.push_back({n, gen_perm(n, "rev")});
    } else if (seed_val == 3) {
        
        int n = MIN_N;
        tests.push_back({n, gen_perm(n, "random")});
    } else {
        
        int strat = rnd.next(0, 10);
        
        if (strat < 4) {
            
            while (remaining_sum >= MIN_N) {
                int max_can = min(remaining_sum, MAX_N_PER_TEST);
                
                int n = rnd.next(max(MIN_N, max_can / 2), max_can);
                tests.push_back({n, gen_perm(n, "random")});
                remaining_sum -= n;
                
                if (tests.size() >= 5 && rnd.next(3) == 0) break;
            }
        } else if (strat < 7) {
            
            while (remaining_sum >= MIN_N) {
                int n = rnd.next(MIN_N, min(remaining_sum, 60));
                tests.push_back({n, gen_perm(n, "random")});
                remaining_sum -= n;
                if (tests.size() >= 1000) break;
            }
        } else {
            
            while (remaining_sum >= MIN_N) {
                int max_can = min(remaining_sum, MAX_N_PER_TEST);
                int n;
                int p = rnd.next(100);
                
                
                if (p < 20) n = rnd.next(MIN_N, 50);
                else if (p < 50) n = rnd.next(50, 500);
                else n = rnd.next(500, max(500, max_can));
                
                n = max(MIN_N, min(n, max_can));

                
                string type = "random";
                int t = rnd.next(10);
                if (t == 0) type = "rev";
                else if (t == 1) type = "interleave";
                else if (t == 2) type = "shift";
                else if (t == 3) type = "swap";

                tests.push_back({n, gen_perm(n, type)});
                remaining_sum -= n;
                
                if (tests.size() >= 500) break;
            }
        }
    }

    
    if (tests.empty()) {
        int n = rnd.next(MIN_N, min(MAX_SUM_N, MAX_N_PER_TEST));
        tests.push_back({n, gen_perm(n, "random")});
    }

    
    println(tests.size());
    for (const auto& t : tests) {
        println(t.n);
        println(t.p);
    }

    return 0;
}