Problem: cf2163_d1
Difficulty: medium
Categories: greedy, data structures, search
Time limit: 3000 ms, memory limit: 256 MB

This is the Easy version of the problem. The difference between the versions is that in this version, you can make at most $\max(300, \lceil\frac{n}{2}\rceil+2)$ queries. 

This problem is interactive.

There is a permutation$^{\text{∗}}$ $p$ of the integers from $0$ to $n-1$ hidden from you. Additionally, you are given $q$ ranges $[l_1, r_1], [l_2, r_2], \ldots, [l_q, r_q]$ where $1 \le l_i \le r_i \le n$.

You need to discover the maximum $\operatorname{MEX}$ among the values of $p$ in the $q$ ranges that are given to you in the input. Formally, you must discover the value of $\max _{i=1}^q {\operatorname{MEX}([p_{l_i}, p_{l_i+1}, \ldots, p_{r_i}])}$$^{\text{†}}$. To do this, you may make at most $\max(300, \lceil\frac{n}{2}\rceil+2)$ queries of the following form:

* Choose any two integers $1 \le l \le r \le n$ and you will receive the value $\operatorname{MEX}([p_{l}, p_{l+1}, \ldots, p_{r}])$.

$^{\text{∗}}$A permutation of the integers from $0$ to $n-1$ is a sequence of $n$ elements where every integer from $0$ to $n-1$ appears exactly once. For example, the sequence $[0, 3, 1, 2]$ is a permutation, but the sequence $[0, 0, 2, 1]$ is not.

$^{\text{†}}$The $\operatorname{MEX}$ of a sequence is defined as the smallest non-negative integer that does not appear in that sequence. For example, $\operatorname{MEX}([0, 0, 1, 3]) = 2$ and $\operatorname{MEX}([1, 2, 2]) = 0.$

Input

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

The first line of each test case contains exactly two integers $n$ and $q$ ($4 \le n \le 10^4$, $1 \le q \le 3 \cdot 10^5$) — the size of the permutation and the number of ranges, respectively.

The $i$-th of the next $q$ lines contains two integers $l_i, r_i$ ($1 \le l_i \le r_i \le n$).

It is guaranteed that the sum of $n$ and $q$ do not exceed $10^4$ and $3 \cdot 10^5$ respectively over all test cases.

Additional Constraint: it is guaranteed that no range is repeated in the same test case.

Interaction

To ask a query, output a line in the following format (without the quotes):

* "? $l$ $r$" ($1 \le l \le r \le n$)

The jury will return a single integer, the value of $\operatorname{MEX}([p_{l}, p_{l+1}, \ldots, p_{r}])$.

When you have found the answer, output a single line in the following format:

* "! $x$" ($0 \le x \le n$).

After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query.

The interactor is not adaptive, meaning that the values of the permutation are known before the participant asks the queries.

If your program makes more than $\max(300, \lceil\frac{n}{2}\rceil+2)$ queries, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get the Idleness Limit Exceeded verdict. 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

```
3
4 3
1 2
2 4
1 3

2

0

1

4

6 6
1 2
2 4
3 3
4 6
5 5
6 6

6

1

2

4 4
1 1
2 2
3 3
4 4
```

Output

```
? 1 3

? 4 4

? 1 1

? 1 4

! 2

? 1 6

? 3 3

? 2 4

! 2

! 1
```

Note

In the first example, the hidden permutation is $p = [0, 3, 1, 2]$ and the ranges are $[1, 2], [2, 4], [1, 3]$. The third range is optimal, as $\operatorname{MEX}([p_1, p_2, p_3]) = \operatorname{MEX}([0, 3, 1]) = 2$, which is maximum.

In our first query, we ask about $l = 1, r = 3$ and the judge gives us the value $\operatorname{MEX}([p_1, p_2, p_3]) = 2$. In the second query, we ask about $l = 4, r = 4$ and the judge gives us the value $\operatorname{MEX}([p_4]) = 0$. Likewise, $\operatorname{MEX}([p_1]) = 1$ and $\operatorname{MEX}([p_1, p_2, p_3, p_4]) = 4$.

Somehow, we figure out that the answer we are looking for is $2$.

In the second example, $p = [3, 5, 0, 1, 4, 2]$.

In the third example, $p = [0, 1, 2, 3]$.

Note that this is just an explanation of the way the interaction works and does not show any strategy to solve the problem.

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 long long hard_cap = 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());
}

