Problem: cf1713_d
Difficulty: medium
Categories: greedy, search
Time limit: 2000 ms, memory limit: 256 MB

This is an interactive problem.

There was a tournament consisting of $2^n$ contestants. The $1$-st contestant competed with the $2$-nd, the $3$-rd competed with the $4$-th, and so on. After that, the winner of the first match competed with the winner of second match, etc. The tournament ended when there was only one contestant left, who was declared the winner of the tournament. Such a tournament scheme is known as the single-elimination tournament.

You don't know the results, but you want to find the winner of the tournament. In one query, you select two integers $a$ and $b$, which are the indices of two contestants. The jury will return $1$ if $a$ won more matches than $b$, $2$ if $b$ won more matches than $a$, or $0$ if their number of wins was equal.

Find the winner in no more than $\left \lceil \frac{1}{3} \cdot 2^{n + 1} \right \rceil$ queries. Here $\lceil x \rceil$ denotes the value of $x$ rounded up to the nearest integer.

Note that the tournament is long over, meaning that the results are fixed and do not depend on your queries.

Input

The first line contains a single integer $t$ ($1 \leq t \leq 2^{14}$) — the number of test cases.

The only line of input contains a single integer $n$ ($1 \leq n \leq 17$).

It is guaranteed that the sum of $2^n$ over all test cases does not exceed $2^{17}$.

Interaction

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

To make a query, output "? a b" ($1 \leq a, b \leq 2^n$) without quotes. Afterwards, you should read one single integer — the answer for your query. You can make at most $\left \lceil \frac{1}{3} \cdot 2^{n + 1} \right \rceil$ such queries in each test case.

If you receive the integer $-1$ instead of an answer or a valid value of $n$, it means your program has made an invalid query, has exceed the limit of queries, or has given incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

When you are ready to give the final answer, output "! x" ($1 \leq x \leq 2^n$) without quotes — the winner of the tournament. Giving this answer does not count towards the limit of queries. After solving a test case, your program should move to the next one immediately. After solving all test cases, your program should be terminated immediately.

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

Example

Input

```
1
3

2

0

2
```

Output

```
? 1 4

? 1 6

? 5 7

! 7
```

Note

