Problem: cf1780_d
Difficulty: medium
Categories: bit
Time limit: 1000 ms, memory limit: 256 MB

This is an interactive problem.

Kira has a hidden positive integer $n$, and Hayato needs to guess it.

Initially, Kira gives Hayato the value $\mathrm{cnt}$ — the number of unit bits in the binary notation of $n$. To guess $n$, Hayato can only do operations of one kind: choose an integer $x$ and subtract it from $n$. Note that after each operation, the number $n$ changes. Kira doesn't like bad requests, so if Hayato tries to subtract a number $x$ greater than $n$, he will lose to Kira. After each operation, Kira gives Hayato the updated value $\mathrm{cnt}$ — the number of unit bits in the binary notation of the updated value of $n$.

Kira doesn't have much patience, so Hayato must guess the original value of $n$ after no more than $30$ operations.

Since Hayato is in elementary school, he asks for your help. Write a program that guesses the number $n$. Kira is an honest person, so he chooses the initial number $n$ before all operations and does not change it afterward.

Input

The input data contains several test cases. The first line contains one integer $t$ ($1 \le t \le 500$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains the number $\mathrm{cnt}$ — the initial number of unit bits in the binary notation $n$.

The hidden integer $n$ satisfies the following constraint: $1 \le n \le 10^9$.

Interaction

To guess $n$, you can perform the operation at most $30$ times. To do that, print a line with the following format: "- x" ($1 \le x \le 10^9$).

After this operation, the number $x$ is subtracted from $n$, and therefore $n$ is changed. If the number $x$ is greater than the current value of $n$, then the request is considered invalid.

After the operation read a line containing a single non-negative integer $\mathrm{cnt}$ — the number of unit bits in the binary notation of the current $n$ after the operation.

When you know the initial value of $n$, print one line in the following format: "! n" ($1 \le n \le 10^9$).

After that, move on to the next test case, or terminate the program if there are none.

If your program performs more than $30$ operations for one test case, subtracts a number $x$ greater than $n$, or makes an incorrect request, then response to the request will be -1, after receiving such response, your program must exit immediately to receive the Wrong Answer verdict. Otherwise, you can get any other verdict.

After printing a query or the answer, do not forget to output the 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 documentation for other languages.

Example

Input

```
3

1

0

1

1

0

2

1

0
```

Output

```
- 1

! 1

- 1

- 1

! 2

- 2

- 1

! 3
```

Note

For example, the number of unit bits in number $6$ is $2$, because binary notation of $6$ is $110$. For $13$ the number of unit bits is $3$, because $13_{10} = 1101_2$.

In the first test case, $n = 1$, so the input is the number $1$. After subtracting one from $n$, it becomes zero, so the number of unit bits in it is $0$.

In the third test case, $n = 3$, which in binary representation looks like $3_{10} = 11_2$, so the input is the number of ones, that is $2$. After subtracting $2$, $n = 1$, so the number of unit bits is now $1$. After subtracting one from $n$, it becomes equal to zero.

Note that the blank lines in the input and output examples are shown for clarity and are not present in the actual interaction.

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 << endl;
        tout << "query_limit=" << 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();
    
    
    query_limit = t * 30LL;
    log_metrics(); 

    
    cout << t << endl;

    for (int i = 0; i < t; i++) {
        
        int initial_n = inf.readInt();
        int current_n = initial_n;
        
        
        cout << __builtin_popcount(current_n) << endl;

        int local_queries = 0;
        
        while (true) {
            string token;
            
            if (!(cin >> token)) {
                finish(_wa, "Unexpected EOF: expected query or answer");
            }

            if (token == "!") {
                
                int guess;
                if (!(cin >> guess)) {
                    finish(_wa, "Unexpected EOF: expected number after '!'");
                }
                
                if (guess == initial_n) {
                    
                    break;
                } else {
                    finish(_wa, format("Wrong Answer: guessed %d, expected %d", guess, initial_n));
                }
            } else {
                
                long long x = -1;
                bool valid_parse = false;

                
                if (token == "-" || token == "?") {
                    if (cin >> x) valid_parse = true;
                } else {
                    
                    try {
                        size_t idx;
                        x = stoll(token, &idx);
                        if (idx == token.size()) valid_parse = true;
                    } catch (...) {
                        valid_parse = false;
                    }
                }

                if (!valid_parse) {
                    finish(_wa, format("Invalid token or format: '%s'", token.c_str()));
                }

                
                if (local_queries >= 30) {
                    
                    cout << -1 << endl;
                    finish(_wa, "Query limit exceeded for test case (max 30)");
                }

                
                if (x < 1 || x > current_n) {
                    
                    cout << -1 << endl;
                    finish(_wa, format("Invalid query: x=%lld out of range [1, current_n=%d]", x, current_n));
                }

                
                current_n -= x;
                local_queries++;
                queries++;
                
                
                if (queries % 10 == 0) log_metrics();

                
                cout << __builtin_popcount(current_n) << endl;
            }
        }
    }

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


Adaptive interactor (C++):













#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;


static long long max_queries = 0;
static const long long QUERY_LIMIT = 30; 
static const long long HARD_CAP = 200000; 
static long long total_ops = 0;


void log_metrics() {
    try {
        tout << "queries=" << max_queries << endl;
        tout << "query_limit=" << QUERY_LIMIT << endl;
    } catch (...) {}
}


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


struct State {
    int original;
    int current;
};


inline int count_bits(int n) {
    return __builtin_popcount(n);
}


vector<int> generate_candidates(int L, int R) {
    set<int> unique_cands;
    
    
    auto add = [&](long long x) {
        if (x >= L && x <= R && unique_cands.size() < 2500) {
            unique_cands.insert((int)x);
        }
    };

    
    for (int k = 0; k < 30; ++k) add(1LL << k);
    
    for (int k = 1; k <= 30; ++k) add((1LL << k) - 1);
    
    for (int i = 0; i < 500; ++i) add(rnd.next(L, R));
    
    for (int i = 0; i < 100; ++i) {
        int k1 = rnd.next(0, 29);
        int k2 = rnd.next(0, 29);
        add((1LL << k1) | (1LL << k2));
    }
    
    for (int i = 0; i < 100; ++i) {
        int k = rnd.next(10, 29);
        long long v = (1LL << k) - 1;
        int rem = rnd.next(1, 3);
        for (int j = 0; j < rem; ++j) {
            int b = rnd.next(0, k - 1);
            if ((v >> b) & 1) v ^= (1LL << b);
        }
        add(v);
    }
    
    for(int i=0; i<50; ++i) {
        long long v = 0;
        int start = rnd.next(0, 1);
        int len = rnd.next(1, 29);
        for (int j = 0; j <= len; j++) {
            if (j % 2 == start) v |= (1LL << j);
        }
        add(v);
    }
    
    
    while (unique_cands.size() < 2500) {
        int v = rnd.next(L, R);
        add(v);
        
        if (unique_cands.size() >= (size_t)(R - L + 1)) break; 
    }
    
    return vector<int>(unique_cands.begin(), unique_cands.end());
}


string read_meaningful_line() {
    while (!inf.eof()) {
        string s = inf.readLine();
        string trimmed;
        for (char c : s) if (!isspace(c)) trimmed += c;
        if (!trimmed.empty()) return s;
    }
    return "";
}

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

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

    for (int tc = 0; tc < t; ++tc) {
        
        string line = read_meaningful_line();
        if (line.empty()) break; 

        stringstream ss(line);
        vector<long long> nums;
        long long val;
        while (ss >> val) nums.push_back(val);

        vector<State> candidates;
        if (nums.size() == 1) {
            
            candidates.push_back({(int)nums[0], (int)nums[0]});
        } else if (nums.size() >= 2) {
            
            int L = (int)nums[0];
            int R = (int)nums[1];
            vector<int> vals = generate_candidates(L, R);
            for (int v : vals) candidates.push_back({v, v});
        } else {
            
            candidates.push_back({1, 1});
        }

        
        
        vector<vector<State>> buckets(32);
        for (auto& c : candidates) buckets[count_bits(c.current)].push_back(c);

        int best_cnt = -1;
        size_t best_sz = 0;
        for (int i = 0; i < 32; ++i) {
            if (buckets[i].size() > best_sz) {
                best_sz = buckets[i].size();
                best_cnt = i;
            }
        }
        
        
        candidates = buckets[best_cnt];
        cout << best_cnt << endl;

        long long current_queries = 0;
        
        
        while (true) {
            
            if (total_ops++ > HARD_CAP) {
                finish(_pe, "Total operations limit exceeded");
            }

            
            string type;
            
            char c; 
            if (!(cin >> c)) {
                 finish(_pe, "Unexpected EOF (solution terminated early?)");
            }
            cin.putback(c);
            cin >> type;

            if (type == "!") {
                int guess;
                cin >> guess;
                
                
                bool all_match = true;
                int mismatch_ex = -1;
                for (auto& st : candidates) {
                    if (st.original != guess) {
                        all_match = false;
                        mismatch_ex = st.original;
                        break;
                    }
                }
                
                if (!all_match) {
                    finish(_wa, format("Wrong answer. Guess %d, but input consistent with %d", guess, mismatch_ex));
                }
                
                
                break; 

            } else if (type == "-") {
                long long x_long;
                cin >> x_long;
                int x = (int)x_long;

                current_queries++;
                max_queries = max(max_queries, current_queries);
                
                
                if (current_queries % 5 == 0) log_metrics();

                if (current_queries > QUERY_LIMIT) {
                    cout << -1 << endl;
                    finish(_wa, "Query limit exceeded (30 per testcase)");
                }
                if (x < 1 || x > 1000000000) {
                    cout << -1 << endl;
                    finish(_wa, "Invalid query range (x < 1 or x > 10^9)");
                }

                
                vector<State> valid;
                valid.reserve(candidates.size());
                for (auto& st : candidates) {
                    if (st.current >= x) valid.push_back(st);
                }

                if (valid.empty()) {
                    
                    cout << -1 << endl;
                    finish(_wa, "Subtracted x greater than current n for all feasible candidates");
                }

                
                for (auto& b : buckets) b.clear();
                for (auto& st : valid) {
                    int nxt = st.current - x;
                    buckets[count_bits(nxt)].push_back({st.original, nxt});
                }

                
                int best_res = -1;
                size_t best_next_sz = 0;
                for (int i = 0; i < 32; ++i) {
                    if (buckets[i].size() > best_next_sz) {
                        best_next_sz = buckets[i].size();
                        best_res = i;
                    }
                }
                
                candidates = buckets[best_res];
                cout << best_res << endl;
            } else {
                finish(_pe, "Invalid protocol: expected '-' or '!'");
            }
        }
    }

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


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 t = opt<int>("t", rnd.next(10, 50));

    cout << t << endl;

    for (int i = 0; i < t; ++i) {
        if (mode == "adp") {
            
            
            
            cout << "1 1000000000" << endl;
        } else {
            
            
            
            int type = rnd.next(1, 10);
            int n = 1;

            if (type == 1) {
                
                n = rnd.next(1, 1000000000);
            } else if (type == 2) {
                
                n = rnd.next(1, 100);
            } else if (type == 3) {
                
                n = rnd.next(1000000000 - 500, 1000000000);
            } else if (type == 4) {
                
                int k = rnd.next(0, 29);
                n = (1 << k);
            } else if (type == 5) {
                
                int k = rnd.next(1, 29);
                n = (1 << k) - 1;
            } else if (type == 6) {
                
                int k1 = rnd.next(0, 29);
                int k2 = rnd.next(0, 29);
                while (k1 == k2) k2 = rnd.next(0, 29);
                n = (1 << k1) | (1 << k2);
            } else if (type == 7) {
                
                int k = rnd.next(10, 29);
                n = (1 << k) - 1;
                int rem = rnd.next(1, 3);
                for (int j = 0; j < rem; ++j) {
                    int b = rnd.next(0, k - 1);
                    if ((n >> b) & 1) n ^= (1 << b);
                }
            } else if (type == 8) {
                
                n = 0;
                int start = rnd.next(0, 1);
                int len = rnd.next(1, 29);
                for (int j = 0; j <= len; j++) {
                    if (j % 2 == start) n |= (1 << j);
                }
            } else if (type == 9) {
                
                n = rnd.next(536870912, 1000000000);
            } else {
                
                n = rnd.next(1, 1000000000);
            }

            
            if (n < 1) n = 1;
            if (n > 1000000000) n = 1000000000;

            cout << n << endl;
        }
    }

    return 0;
}