Problem: cf2134_e
Difficulty: medium
Categories: game, data structures
Time limit: 2000 ms, memory limit: 256 MB

This is an interactive problem.

You are given $n$ boxes, indexed from $1$ to $n$. The boxes look identical, but each one has a hidden power value $a_i$, which is either $1$ or $2$.

You want to determine the power value of each box. To do so, you conduct the following experiment. Initially, the $i$-th box is placed at coordinate $i$ on a number line ($1 \le i \le n$).

You are allowed to perform the following two types of queries:

* "swap $x$" ($1 \le x \le n - 1$): Swap the boxes currently located at coordinates $x$ and $x + 1$. Note that this change is permanent and affects all subsequent queries.
* "throw $x$" ($1 \le x \le n$): Throw a ball at the box located at coordinate $x$. The ball travels $p$ units forward to coordinate $x + p$ if the power value of the box is $p$. If there is a box at the new coordinate, the ball jumps again using the power of that box. This continues until the ball lands on a coordinate without a box. As a response, you are given the total number of jumps the ball made before stopping.

Your task is to determine the power value of each box using no more than $\left\lceil \frac{3n}{2} \right\rceil$ queries in total, counting both swap and throw queries.

Input

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

The first and the only line of each test case contains a single integer $n$ ($2 \le n \le 1000$) — the number of boxes.

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

Interaction

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

To make a query, output a line in one of the following formats:

* "swap $x$" (without quotes) ($1 \le x \le n - 1$): Swap the boxes currently located at coordinates $x$ and $x + 1$.
* "throw $x$" (without quotes) ($1 \le x \le n$): Throw a ball at the box located at coordinate $x$. The jury will respond with an integer representing the number of jumps the ball made before stopping.

Note that queries are case sensitive.

Once you have determined the power value of each box, output a line in the following format:

* "! $a_1 \space a_2 \space \ldots \space a_n$" (without quotes): Here, $a_i$ is the power value of the box initially located at coordinate $i$ ($1 \le i \le n$). After submitting the final answer, proceed on to the next test case.

Note that submitting the final answer with the previous query does not count towards the limit of $\left\lceil \frac{3n}{2} \right\rceil$ queries.

If your program exceeds $\left\lceil \frac{3n}{2} \right\rceil$ queries in one test case, your program must terminate immediately to receive the verdict Wrong Answer. Otherwise, it may receive any other verdict.

After outputting 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.

The interactor is non-adaptive; the power values of the boxes remain constant throughout the interaction.

Example

Input

```
2
4

2

3

3

2

2

1
```

Output

```
throw 2

swap 3
throw 2

throw 1

! 2 1 2 1

throw 1

swap 1
throw 1

! 1 2
```

Note

Below is the interaction process in the example:

|  |  |  |
| --- | --- | --- |
| Solution | Jury | Explanation |
|  | 2 | There are $2$ test cases. |
|  | 4 | There are $4$ boxes in the first test case. The hidden power values are $a = [2,1,2,1]$. |
| throw 2 | 2 | Throw a ball at the box located at coordinate $2$. The ball travels through coordinates $2 \to 3 \to 5$ and stops at coordinate $5$, so the response is $2$. |
| swap 3 |  | Swap the boxes located at coordinate $3$ and $4$. Now box $3$ is located at coordinate $4$ and box $4$ is located at coordinate $3$. |
| throw 2 | 3 | Throw a ball at the box located at coordinate $2$. The ball travels through coordinates $2 \to 3 \to 4 \to 6$ and stops at coordinate $6$, so the response is $3$. Note that the response is different because of the swap. |
| throw 1 | 3 | Throw a ball at the box located at coordinate $1$. The ball travels through coordinates $1 \to 3 \to 4 \to 6$ and stops at coordinate $6$, so the response is $3$. |
| ! 2 1 2 1 |  | The solution concludes that the power values are $[2,1,2,1]$. |
|  | 2 | There are $2$ boxes in the second test case. The hidden power values are $a = [1, 2]$. |
| throw 1 | 2 | Throw a ball at the box located at coordinate $1$. The ball travels through coordinates $1 \to 2 \to 4$ and stops at coordinate $4$, so the response is $2$. |
| swap 1 |  | Swap the boxes located at coordinate $1$ and $2$. Now box $1$ is located at coordinate $2$ and box $2$ is located at coordinate $1$. |
| throw 1 | 1 | Throw a ball at the box located at coordinate $1$. The ball travels through coordinates $1 \to 3$ and stops at coordinate $3$, so the response is $1$. |
| ! 1 2 |  | The solution concludes that the power values are $[1,2]$. |