The tournament in the first test case is shown below. The number of wins is $[1,0,0,2,0,1,3,0]$.

 ![](https://espresso.codeforces.com/676d8080959faeceabb866efe8792b034ebdadf5.png) In this example, the winner is the $7$-th contestant.

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

using namespace std;


static long long global_queries = 0;
static long long global_limit = 0;




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


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

int main(int argc, char* argv[]) {
    
    setName("Interactive judge for CF1713D (Non-Adaptive)");
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    int t = inf.readInt();

    
    cout << t << endl;

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

        
        
        
        vector<int> strength(m + 1);
        for (int i = 1; i <= m; ++i) {
            strength[i] = inf.readInt();
        }

        
        
        
        vector<int> wins(m + 1, 0);
        vector<int> current_round(m);
        iota(current_round.begin(), current_round.end(), 1); 

        for (int r = 0; r < n; ++r) {
            vector<int> next_round;
            
            for (size_t i = 0; i < current_round.size(); i += 2) {
                int u = current_round[i];
                int v = current_round[i+1];
                
                int winner = (strength[u] > strength[v]) ? u : v;
                wins[winner]++;
                next_round.push_back(winner);
            }
            current_round = next_round;
        }
        int tournament_winner = current_round[0];

        
        
        
        long long num = 1LL << (n + 1);
        long long local_limit = (num + 2) / 3;
        global_limit += local_limit;
        
        
        
        log_metrics();

        
        cout << n << endl;

        int local_queries = 0;

        
        while (true) {
            string token;
            
            if (!(cin >> token)) {
                safe_finish(_wa, "Unexpected EOF while reading token");
            }

            if (token == "!") {
                
                int ans;
                if (!(cin >> ans)) {
                    safe_finish(_wa, "Expected answer after '!'");
                }
                
                if (ans == tournament_winner) {
                    break; 
                } else {
                    
                    cout << -1 << endl;
                    safe_finish(_wa, format("Wrong answer: expected %d, got %d", tournament_winner, ans));
                }
            } else {
                
                int a, b;
                if (token == "?") {
                    if (!(cin >> a)) safe_finish(_wa, "Expected 1st index after '?'");
                } else {
                    
                    try {
                        a = stoi(token);
                    } catch (...) {
                        safe_finish(_pe, format("Invalid token '%s'", token.c_str()));
                    }
                }
                if (!(cin >> b)) safe_finish(_wa, "Expected 2nd index");

                
                if (a < 1 || a > m || b < 1 || b > m) {
                    cout << -1 << endl;
                    safe_finish(_wa, format("Index out of range: %d %d", a, b));
                }

                local_queries++;
                global_queries++;
                
                
                if (local_queries > local_limit) {
                    cout << -1 << endl;
                    safe_finish(_wa, format("Query limit exceeded (used %d, limit %lld)", local_queries, local_limit));
                }

                
                
                
                
                int res = 0;
                if (wins[a] > wins[b]) res = 1;
                else if (wins[b] > wins[a]) res = 2;
                else res = 0; 

                cout << res << endl;
            }
        }
    }

    safe_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;


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


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


mt19937 rng(58723);

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) {
        
        int n = inf.readInt();
        int num_players = 1 << n;
        
        
        long long current_limit = ((1LL << (n + 1)) + 2) / 3;
        total_query_limit += current_limit;
        
        
        log_metrics();

        
        cout << n << endl;

        
        
        vector<int> p(num_players);
        string token = inf.readToken();
        
        if (token == "adaptive") {
            int target = inf.readInt(); 
            int target_idx = target - 1;
            
            
            
            p[target_idx] = num_players;
            
            vector<int> others;
            others.reserve(num_players - 1);
            for (int v = 1; v < num_players; ++v) {
                others.push_back(v);
            }
            
            
            shuffle(others.begin(), others.end(), rng);
            
            int ptr = 0;
            for (int j = 0; j < num_players; ++j) {
                if (j == target_idx) continue;
                p[j] = others[ptr++];
            }
        } else {
            
            
            p[0] = stoi(token);
            for (int j = 1; j < num_players; ++j) {
                p[j] = inf.readInt();
            }
        }

        
        
        
        vector<int> wins(num_players, 0);
        vector<int> current_round(num_players);
        iota(current_round.begin(), current_round.end(), 0); 

        while (current_round.size() > 1) {
            vector<int> next_round;
            for (size_t k = 0; k < current_round.size(); k += 2) {
                int u = current_round[k];
                int v = current_round[k+1];
                
                if (p[u] > p[v]) {
                    wins[u]++;
                    next_round.push_back(u);
                } else {
                    wins[v]++;
                    next_round.push_back(v);
                }
            }
            current_round = next_round;
        }
        
        int winner_idx = current_round[0];
        
        
        int queries_this_case = 0;
        bool case_active = true;
        
        while (case_active) {
            string type;
            if (!(cin >> type)) {
                finish_verdict(_wa, "Unexpected EOF from solution");
            }
            
            if (type == "!") {
                int ans;
                if (!(cin >> ans)) {
                    finish_verdict(_wa, "Unexpected EOF reading answer");
                }
                
                if (ans != winner_idx + 1) {
                    finish_verdict(_wa, format("Wrong answer on case %d. Expected %d, found %d", i + 1, winner_idx + 1, ans));
                }
                
                case_active = false; 
            } 
            else if (type == "?") {
                int u, v;
                if (!(cin >> u >> v)) {
                    finish_verdict(_wa, "Unexpected EOF reading query indices");
                }
                
                queries_this_case++;
                total_queries++;
                
                
                if (queries_this_case > current_limit) {
                    
                    cout << -1 << endl;
                    finish_verdict(_wa, format("Query limit exceeded on case %d", i + 1));
                }
                
                
                if (u < 1 || u > num_players || v < 1 || v > num_players) {
                    cout << -1 << endl;
                    finish_verdict(_wa, format("Invalid indices on case %d: %d %d", i + 1, u, v));
                }
                
                int resp;
                if (u == v) {
                    resp = 0; 
                } else {
                    int w1 = wins[u - 1];
                    int w2 = wins[v - 1];
                    
                    if (w1 > w2) resp = 1;
                    else if (w2 > w1) resp = 2;
                    else resp = 0;
                }
                
                cout << resp << endl;
            } 
            else {
                finish_verdict(_wa, format("Invalid query type '%s' on case %d", type.c_str(), i + 1));
            }
            
            
            if (total_queries % 100 == 0) log_metrics();
        }
    }

    finish_verdict(_ok, "Passed all tests");
    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");
    
    
    
    const int BUDGET = 131072; 
    
    const int MAX_T = 16384; 

    vector<int> ns;
    int seed = atoi(argv[1]); 

    
    if (seed == 1) {
        
        ns.push_back(17);
    } 
    else if (seed == 2) {
        
        
        
        ns.assign(MAX_T, 1);
    }
    else if (seed == 3) {
        
        
        for (int i = 1; i <= 16; ++i) {
            ns.push_back(i);
        }
    }
    else {
        
        
        int current_sum = 0;
        int dtype = rnd.next(1, 3);
        
        if (dtype == 1) {
            
            while (current_sum < BUDGET && ns.size() < MAX_T) {
                int rem = BUDGET - current_sum;
                int max_n = 0;
                while ((1 << (max_n + 1)) <= rem) max_n++;
                max_n = min(max_n, 17);
                if (max_n == 0) break;
                
                
                int n = rnd.next(max(1, max_n - 4), max_n);
                ns.push_back(n);
                current_sum += (1 << n);
            }
        }
        else if (dtype == 2) {
            
            while (current_sum < BUDGET && ns.size() < MAX_T) {
                int rem = BUDGET - current_sum;
                int max_n = 0;
                while ((1 << (max_n + 1)) <= rem) max_n++;
                max_n = min(max_n, 17);
                if (max_n == 0) break;
                
                
                int n = rnd.next(1, min(max_n, 10));
                
                if (rnd.next(10) == 0) n = rnd.next(1, max_n);
                
                ns.push_back(n);
                current_sum += (1 << n);
            }
        }
        else {
            
            while (current_sum < BUDGET && ns.size() < MAX_T) {
                int rem = BUDGET - current_sum;
                int max_n = 0;
                while ((1 << (max_n + 1)) <= rem) max_n++;
                max_n = min(max_n, 17);
                if (max_n == 0) break;
                
                int n = rnd.next(1, max_n);
                ns.push_back(n);
                current_sum += (1 << n);
            }
        }
        
        shuffle(ns.begin(), ns.end());
    }

    
    println(ns.size());

    for (int n : ns) {
        println(n);
        int count = 1 << n;
        
        if (mode == "non") {
            
            
            
            vector<int> p(count);
            iota(p.begin(), p.end(), 1);
            shuffle(p.begin(), p.end());
            println(p);
        } 
        else if (mode == "adp") {
            
            
            
            
            int target = rnd.next(1, count);
            cout << "adaptive " << target << endl;
        }
    }

    return 0;
}