static int mex_subarray(const vector<int>& p, int l, int r, vector<char>& used) {
    int len = r - l + 1;
    for (int i = 0; i <= len + 1; i++) used[i] = 0;
    for (int i = l; i <= r; i++) {
        int v = p[i];
        if (v <= len + 1) used[v] = 1;
    }
    for (int i = 0; ; i++) if (!used[i]) return i;
}

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

    int t = inf.readInt(1, 100, "t");

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

    for (int tc = 0; tc < t; tc++) {
        int n = inf.readInt(4, 10000, "n");
        int q = inf.readInt(1, 300000, "q");

        vector<pair<int,int>> ranges(q);
        for (int i = 0; i < q; i++) {
            int l = inf.readInt(1, n, "l");
            int r = inf.readInt(l, n, "r");
            ranges[i] = {l-1, r-1};
        }

        vector<int> p(n);
        for (int i = 0; i < n; i++) {
            p[i] = inf.readInt(0, n-1, "p[i]");
        }

        long long B = max(300LL, (long long)((n + 1) / 2) + 2);
        query_limit += B;
        hard_cap = max(hard_cap, max(200000LL, 10 * B));

        log_metrics();

        cout << n << " " << q << endl;
        for (auto &rg : ranges) {
            cout << (rg.first + 1) << " " << (rg.second + 1) << endl;
        }
        cout.flush();

        vector<char> used(n + 5);
        int correct = 0;
        for (auto &rg : ranges) {
            correct = max(correct, mex_subarray(p, rg.first, rg.second, used));
        }

        while (true) {
            if (queries > hard_cap) {
                finish(_pe, "Hard cap exceeded");
            }

            string tok;
            if (!(cin >> tok)) {
                finish(_pe, "Unexpected EOF from contestant");
            }

            if (tok == "?") {
                int l, r;
                if (!(cin >> l >> r)) {
                    finish(_pe, "Malformed query");
                }
                l--; r--;
                if (l < 0 || r < l || r >= n) {
                    finish(_wa, "Query out of bounds");
                }
                int ans = mex_subarray(p, l, r, used);
                queries++;
                cout << ans << endl;
                cout.flush();
                if ((queries & 7) == 0) log_metrics();
            } else if (tok == "!") {
                int x;
                if (!(cin >> x)) {
                    finish(_pe, "Malformed answer");
                }
                if (x != correct) {
                    finish(_wa, "Wrong answer");
                }
                break;
            } else {
                int l = stoi(tok);
                int r;
                if (!(cin >> r)) {
                    finish(_pe, "Malformed query");
                }
                l--; r--;
                if (l < 0 || r < l || r >= n) {
                    finish(_wa, "Query out of bounds");
                }
                int ans = mex_subarray(p, l, r, used);
                queries++;
                cout << ans << endl;
                cout.flush();
                if ((queries & 7) == 0) log_metrics();
            }
        }
    }

    finish(_ok, "OK");
    return 0;
}


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 long long MAX_SUM_N = 10000;
    const long long MAX_SUM_Q = 300000;

    
    int t = -1;
    int seed = atoi(argv[1]);
    
    struct TestCase {
        int n, q;
    };
    vector<TestCase> cases;

    
    if (seed == 1) {
        
        t = 3;
        cases = {{4, 1}, {4, 10}, {10, 20}};
    } else if (seed == 2) {
        
        t = 1;
        cases = {{10000, 300000}};
    } else if (seed == 3) {
        
        t = 100; 
        
    } else {
        
        if (rnd.next(2) == 0) t = rnd.next(1, 10);
        else t = rnd.next(10, 100);
    }

    
    if (cases.empty()) {
        long long budget_n = MAX_SUM_N;
        long long budget_q = MAX_SUM_Q;
        
        
        if (budget_n < t * 4LL) budget_n = t * 4LL; 
        
        
        
        vector<long long> ns = rnd.partition(t, budget_n, 4LL);
        vector<long long> qs = rnd.partition(t, budget_q, 1LL);

        for (int i = 0; i < t; i++) {
            long long n_i = ns[i];
            long long q_i = qs[i];
            
            long long max_ranges = n_i * (n_i + 1) / 2;
            if (q_i > max_ranges) q_i = max_ranges;
            cases.push_back({(int)n_i, (int)q_i});
        }
    }

    println(t);
    for (const auto& tc : cases) {
        int n = tc.n;
        int q = tc.q;
        println(n, q);

        long long max_ranges = 1LL * n * (n + 1) / 2;
        vector<pair<int, int>> ranges;
        
        
        
        if (max_ranges <= 200000) {
            vector<pair<int, int>> all;
            all.reserve(max_ranges);
            for (int l = 1; l <= n; l++) {
                for (int r = l; r <= n; r++) {
                    all.push_back({l, r});
                }
            }
            shuffle(all.begin(), all.end());
            for (int i = 0; i < q; i++) {
                ranges.push_back(all[i]);
            }
        } else {
            
            set<pair<int, int>> distinct_ranges;
            while ((int)distinct_ranges.size() < q) {
                int l = rnd.next(1, n);
                int r = rnd.next(1, n);
                if (l > r) swap(l, r);
                distinct_ranges.insert({l, r});
            }
            ranges.assign(distinct_ranges.begin(), distinct_ranges.end());
            shuffle(ranges.begin(), ranges.end());
        }

        for (auto p : ranges) {
            println(p.first, p.second);
        }

        if (mode == "non") {
            vector<int> p(n);
            iota(p.begin(), p.end(), 0);
            shuffle(p.begin(), p.end());
            println(p);
        }
        
    }
    return 0;
}