Empty lines in the example input and output are given only for better readability; you don't need to output them in your solution.

Note that in the first test case, the given queries are in fact insufficient to uniquely determine the power values; they are given only to illustrate the input/output format.

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 bool readToken(string& s) {
    if (!(cin >> s)) return false;
    return true;
}

static bool parseLLStrict(const string& s, long long& out) {
    if (s.empty()) return false;
    int i = 0;
    bool neg = false;
    if (s[0] == '-') {
        neg = true;
        i = 1;
    } else if (s[0] == '+') {
        i = 1;
    }
    if (i == (int)s.size()) return false;
    for (int j = i; j < (int)s.size(); j++) {
        if (s[j] < '0' || s[j] > '9') return false;
    }
    __int128 val = 0;
    for (int j = i; j < (int)s.size(); j++) {
        val = val * 10 + (s[j] - '0');
        if (val > (__int128)LLONG_MAX + 1) return false;
    }
    if (neg) val = -val;
    if (val < (__int128)LLONG_MIN || val > (__int128)LLONG_MAX) return false;
    out = (long long)val;
    return true;
}

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

    try {
        int t = inf.readInt(1, 500, "t");
        struct CaseData {
            int n;
            vector<int> a; 
        };
        vector<CaseData> cases;
        cases.reserve(t);

        long long totalBudget = 0;
        for (int tc = 0; tc < t; tc++) {
            int n = inf.readInt(2, 1000, "n");
            vector<int> a(n + 1, 1);
            for (int i = 1; i <= n; i++) a[i] = inf.readInt(1, 2, ("a" + to_string(i)).c_str());
            cases.push_back(CaseData{n, std::move(a)});
            totalBudget += (3LL * n + 1) / 2; 
        }

        query_limit = totalBudget;
        log_metrics(); 

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

        cout << t << endl; 
        for (int tc = 0; tc < t; tc++) {
            int n = cases[tc].n;
            const vector<int>& a = cases[tc].a;

            vector<int> pos_to_box(n + 1), box_to_pos(n + 1);
            for (int i = 1; i <= n; i++) {
                pos_to_box[i] = i;
                box_to_pos[i] = i;
            }

            cout << n << endl; 

            while (true) {
                string cmd;
                if (!readToken(cmd)) finish(_pe, "unexpected EOF while waiting for a command");

                if (cmd == "swap") {
                    string xs;
                    if (!readToken(xs)) finish(_pe, "unexpected EOF after swap");
                    long long xll;
                    if (!parseLLStrict(xs, xll)) finish(_pe, "swap argument is not an integer");
                    if (xll < 1 || xll > n - 1) finish(_pe, "swap index out of range");
                    int x = (int)xll;

                    queries++;
                    if (queries > hard_cap) finish(_pe, "hard cap exceeded");

                    int b1 = pos_to_box[x];
                    int b2 = pos_to_box[x + 1];
                    pos_to_box[x] = b2;
                    pos_to_box[x + 1] = b1;
                    box_to_pos[b1] = x + 1;
                    box_to_pos[b2] = x;
                } else if (cmd == "throw") {
                    string xs;
                    if (!readToken(xs)) finish(_pe, "unexpected EOF after throw");
                    long long xll;
                    if (!parseLLStrict(xs, xll)) finish(_pe, "throw argument is not an integer");
                    if (xll < 1 || xll > n) finish(_pe, "throw index out of range");
                    int x = (int)xll;

                    queries++;
                    if (queries > hard_cap) finish(_pe, "hard cap exceeded");

                    int jumps = 0;
                    int cur = x;
                    while (cur <= n) {
                        jumps++;
                        int box = pos_to_box[cur];
                        int p = a[box];
                        cur += p;
                    }

                    cout << jumps << endl; 
                } else if (cmd == "!") {
                    vector<int> ans(n + 1, -1);
                    for (int i = 1; i <= n; i++) {
                        string ts;
                        if (!readToken(ts)) finish(_pe, "unexpected EOF in final answer");
                        long long vll;
                        if (!parseLLStrict(ts, vll)) finish(_pe, "final answer contains non-integer token");
                        if (vll != 1 && vll != 2) finish(_pe, "final answer values must be 1 or 2");
                        ans[i] = (int)vll;
                    }
                    for (int i = 1; i <= n; i++) {
                        if (ans[i] != a[i]) finish(_wa, "wrong final answer");
                    }
                    break; 
                } else {
                    finish(_pe, "unknown command");
                }
            }
        }

        string extra;
        if (readToken(extra)) finish(_pe, "extra output after finishing all test cases");
        finish(_ok, "OK");
    } 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 vector<int> make_case_non(int n, long long seedVal, int p2permille) {
    n = max(n, 2);
    vector<int> a(n + 1, 1);

    if (seedVal == 1) {
        n = 2;
        a.assign(n + 1, 1);
        a[1] = 1;
        a[2] = 2;
        return a;
    }
    if (seedVal == 2) {
        n = 1000;
        a.assign(n + 1, 2);
        return a;
    }
    if (seedVal == 3) {
        n = 1000;
        a.assign(n + 1, 1);
        for (int i = 1; i <= n; i++) a[i] = (i % 2 ? 1 : 2);
        return a;
    }

    
    if (n < 900) n = 1000;
    a.assign(n + 1, 1);

    
    int i = 1;
    while (i <= n) {
        int segType = rnd.next(0, 4);
        int len = rnd.next(10, 80);
        len = min(len, n - i + 1);

        if (segType == 0) { 
            for (int j = 0; j < len; j++) a[i + j] = 1;
        } else if (segType == 1) { 
            for (int j = 0; j < len; j++) a[i + j] = 2;
        } else if (segType == 2) { 
            for (int j = 0; j < len; j++) a[i + j] = (j % 2 == 0 ? 1 : 2);
        } else if (segType == 3) { 
            for (int j = 0; j < len; j++) a[i + j] = (j % 2 == 0 ? 2 : 1);
        } else { 
            for (int j = 0; j < len; j++) {
                int m = j % 3;
                a[i + j] = (m == 0 ? 2 : 1);
            }
        }
        i += len;
    }

    
    int suf = min(120, n);
    int start = n - suf + 1;
    for (int k = 0; k < suf; k++) {
        int pos = start + k;
        if (k < suf / 2) a[pos] = (k % 2 == 0 ? 1 : 2);
        else a[pos] = (rnd.next(0, 9) < 7 ? 1 : 2);
    }

    
    int target2 = (int)((1LL * n * p2permille + 500) / 1000);
    target2 = max(0, min(n, target2));

    int cnt2 = 0;
    for (int idx = 1; idx <= n; idx++) cnt2 += (a[idx] == 2);

    vector<int> pos1, pos2;
    pos1.reserve(n);
    pos2.reserve(n);
    for (int idx = 1; idx <= n; idx++) {
        if (a[idx] == 1) pos1.push_back(idx);
        else pos2.push_back(idx);
    }

    while (cnt2 < target2 && !pos1.empty()) {
        int pick = rnd.next(0, (int)pos1.size() - 1);
        int idx = pos1[pick];
        a[idx] = 2;
        cnt2++;
        pos1[pick] = pos1.back();
        pos1.pop_back();
    }
    while (cnt2 > target2 && !pos2.empty()) {
        int pick = rnd.next(0, (int)pos2.size() - 1);
        int idx = pos2[pick];
        a[idx] = 1;
        cnt2--;
        pos2[pick] = pos2.back();
        pos2.pop_back();
    }

    
    for (int idx = 1; idx <= n; idx++) if (a[idx] != 1 && a[idx] != 2) a[idx] = 1;
    return a;
}

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

    string mode = opt<string>("mode", "non");
    int nOpt = opt<int>("n", 1000);
    int p2permille = opt<int>("p2", 500); 
    p2permille = max(0, min(1000, p2permille));

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

    if (mode != "non" && mode != "adp") {
        
        mode = "non";
    }

    cout << 1 << "\n"; 

    if (mode == "non") {
        vector<int> a = make_case_non(nOpt, seedVal, p2permille);
        int n = (int)a.size() - 1;
        cout << n << "\n";
        for (int i = 1; i <= n; i++) {
            if (i > 1) cout << ' ';
            cout << a[i];
        }
        cout << "\n";
    } else { 
        int n = max(nOpt, 2);
        if (seedVal == 1) n = 2;
        else if (seedVal == 2 || seedVal == 3) n = 1000;
        else if (n < 900) n = 1000;

        int k2;
        if (seedVal == 1) k2 = 1;
        else if (seedVal == 2) k2 = n;
        else if (seedVal == 3) k2 = n / 2;
        else {
            k2 = (int)((1LL * n * p2permille + 500) / 1000);
        }
        k2 = max(0, min(n, k2));

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

    return 0;
}