All problems in this contest share the same statement, the only difference is the test your solution runs on. For further information on scoring please refer to "Scoring" section of the statement.

This is an interactive problem.

Imagine you are a treasure hunter, a very skillful one. One day you came across an ancient map which could help you to become rich. The map shows multiple forestry roads, and at each junction there is a treasure. So, you start your journey hoping to retrieve all the hidden treasures, but you don't know yet that there is a wicked wizard standing against you and craving to tangle up these roads and impede your achievements.

The treasure map is represented as an undirected graph in which vertices correspond to junctions and edges correspond to roads. Your path begins at a certain fixed vertex with a label known to you. Every time you come to a vertex that you have not been to before, you dig up a treasure chest and put a flag in this vertex. At the initial vertex you'll find a treasure chest immediately and, consequently, you'll put a flag there immediately as well.

When you are standing at the junction you can see for each of the adjacent vertices its degree and if there is a flag there. There are no other things you can see from there. Besides, the power of the wicked wizard is so great that he is able to change the location of the roads and junctions on the map without changing the graph structure. Therefore, the sequence of the roads coming from the junction $v$ might be different each time you come in the junction $v$. However, keep in mind that the set of adjacent crossroads does not change, and you are well aware of previously dug treasures at each adjacent to $v$ vertex.

Your goal is to collect treasures from all vertices of the graph as fast as you can. Good luck in hunting!

Interaction

On the first line the interactor prints an integer $t$ ($1 \leq t \leq 5$) — the number of maps for which you need to solve the problem.

Then for each of the $t$ maps interactor firstly prints the graph description and after that interaction on this map starts. The map description is given in the following format.

The first line contains four integers $n, m, start, base\_move\_count$ ($2 \leq n \leq 300$, $1 \leq m \leq min(\frac{n(n-1)}{2}, 25n)$, $1 \leq start \leq n$, $1000 \leq base\_move\_count \leq 30000$) — the number of vertices and edges in the graph, the label of the vertex where you start the journey and some base move count, on which the score for the map depends. It's guaranteed that the jury has a solution traversing the map using not more than $base\_move\_count$ moves with high probability.

The next $m$ lines contain descriptions of the edges of the graph $u, v$ ($1 \leq u, v \leq n$, $u \neq v$). It is guaranteed that the graph is connected, does not contain multiple edges and the degrees of all vertices do not exceed 50.

You can find map descriptions for all tests in the archive 'map\_descriptions.zip' which you can find in any of the archives 'problem-X-materials.zip' in the section "Contest materials" — they are all the same.

After that the interaction begins. The interactor prints vertex descriptions in the following format:

R~d~deg\_1~flag\_1~deg\_2~flag\_2 \ldots deg\_d~flag\_d, where $d$ is the degree of the current vertex, $deg_i$ is the degree of the $i$-th vertex adjacent to the current one, and $flag_i$ (0 or 1) is an indicator if the $i$-th adjacent vertex contains a flag. The order of neighbors in the vertex description is chosen by the interactor uniformly at random independently each time. Please keep in mind that you are not given the actual labels of adajacent vertices.

In response to the description of the vertex you should print a single integer $i$ ($1 \leq i \leq d$), which means that you have chosen the vertex with description $deg_i~flag_i$. Remember to use the flush operation after each output. If your output is invalid, you will get a "Wrong Answer" verdict.

When you have visited all the vertices of the graph at least once, the interactor prints the string "AC" instead of the vertex description. If you use more than $2 \cdot base\_move\_count$ moves, the interactor prints the string "F". In both cases, you have to either start reading the description of the next map, or terminate the program, if it has been the last map in the test.

Below the example of interaction is presented. Note that the test from the example is not the same that your solution will be tested on.

```
|  |  |
| --- | --- |
| Interactor | Solution |
| 1 |  |
| 3 3 1 1000 |  |
| 1 2 |  |
| 2 3 |  |
| 3 1 |  |
| R 2 2 0 2 0 |  |
|  | 1 |
| R 2 2 0 2 1 |  |
|  | 2 |
| R 2 2 0 2 1 |  |
|  | 1 |
| AC |  |
```

Scoring

All problems of this contest share the same statement and differ only in the test. Each problem contains one test. Map descriptions for the test are available to look at in the archive.

Each test consists of several maps.

The solution score for the problem will be $0$ in case your solution for some map failed to get an answer "AC" or "F". If the solution correctly interacted with the interactor on all $t$ maps, the score for the task will be equal to the sum of the scores for each of the maps.

If you have successfully passed all vertices of a graph with $n$ vertices using $moves$ moves, the score for the map is calculated as follows. Denote

base\\_fraction=\frac{base\\_move\\_count + 1}{n},sol\\_fraction=\frac{moves+1}{n},c=\frac{90}{\sqrt{base\\_fraction - 1}}.

Then:

* if $moves \leq base\_move\_count$, you get $100-c \sqrt{sol\_fraction - 1}$ points.
* if $base\_move\_count < moves \leq 2 \cdot base\_move\_count$, you get $20 - \frac{10\cdot(moves + 1)}{base\_move\_count + 1}$ points.

If you use more than $2 \cdot base\_move\_count$ moves, you get 0 points for the map.

For each problem the solution with the highest score is chosen. Please note that the maximum is chosen for the entire test as a whole, not for each separate map.

The final result of the participant is the sum of points for each of the problems.

#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

static long long queries = 0;
static long long query_limit = 0;
static mt19937 rng(42);

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 read_choice_index() {
    string token;
    if (!(cin >> token)) {
        finish(_wa, "Unexpected EOF while reading move");
    }
    if (token.empty()) {
        finish(_wa, "Invalid move token");
    }
    for (char ch : token) {
        if (ch < '0' || ch > '9') {
            finish(_wa, "Invalid move token");
        }
    }
    try {
        size_t pos = 0;
        long long v = stoll(token, &pos);
        if (pos != token.size() || v > numeric_limits<int>::max()) {
            finish(_wa, "Invalid move token");
        }
        return (int)v;
    } catch (...) {
        finish(_wa, "Invalid move token");
    }
    return -1;
}

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

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = inf.readInt(1, 5, "t");
    cout << t << endl;
    cout.flush();

    for (int tc = 0; tc < t; ++tc) {
        int n = inf.readInt(2, 300, "n");
        int max_m = (int)min(1LL * n * (n - 1) / 2, 25LL * n);
        int m = inf.readInt(1, max_m, "m");
        int start = inf.readInt(1, n, "start");
        int base_move_count = inf.readInt(1000, 30000, "base_move_count");

        long long map_limit = 2LL * base_move_count;
        query_limit += map_limit;
        if (tc == 0) log_metrics();

        vector<vector<int>> adj(n + 1);
        vector<int> deg(n + 1, 0);
        vector<pair<int, int>> edges;
        edges.reserve(m);

        for (int i = 0; i < m; ++i) {
            int u = inf.readInt(1, n, "u");
            int v = inf.readInt(1, n, "v");
            adj[u].push_back(v);
            adj[v].push_back(u);
            deg[u]++;
            deg[v]++;
            edges.push_back({u, v});
        }

        cout << n << " " << m << " " << start << " " << base_move_count << endl;
        for (const auto& e : edges) {
            cout << e.first << " " << e.second << endl;
        }
        cout.flush();

        vector<char> visited(n + 1, 0);
        visited[start] = 1;
        int visited_count = 1;
        int cur = start;
        long long moves = 0;

        while (true) {
            if (visited_count == n) {
                cout << "AC" << endl;
                cout.flush();
                break;
            }
            if (moves > map_limit) {
                cout << "F" << endl;
                cout.flush();
                break;
            }

            vector<int> neighbors = adj[cur];
            shuffle(neighbors.begin(), neighbors.end(), rng);

            cout << "R " << neighbors.size();
            for (int v : neighbors) {
                cout << " " << deg[v] << " " << (visited[v] ? 1 : 0);
            }
            cout << endl;
            cout.flush();

            int choice = read_choice_index();
            if (choice < 1 || choice > (int)neighbors.size()) {
                finish(_wa, format("Invalid choice %d (expected 1..%d)", choice, (int)neighbors.size()));
            }

            cur = neighbors[choice - 1];
            moves++;
            queries++;
            if (queries % 200 == 0) log_metrics();

            if (!visited[cur]) {
                visited[cur] = 1;
                visited_count++;
            }
        }
    }

    finish(_ok, "All maps processed");
    return 0;
}

#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

static long long queries = 0;
static long long query_limit = 0;
static mt19937 rng(239);

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 read_choice_index() {
    string token;
    if (!(cin >> token)) {
        finish(_wa, "Unexpected EOF while reading move");
    }
    if (token.empty()) {
        finish(_wa, "Invalid move token");
    }
    for (char ch : token) {
        if (ch < '0' || ch > '9') {
            finish(_wa, "Invalid move token");
        }
    }
    try {
        size_t pos = 0;
        long long v = stoll(token, &pos);
        if (pos != token.size() || v > numeric_limits<int>::max()) {
            finish(_wa, "Invalid move token");
        }
        return (int)v;
    } catch (...) {
        finish(_wa, "Invalid move token");
    }
    return -1;
}

struct DisplayEntry {
    int v;
    int degree;
    int flag;
};

static void run_map(int n, int m, int start, int base_move_count, const vector<pair<int, int>>& edges) {
    vector<vector<int>> adj(n + 1);
    vector<int> deg(n + 1, 0);
    for (const auto& e : edges) {
        adj[e.first].push_back(e.second);
        adj[e.second].push_back(e.first);
        deg[e.first]++;
        deg[e.second]++;
    }

    cout << n << " " << m << " " << start << " " << base_move_count << endl;
    for (const auto& e : edges) {
        cout << e.first << " " << e.second << endl;
    }
    cout.flush();

    long long map_limit = 2LL * base_move_count;
    vector<char> visited(n + 1, 0);
    visited[start] = 1;
    int visited_count = 1;
    int cur = start;
    long long moves = 0;

    while (true) {
        if (visited_count == n) {
            cout << "AC" << endl;
            cout.flush();
            return;
        }
        if (moves > map_limit) {
            cout << "F" << endl;
            cout.flush();
            return;
        }

        const vector<int>& neighbors = adj[cur];
        int d = (int)neighbors.size();

        vector<DisplayEntry> display;
        display.reserve(d);
        for (int v : neighbors) {
            display.push_back({v, deg[v], visited[v] ? 1 : 0});
        }
        shuffle(display.begin(), display.end(), rng);

        cout << "R " << d;
        for (const auto& e : display) {
            cout << " " << e.degree << " " << e.flag;
        }
        cout << endl;
        cout.flush();

        int choice = read_choice_index();
        if (choice < 1 || choice > d) {
            finish(_wa, format("Invalid choice %d (expected 1..%d)", choice, d));
        }

        cur = display[choice - 1].v;
        if (!visited[cur]) {
            visited[cur] = 1;
            visited_count++;
        }

        moves++;
        queries++;
        if (queries % 200 == 0) log_metrics();
        if (queries > 2000000) finish(_pe, "Hard query cap exceeded");
    }
}

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

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = inf.readInt(1, 5, "t");
    cout << t << endl;
    cout.flush();

    for (int tc = 0; tc < t; ++tc) {
        int n = inf.readInt(2, 300, "n");
        int max_m = (int)min(1LL * n * (n - 1) / 2, 25LL * n);
        int m = inf.readInt(1, max_m, "m");
        int start = inf.readInt(1, n, "start");
        int base_move_count = inf.readInt(1000, 30000, "base_move_count");

        query_limit += 2LL * base_move_count;
        if (tc == 0) log_metrics();

        vector<pair<int, int>> edges;
        edges.reserve(m);
        for (int i = 0; i < m; ++i) {
            int u = inf.readInt(1, n, "u");
            int v = inf.readInt(1, n, "v");
            edges.push_back({u, v});
        }

        run_map(n, m, start, base_move_count, edges);
    }

    finish(_ok, "All maps processed");
    return 0;
}

#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

vector<pair<int, int>> generate_graph(int n, int max_deg, string type) {
    vector<pair<int, int>> edges;
    vector<int> deg(n + 1, 0);
    vector<set<int>> adj(n + 1);

    auto add_edge = [&](int u, int v) {
        if (u == v) return false;
        if (u > v) swap(u, v);
        if (adj[u].count(v)) return false;
        if (deg[u] >= max_deg || deg[v] >= max_deg) return false;
        adj[u].insert(v);
        adj[v].insert(u);
        edges.push_back({u, v});
        deg[u]++;
        deg[v]++;
        return true;
    };

    if (type == "line") {
        vector<int> p(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());
        for (int i = 0; i < n - 1; i++) {
            add_edge(p[i], p[i+1]);
        }
    } else if (type == "star") {
        vector<int> p(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());
        int center = p[0];
        int current = 1;

        while(deg[center] < max_deg && current < n) {
            add_edge(center, p[current]);
            current++;
        }

        for (int i = current; i < n; i++) {
            int tries = 0;
            while (tries < 100) {
                int target = p[rnd.next(0, i-1)];
                if (deg[target] < max_deg) {
                    add_edge(target, p[i]);
                    break;
                }
                tries++;
            }
        }
    } else {

        vector<int> p(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());

        for (int i = 1; i < n; i++) {
            int tries = 0;
            while (tries < 100) {
                int parent = p[rnd.next(0, i-1)];
                if (deg[parent] < max_deg) {
                    add_edge(parent, p[i]);
                    break;
                }
                tries++;
            }
        }

        long long max_allowed = 25LL * n;
        long long complete_graph = (long long)n * (n - 1) / 2;
        long long limit = min(max_allowed, complete_graph);

        int target_m;
        if (type == "dense") {

            target_m = (int)(limit * rnd.next(0.9, 1.0));
        } else {

            int upper = (int)min((long long)n * 5, limit);
            target_m = rnd.next(n - 1, upper);
        }

        int tries = 0;
        while ((int)edges.size() < target_m && tries < 2000) {
            int u = rnd.next(1, n);
            int v = rnd.next(1, n);
            if (!add_edge(u, v)) tries++;
            else tries = 0;
        }
    }
    return edges;
}

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

    string mode = opt<string>("mode", "non");

    int seed_val = (argc > 1) ? atoi(argv[1]) : 0;

    int t = 1;
    cout << t << endl;

    for (int k = 0; k < t; k++) {
        int n;
        string type;

        if (seed_val == 1) {
            n = 300;
            type = "dense";
        } else if (seed_val == 2) {
            n = 300;
            type = "line";
        } else if (seed_val == 3) {
            n = 300;
            type = "star";
        } else {

            n = rnd.next(250, 300);
            int r = rnd.next(1, 100);

            if (mode == "adp") {

                if (r <= 50) type = "line";
                else type = "random";
            } else {

                if (r <= 10) type = "line";
                else if (r <= 20) type = "star";
                else if (r <= 60) type = "random";
                else type = "dense";
            }
        }

        auto edges = generate_graph(n, 50, type);
        int m = edges.size();
        int start = rnd.next(1, n);

        int dfs_bound = 2 * m;
        int lower = max(1000, dfs_bound);
        int upper = 30000;

        if (lower > upper) lower = upper;

        int base_move_count = rnd.next(lower, min(upper, lower + 5000));

        cout << n << " " << m << " " << start << " " << base_move_count << endl;
        shuffle(edges.begin(), edges.end());
        for (auto p : edges) {
            cout << p.first << " " << p.second << endl;
        }
    }

    return 0;
}


B. Ithea Plays With Chtholly

This is an interactive problem. Refer to the Interaction section below for better understanding.

Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight.

![](https://espresso.codeforces.com/2dba15fc5c436b23cd2988708f9cd30993b18178.png)

Initially, Ithea puts *n* clear sheets of paper in a line. They are numbered from 1 to *n* from left to right.

This game will go on for *m* rounds. In each round, Ithea will give Chtholly an integer between 1 and *c*, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one).

Chtholly wins if, at any time, all the sheets are filled with a number and the *n* numbers are in non-decreasing order looking from left to right from sheet 1 to sheet *n*, and if after *m* rounds she still doesn't win, she loses the game.

Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number.

Input

The first line contains 3 integers *n*, *m* and *c* (![](https://espresso.codeforces.com/257604f27c6d773d788295abb7197dacaf673770.png), ![](https://espresso.codeforces.com/69d952fbb38d40c3c6e095688911e180261b4c6c.png) means ![](https://espresso.codeforces.com/8f85bbe2572c28c30e72b9446b520cbdeb7eabe7.png) rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process.

Interaction

In each round, your program needs to read one line containing a single integer *p**i* (1 ≤ *p**i* ≤ *c*), indicating the number given to Chtholly.

Your program should then output a line containing an integer between 1 and *n*, indicating the number of sheet to write down this number in.

After outputting each line, don't forget to flush the output. For example:

* fflush(stdout) in C/C++;
* System.out.flush() in Java;
* sys.stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.

If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game.

Example

Input

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

Output

```
1
2
2
```

Note

In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game.

Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected.

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

static long long queries = 0;
static long long query_limit = 0;
static int n, m, c;
static vector<int> sheets;

static void log_metrics() {

    if (query_limit == 0) return;
    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);

    n = inf.readInt();
    m = inf.readInt();
    c = inf.readInt();

    query_limit = m;

    log_metrics();

    sheets.assign(n, 0);

    cout << n << " " << m << " " << c << endl;

    for (int round = 1; round <= m; round++) {

        int p = inf.readInt();

        cout << p << endl;

        string token;
        if (!(cin >> token)) {
            finish(_wa, "Unexpected EOF or reading error from contestant output");
        }

        bool is_num = true;
        size_t start_idx = 0;
        if (token.size() > 0 && token[0] == '+') start_idx = 1;
        if (token.size() > 0 && token[0] == '-') is_num = false;
        if (start_idx >= token.size()) is_num = false;

        for (size_t i = start_idx; i < token.size(); i++) {
            if (!isdigit(token[i])) {
                is_num = false;
                break;
            }
        }

        if (!is_num) {
            finish(_wa, "Invalid format: expected integer index 1..N, got '" + token + "'");
        }

        long long idx = 0;
        try {
            idx = stoll(token);
        } catch (...) {
            finish(_wa, "Integer parse error or overflow: " + token);
        }

        queries++;

        if (queries % 10 == 0) log_metrics();

        if (idx < 1 || idx > n) {
            finish(_wa, format("Index out of bounds: %lld (must be 1..%d)", idx, n));
        }

        sheets[idx - 1] = p;

        bool filled = true;
        for (int x : sheets) {
            if (x == 0) {
                filled = false;
                break;
            }
        }

        if (filled) {
            bool sorted = true;
            for (int i = 0; i < n - 1; i++) {
                if (sheets[i] > sheets[i+1]) {
                    sorted = false;
                    break;
                }
            }
            if (sorted) {
                finish(_ok, format("Win after %d rounds", round));
            }
        }
    }

    finish(_wa, format("Failed to win after %d rounds", m));

    return 0;
}

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

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

void log_metrics() {

    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {

    }
}

template <typename... Args>
void finish(TResult verdict, const char* fmt, Args... args) {
    log_metrics();
    quitf(verdict, fmt, args...);
}

struct LCG {
    long long state;
    int mod_val;

    LCG(long long seed, int m) : state(seed), mod_val(m) {}

    int next() {

        state = (state * 48271LL + 239LL) % 2147483647LL;

        return 1 + (int)(state % mod_val);
    }
};

int main(int argc, char* argv[]) {

    registerInteraction(argc, argv);

    atexit(log_metrics);

    int n = inf.readInt();
    int m = inf.readInt();
    int c = inf.readInt();

    query_limit = m;

    log_metrics();

    cout << n << " " << m << " " << c << endl;

    vector<int> board(n + 1, 0);
    int filled_count = 0;

    LCG rng(n * 1009LL + m * 101LL + c + 1337LL, c);

    for (int round = 1; round <= m; round++) {

        int p;
        inf.skipBlanks();
        if (!inf.eof()) {

            p = inf.readInt();
        } else {

            p = rng.next();
        }

        queries = round;

        if (round % 100 == 0) log_metrics();

        cout << p << endl;

        int idx;
        if (!(cin >> idx)) {
            finish(_wa, "Contestant output stream closed unexpectedly or invalid format");
        }

        if (idx < 1 || idx > n) {
            finish(_wa, "Sheet index %d is out of bounds [1, %d]", idx, n);
        }

        if (board[idx] == 0) {
            filled_count++;
        }
        board[idx] = p;

        if (filled_count == n) {
            bool sorted = true;
            for (int i = 1; i < n; i++) {
                if (board[i] > board[i+1]) {
                    sorted = false;
                    break;
                }
            }
            if (sorted) {
                finish(_ok, "Contestant won in %d rounds", round);
            }
        }
    }

    finish(_wa, "Contestant failed to win within m=%d rounds", m);

    return 0;
}

#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 n = opt<int>("n", -1);
    int c = opt<int>("c", -1);
    int m = opt<int>("m", -1);

    int seed = atoi(argv[1]);

    if (n == -1) {
        if (seed == 1) n = 2;
        else if (seed == 2) n = 50;
        else if (seed == 3) n = 10;
        else if (seed == 4) n = 50;
        else n = rnd.next(2, 50);
    }

    if (c == -1) {
        if (seed == 1) c = 2;
        else if (seed == 2) c = 100;
        else if (seed == 3) c = 10;
        else if (seed == 4) c = 2;
        else c = rnd.next(2, 100);
    }

    if (m == -1) {
        m = n * c;
    }

    cout << n << " " << m << " " << c << endl;

    if (mode == "non") {

        int type = rnd.next(0, 4);

        for (int i = 0; i < m; i++) {
            int val;
            if (type == 0) {

                val = rnd.next(1, c);
            } else if (type == 1) {

                val = rnd.next(1, max(1, c / 2));
            } else if (type == 2) {

                int low = min(c, c / 2 + 1);
                val = rnd.next(low, c);
            } else if (type == 3 && c >= 4) {

                if (rnd.next(2))
                    val = rnd.next(1, c / 4);
                else
                    val = rnd.next(c - c / 4 + 1, c);
            } else {

                val = rnd.next(1, c);
            }
            cout << val << "\n";
        }
    } else {

    }

    return 0;
}


# This is an interactive problem.

Randias has an unknown hidden tree with $n$ vertices. The tree is either a chain or a star. Randias now needs to determine whether the tree is a chain or a star. He can ask a question in the following form, but no more than $\lceil \frac { n } { 2 } \rceil + 3$ times:

• Is there an edge between vertex $u$ and vertex $v$ ( $1 \leq u , v \leq n$ , $u \neq v$ )?

Randias needs to determine which of the two kinds the tree is. Help him to ask the questions and determine the answer.

A tree is called a chain if and only if there exists a permutation $p 1 , p 2 , \ldots , p _ { n }$ such that, for every $i$ $1 \leq i < n$ ), there is an edge $( p _ { i } , p _ { i + 1 } )$ in the tree. Here, a permutation of length $n$ is an array where each integer from 1 to $n$ appears exactly once.

A tree is called a star if and only if there exists a vertex $u$ such that, for every other vertex $v$ , there is an edge $( u , v )$ in the tree.

In this problem, the interactor is adaptive, which means that the secret tree is not fixed beforehand. Instead, the interactor can change the tree arbitrarily during the interaction. Nevertheless, at every moment, the tree will be consistent with all the answers you got.

# Input

Each test contains multiple test cases. The first line contains a single integer $t$ ( $1 \leq t \leq 2 5 0$ ) denoting the number of test cases.

For each test case, the first line contains one integer $n$ ( $4 \leq n \leq 1 0 0 0$ ) denoting the number of vertices.
It is guaranteed that the sum of $n$ over all test cases does not exceed 1000.

# Interaction Protocol

You can ask at most $\lceil \frac { n } { 2 } \rceil + 3$ questions in every test case. To ask a question, output a line of the form “? u $v ^ { \flat }$ ( $1 \leq u , v \leq n$ , $u \neq v$ ). Then you should read the response from standard input.

In response to the query, the interactor will output a line with a single integer: 1 if there is an edge between $u$ and $v$ in the tree, or 0 if there is no such edge.

To give your answer, print a line of the form “! 1” if you determined that the tree is a chain, or “ ! 2” if you determined that it is a star. The output of the answer is not counted towards the limit of $\lceil \frac { n } { 2 } \rceil + 3$ queries.

After printing the answer, your program should process the next test case, or terminate if there are no more test cases.

After printing each line, do not forget to output end of line and flush the output. To do the latter, you can use fflush(stdout) or cout.flush() in $\mathrm { C } + +$ , System.out.flush() in Java, or stdout.flush() in Python.

# Example

standard input	standard output

2

	？12

1	？23

1	？34

1	！1

4	？13

1	？24

0	？12

0	？14

0	！2

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

static long long queries = 0;
static long long query_limit = 0;
static const long long HARD_CAP = 200000;

bool is_positive_integer(const string& s) {
    if (s.empty()) return false;
    for (char c : s) if (!isdigit(c)) return false;
    return true;
}

void log_metrics() {
    try {

        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {

    }
}

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

    cout << t << endl;

    for (int i = 0; i < t; ++i) {

        int n = inf.readInt();
        int type = inf.readInt();

        vector<int> p;
        vector<int> pos(n + 1, 0);
        int center = -1;

        if (type == 1) {

            p.resize(n);
            for (int k = 0; k < n; ++k) {
                p[k] = inf.readInt();
                pos[p[k]] = k;
            }
        } else {

            center = inf.readInt();
        }

        long long local_limit = (long long)ceil(n / 2.0) + 3;
        query_limit += local_limit;
        long long local_queries = 0;

        log_metrics();

        cout << n << endl;

        bool case_active = true;
        while (case_active) {

            if (queries > HARD_CAP) {
                finish(_pe, "Hard query limit exceeded (infinite loop protection)");
            }

            string token;
            if (!(cin >> token)) {
                finish(_pe, "Unexpected EOF (solution exited prematurely)");
            }

            if (token == "!") {

                int ans;
                if (!(cin >> ans)) {
                    finish(_pe, "Expected integer answer after '!'");
                }

                if (ans != 1 && ans != 2) {
                    finish(_wa, "Answer must be 1 or 2");
                }
                if (ans != type) {
                    finish(_wa, format("Wrong answer on case %d. Expected %d, got %d", i+1, type, ans));
                }

                if (local_queries > local_limit) {
                    finish(_wa, format("Query limit exceeded on case %d (used %lld, allowed %lld)", i+1, local_queries, local_limit));
                }

                case_active = false;
            } else {

                int u, v;
                if (token == "?") {
                    if (!(cin >> u >> v)) finish(_pe, "Expected u v after '?'");
                } else {

                    if (!is_positive_integer(token)) {
                        finish(_pe, format("Invalid token '%s'", token.c_str()));
                    }
                    u = atoi(token.c_str());
                    if (!(cin >> v)) finish(_pe, "Expected v");
                }

                if (u < 1 || u > n || v < 1 || v > n || u == v) {
                    finish(_pe, format("Invalid query vertices: %d %d", u, v));
                }

                local_queries++;
                queries++;

                if (queries % 10 == 0) log_metrics();

                int res = 0;
                if (type == 1) {

                    if (abs(pos[u] - pos[v]) == 1) res = 1;
                } else {

                    if (u == center || v == center) res = 1;
                }

                cout << res << endl;
            }
        }
    }

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

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

static long long queries = 0;
static long long query_limit = 0;
static bool metrics_logged = false;

static void log_metrics() {
    if (metrics_logged) return;
    metrics_logged = true;
    try {
        if (query_limit == 0) query_limit = 1000;
        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());
}

const int HARD_CAP = 5000;

struct AdaptiveState {
    int n;
    set<int> star_centers;
    vector<pair<int,int>> forbidden_edges;
    vector<pair<int,int>> forced_edges;

    AdaptiveState(int _n) : n(_n) {
        for(int i=1; i<=n; ++i) star_centers.insert(i);
    }

    bool can_be_star(bool ans_is_1, int u, int v) {

        set<int> next_centers = star_centers;
        if (ans_is_1) {

            set<int> intersect;
            if (next_centers.count(u)) intersect.insert(u);
            if (next_centers.count(v)) intersect.insert(v);
            next_centers = intersect;
        } else {

            next_centers.erase(u);
            next_centers.erase(v);
        }
        return !next_centers.empty();
    }

    bool can_be_chain(bool ans_is_1, int u, int v) {

        if (n >= 9) return true;

        vector<int> p(n);
        iota(p.begin(), p.end(), 1);

        do {
            bool valid = true;

            for (int i = 0; i < n - 1; ++i) {
                int a = p[i], b = p[i+1];
                if (a > b) swap(a, b);

                for (auto& fe : forbidden_edges) {
                    int fa = fe.first, fb = fe.second;
                    if (fa > fb) swap(fa, fb);
                    if (a == fa && b == fb) { valid = false; break; }
                }
                if (!valid) break;

                if (!ans_is_1) {
                    int cur_u = u, cur_v = v;
                    if (cur_u > cur_v) swap(cur_u, cur_v);
                    if (a == cur_u && b == cur_v) { valid = false; break; }
                }
                if (!valid) break;
            }
            if (!valid) continue;

            set<pair<int,int>> chain_edges;
            for (int i = 0; i < n - 1; ++i) {
                int a = p[i], b = p[i+1];
                if (a > b) swap(a, b);
                chain_edges.insert({a, b});
            }

            for (auto& fe : forced_edges) {
                int fa = fe.first, fb = fe.second;
                if (fa > fb) swap(fa, fb);
                if (chain_edges.find({fa, fb}) == chain_edges.end()) {
                    valid = false; break;
                }
            }
            if (!valid) continue;

            if (ans_is_1) {
                int cur_u = u, cur_v = v;
                if (cur_u > cur_v) swap(cur_u, cur_v);
                if (chain_edges.find({cur_u, cur_v}) == chain_edges.end()) {
                    valid = false;
                }
            }

            if (valid) return true;
        } while (next_permutation(p.begin(), p.end()));

        return false;
    }

    void update(bool ans, int u, int v) {
        if (ans) {
            set<int> intersect;
            if (star_centers.count(u)) intersect.insert(u);
            if (star_centers.count(v)) intersect.insert(v);
            star_centers = intersect;
            forced_edges.push_back({u, v});
        } else {
            star_centers.erase(u);
            star_centers.erase(v);
            forbidden_edges.push_back({u, v});
        }
    }
};

void run_case(int n, bool is_fixed, int fixed_type, const vector<int>& fixed_data) {
    int current_case_limit = (n + 1) / 2 + 3;
    int current_case_queries = 0;

    set<pair<int,int>> fixed_edges;
    if (is_fixed) {
        if (fixed_type == 1) {
            for(int i=0; i<n-1; ++i) {
                int u = fixed_data[i], v = fixed_data[i+1];
                if (u > v) swap(u, v);
                fixed_edges.insert({u, v});
            }
        } else {
            int center = fixed_data[0];
            for(int i=1; i<=n; ++i) {
                if (i == center) continue;
                int u = center, v = i;
                if (u > v) swap(u, v);
                fixed_edges.insert({u, v});
            }
        }
    }

    AdaptiveState state(n);

    while (true) {

        queries = current_case_queries;
        query_limit = current_case_limit;

        string token;
        cin >> token;

        if (cin.eof() || token.empty()) {
            finish(_pe, "Unexpected EOF from solution");
        }

        if (token == "!") {
            int ans;
            cin >> ans;

            if (is_fixed) {
                if (fixed_type == ans) {

                } else {
                    finish(_wa, format("Wrong answer on fixed case: expected %d, got %d", fixed_type, ans));
                }
            } else {

                bool possible_star = !state.star_centers.empty();

                bool possible_chain = state.can_be_chain(false, -1, -1);

                if (ans == 1) {
                    if (possible_star) finish(_wa, "Star was still possible");
                    if (!possible_chain) finish(_wa, "Chain was NOT possible (inconsistent state)");
                } else if (ans == 2) {
                    if (possible_chain) finish(_wa, "Chain was still possible");
                    if (!possible_star) finish(_wa, "Star was NOT possible (inconsistent state)");
                } else {
                    finish(_pe, format("Invalid answer type: %d", ans));
                }
            }
            return;
        } else if (token == "?") {
            int u, v;
            cin >> u >> v;

            if (u < 1 || u > n || v < 1 || v > n || u == v) {
                finish(_pe, format("Invalid query: %d %d", u, v));
            }

            current_case_queries++;

            if (current_case_queries > HARD_CAP) {
                finish(_pe, "Hard query limit exceeded");
            }

            int reply = 0;
            if (is_fixed) {
                if (u > v) swap(u, v);
                reply = fixed_edges.count({u, v}) ? 1 : 0;
            } else {

                bool ok0_star = state.can_be_star(false, u, v);
                bool ok0_chain = state.can_be_chain(false, u, v);

                if (ok0_star || ok0_chain) {
                    reply = 0;
                } else {

                    reply = 1;
                }
                state.update(reply == 1, u, v);
            }

            cout << reply << endl;
        } else {
            finish(_pe, "Expected '?' or '!'");
        }
    }
}

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

    int t = inf.readInt();

    int next_n = inf.readInt();

    cout << t << endl;

    for (int i = 0; i < t; ++i) {
        int n = next_n;
        bool is_fixed = false;
        int fixed_type = 0;
        vector<int> fixed_data;

        cout << n << endl;

        if (i == t - 1) {

            inf.skipBlanks();
            if (!inf.eof()) {
                is_fixed = true;
                fixed_type = inf.readInt();
            }
        } else {

            int val = inf.readInt();
            if (val == 1 || val == 2) {
                is_fixed = true;
                fixed_type = val;
            } else {
                next_n = val;
            }
        }

        if (is_fixed) {
            if (fixed_type == 1) {
                for (int k = 0; k < n; ++k) fixed_data.push_back(inf.readInt());
            } else {
                fixed_data.push_back(inf.readInt());
            }

            if (i < t - 1) {
                next_n = inf.readInt();
            }
        }

        run_case(n, is_fixed, fixed_type, fixed_data);
    }

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

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

    long long seed_val = -1;
    if (argc > 1) {
        string s = argv[1];
        bool is_num = true;
        for (char c : s) if (!isdigit(c)) is_num = false;
        if (is_num && s.length() < 18) seed_val = atoll(s.c_str());
    }

    int sum_limit = 1000;
    int min_n = 4;
    vector<int> ns;

    if (seed_val == 1) {

        ns.push_back(1000);
    } else if (seed_val == 2) {

        for (int i = 0; i < 250; ++i) ns.push_back(4);
    } else if (seed_val == 3) {

        vector<int> sizes = {4, 5, 8, 10, 16, 32};
        int cur = 0;
        while (cur < sum_limit) {
            int x = sizes[rnd.next(sizes.size())];
            if (cur + x <= sum_limit) {
                ns.push_back(x);
                cur += x;
            } else {
                int rem = sum_limit - cur;
                if (rem >= min_n) ns.push_back(rem);
                break;
            }
        }
    } else {

        int rem = sum_limit;
        while (rem >= min_n) {

            int type = rnd.next(100);
            int n_curr;

            if (type < 15) n_curr = rnd.next(min_n, min(rem, 10));
            else if (type < 40) n_curr = rnd.next(min_n, min(rem, 50));
            else if (type < 85 && rem > 50) n_curr = rnd.next(50, min(rem, 200));
            else n_curr = rnd.next(min_n, rem);

            n_curr = max(min_n, min(rem, n_curr));

            if (rem - n_curr > 0 && rem - n_curr < min_n) {
                n_curr = rem;
            }

            ns.push_back(n_curr);
            rem -= n_curr;

            if (ns.size() >= 250) {
                if (rem > 0) ns.back() += rem;
                break;
            }
        }
    }

    shuffle(ns.begin(), ns.end());

    println(ns.size());

    for (int n : ns) {
        if (mode == "non") {

            println(n);

            int type = rnd.next(1, 2);
            println(type);

            if (type == 1) {

                vector<int> p(n);
                iota(p.begin(), p.end(), 1);
                shuffle(p.begin(), p.end());
                println(p);
            } else {

                int center = rnd.next(1, n);
                println(center);
            }
        } else {

            println(n);
        }
    }

    return 0;
}


This is an interactive problem.

Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by *n* students, where *n* is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant *i* and participant *i* + 1 (1 ≤ *i* ≤ *n* - 1) are adjacent, as well as participant *n* and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.

As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers *i* and ![](https://espresso.codeforces.com/bb70c692e73bcdcab618b5caa2269589ff959700.png) sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.

You can ask questions of form «which number was received by student *i*?», and the goal is to determine whether the desired pair exists in no more than 60 questions.

Input

At the beginning the even integer *n* (2 ≤ *n* ≤ 100 000) is given — the total number of students.

You are allowed to ask no more than 60 questions.

Output

To ask the question about the student *i* (1 ≤ *i* ≤ *n*), you should print «? *i*». Then from standard output you can read the number *a**i* received by student *i* ( - 109 ≤ *a**i* ≤ 109).

When you find the desired pair, you should print «! *i*», where *i* is any student who belongs to the pair (1 ≤ *i* ≤ *n*). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program.

The query that contains your answer is not counted towards the limit of 60 queries.

Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.

Examples

Input

```
8

2

2
```

Output

```
? 4

? 8

! 4
```

Input

```
6

1

2

3

2

1

0
```

Output

```
? 1

? 2

? 3

? 4

? 5

? 6

! -1
```

Note

Input-output in statements illustrates example interaction.

In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2

In the second sample the selection sequence is 1, 2, 3, 2, 1, 0.

#include "testlib.h"

#include <csignal>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

static int queries = 0;
static int query_limit = 60;

static void log_queries() {
    tout << "queries=" << queries << "\n";
    tout << "query_limit=" << query_limit << "\n";
}

[[noreturn]] static void quit_with(TResult verdict, const string& msg) {
    log_queries();
    quitf(verdict, "%s", msg.c_str());
}

static int across(int i, int n) {
    return ((i - 1 + (n / 2)) % n) + 1;
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);
    signal(SIGPIPE, SIG_IGN);
    atexit(log_queries);

    const int n = inf.readInt();
    vector<long long> a(n + 1);
    if (inf.seekEof()) {

        for (int i = 1; i <= n; i++) {
            a[i] = (i & 1) ? 0 : 1;
        }
    } else {
        for (int i = 1; i <= n; i++) {
            a[i] = inf.readLong();
        }
    }

    bool exists = false;
    for (int i = 1; i <= n; i++) {
        int j = across(i, n);
        if (a[i] == a[j]) {
            exists = true;
            break;
        }
    }

    cout << n << "\n" << flush;

    while (true) {
        string cmd;
        if (!(cin >> cmd)) {
            quit_with(_pe, "Unexpected EOF");
        }

        if (cmd == "?") {
            int idx;
            if (!(cin >> idx)) {
                quit_with(_pe, "Invalid query index");
            }
            if (idx < 1 || idx > n) {
                quit_with(_pe, "Query index out of range");
            }
            queries++;
            if (queries > query_limit) {
                quit_with(_wa, "Too many queries");
            }
            cout << a[idx] << "\n" << flush;
            continue;
        }

        if (cmd == "!") {
            long long ans;
            if (!(cin >> ans)) {
                quit_with(_pe, "Missing answer");
            }
            if (ans == -1) {
                if (exists) {
                    quit_with(_wa, "Answered -1 but a valid pair exists");
                }
                quit_with(_ok, "Correct (-1)");
            }
            if (ans < 1 || ans > n) {
                quit_with(_pe, "Answer index out of range");
            }
            int i = int(ans);
            int j = across(i, n);
            if (a[i] == a[j]) {
                quit_with(_ok, "Correct");
            }
            quit_with(_wa, "Wrong answer");
        }

        quit_with(_pe, "Unknown command: " + cmd);
    }
}

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

static int across(int i, int n) {
    return ((i - 1 + (n / 2)) % n) + 1;
}

static bool hasAcrossEqual(const vector<long long>& a) {
    const int n = (int)a.size() - 1;
    for (int i = 1; i <= n; i++) {
        if (a[i] == a[across(i, n)]) return true;
    }
    return false;
}

static vector<long long> buildFromSteps(int n, const vector<int>& steps, long long start) {
    vector<long long> a(n + 1, 0);
    a[1] = start;
    for (int i = 1; i <= n - 1; i++) {
        a[i + 1] = a[i] + steps[i - 1];
    }
    return a;
}

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

    const long long seed = (argc >= 2) ? atoll(argv[1]) : 1;

    int n = 100000;
    if (seed == 1) n = 2;
    else if (seed == 2) n = 4;
    else if (seed <= 6) n = 2 * (int)seed;
    else if (seed <= 20) n = 2000 + 200 * (int)seed;
    if (n > 100000) n = 100000;
    if (n % 2 == 1) n++;
    n = max(2, min(100000, n));

    const bool want_exists = (seed % 2 == 0);
    if (n == 2 && want_exists) n = 4;

    vector<long long> a;

    const int tries = 200;
    for (int it = 0; it < tries; it++) {
        vector<int> steps(n, -1);
        for (int i = 0; i < n / 2; i++) steps[i] = +1;
        shuffle(steps.begin(), steps.end());

        const long long margin = (long long)n + 10;
        const long long lo = -1000000000LL + margin;
        const long long hi = +1000000000LL - margin;
        const long long start = (lo <= hi) ? rnd.next(lo, hi) : 0LL;

        a = buildFromSteps(n, steps, start);

        bool ok = true;
        for (int i = 1; i <= n; i++) {
            int j = (i == n) ? 1 : (i + 1);
            if (llabs(a[i] - a[j]) != 1) {
                ok = false;
                break;
            }
            if (a[i] < -1000000000LL || a[i] > 1000000000LL) {
                ok = false;
                break;
            }
        }
        if (!ok) continue;

        const bool exists = hasAcrossEqual(a);
        if (exists == want_exists) break;
        a.clear();
    }

    if (a.empty()) {
        const long long start = 0;
        if (want_exists) {

            if (n % 4 != 0) n += (4 - (n % 4));
            if (n > 100000) n = 100000 - (100000 % 4);
            a.assign(n + 1, 0);
            for (int i = 1; i <= n; i++) a[i] = start + (i % 2);
        } else {

            a.assign(n + 1, 0);
            a[1] = start;
            int up = n / 2;
            for (int i = 2; i <= up + 1; i++) a[i] = a[i - 1] + 1;
            for (int i = up + 2; i <= n; i++) a[i] = a[i - 1] - 1;

            if (llabs(a[n] - a[1]) != 1) a[n] = a[1] + 1;
        }
    }

    cout << n << "\n";
    for (int i = 1; i <= n; i++) {
        if (i > 1) cout << ' ';
        cout << a[i];
    }
    cout << "\n";
    return 0;
}


This is an interactive problem.

There is an array $a_1, a_2, \dots, a_n$ of $n$ integers hidden from you. Your task is to find the smallest element of the array.

To do this, you can ask several queries. In each query, you can pick two integers $l$ and $r$ ($1 \leq l \leq r \leq n$). In return, you will be given the value of $\max(a_l, a_{l+1}, \dots, a_r)$. In other words, you will be told the maximum value of the subarray from $a_l$ to $a_r$.

Find the minimum of the array. You can ask at most $\mathbf{624}$ queries.

It is guaranteed that the values $a_1, a_2, \dots, a_n$ are chosen uniformly at random between $0$ and $2^{32}-1$ (inclusive).

Interaction

Begin the interaction by reading an integer $n$ ($1 \le n \le 10^4$) on a separate line.

To ask a query, print a line in the form $\texttt{?}\;\;l\;\;r$ ($1 \le l \le r \le n$). Then you should read a single line containing the answer to your query.

After you have determined the answer, print a line in the form $\texttt{!}\;\;x$ where $x$ is the minimum value.

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

Answer $\texttt{-1}$ means that you made an invalid query. Exit immediately after receiving $\texttt{-1}$ and you will see Wrong answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

Example

Input

```
3

1295320727

984617979

1295320727

167316954
```

Output

```
? 1 3

? 1 1

? 2 2

? 3 3

! 167316954
```

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

static long long queries = 0;

static const long long query_limit = 624;

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 long long parse_case_id(const char* case_path) {
    string s(case_path ? case_path : "");
    size_t slash = s.find_last_of("/\\");
    if (slash != string::npos) s = s.substr(slash + 1);
    size_t dot = s.find_last_of('.');
    if (dot != string::npos) s = s.substr(0, dot);
    long long id = 0;
    bool any = false;
    for (unsigned char ch : s) {
        if (isdigit(ch)) {
            any = true;
            id = id * 10 + (ch - '0');
        }
    }
    return any ? id : 0;
}

int main(int argc, char** argv) {

    registerInteraction(argc, argv);
    atexit(log_metrics);

    const int n = inf.readInt(1, 10000, "n");

    const long long case_id = (argc >= 2) ? parse_case_id(argv[1]) : 0;
    const uint32_t seed = (uint32_t)(0xC0FFEEU ^ (uint32_t)case_id ^ (uint32_t)n * 2654435761U);
    std::mt19937 rng(seed);

    vector<uint32_t> a((size_t)n + 1);
    uint32_t true_min = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = rng();
        if (i == 1 || a[i] < true_min) true_min = a[i];
    }

    cout << n << endl;

    while (true) {

        if (queries > 200000) {
            finish(_pe, "Hard query limit exceeded (200000)");
        }

        string token;
        cin >> token;

        if (cin.fail()) {

            finish(_wa, "Unexpected end of stream (expected query or answer)");
        }

        if (token == "!") {

            long long ans;
            cin >> ans;
            if (cin.fail()) {
                finish(_wa, "Failed to read answer value");
            }

            if (ans == true_min) {
                finish(_ok, "Correct minimum found");
            } else {
                finish(_wa, format("Wrong answer: expected %lld, found %lld", true_min, ans));
            }
        } else {

            int l, r;
            if (token == "?") {
                cin >> l >> r;
            } else {

                try {
                    l = stoi(token);
                } catch (...) {
                    finish(_wa, format("Invalid token '%s' (expected '?', '!', or integer)", token.c_str()));
                }
                cin >> r;
            }

            if (cin.fail()) {
                finish(_wa, "Failed to read query coordinates");
            }

            queries++;
            if (queries > query_limit) {
                cout << -1 << endl;
                finish(_wa, "Query limit exceeded");
            }

            if (l < 1 || l > r || r > n) {

                cout << -1 << endl;
                finish(_wa, format("Invalid query range: %d %d (N=%d)", l, r, n));
            }

            uint32_t current_max = 0;
            for (int i = l; i <= r; i++) current_max = max(current_max, a[i]);

            cout << current_max << endl;

            if (queries % 100 == 0) {
                log_metrics();
            }
        }
    }

    return 0;
}

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

static int queries = 0;
static int query_limit = 624;

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 long long parse_case_id(const char* case_path) {
    string s(case_path ? case_path : "");
    size_t slash = s.find_last_of("/\\");
    if (slash != string::npos) s = s.substr(slash + 1);
    size_t dot = s.find_last_of('.');
    if (dot != string::npos) s = s.substr(0, dot);
    long long id = 0;
    bool any = false;
    for (unsigned char ch : s) {
        if (isdigit(ch)) {
            any = true;
            id = id * 10 + (ch - '0');
        }
    }
    return any ? id : 0;
}

int main(int argc, char** argv) {

    registerInteraction(argc, argv);
    atexit(log_metrics);

    const int n = inf.readInt(1, 10000, "n");

    const long long case_id = (argc >= 2) ? parse_case_id(argv[1]) : 0;
    const uint32_t seed = (uint32_t)(0xC0FFEEU ^ (uint32_t)case_id ^ (uint32_t)n * 2654435761U);
    std::mt19937 rng(seed);

    vector<uint32_t> a((size_t)n + 1);
    uint32_t min_val = 0;
    for (int i = 1; i <= n; i++) {
        a[i] = rng();
        if (i == 1 || a[i] < min_val) min_val = a[i];
    }

    cout << n << endl;

    int hard_cap = 20000;
    int ops = 0;

    while (true) {
        if (ops++ > hard_cap) {
            finish(_pe, "Hard cap on interactions exceeded");
        }

        string type;

        if (!(cin >> type)) {
            finish(_wa, "Unexpected EOF (expected '?' or '!')");
        }

        if (type == "?") {
            int l, r;
            if (!(cin >> l >> r)) {
                finish(_wa, "Invalid query format");
            }

            if (l < 1 || l > n || r < 1 || r > n || l > r) {
                finish(_wa, format("Invalid query range: %d %d", l, r));
            }

            queries++;
            if (queries > query_limit) {
                cout << -1 << endl;
                finish(_wa, "Query limit exceeded");
            }

            if (queries % 50 == 0) log_metrics();

            uint32_t current_max = 0;
            for (int i = l; i <= r; i++) current_max = max(current_max, a[i]);

            cout << current_max << endl;

        } else if (type == "!") {
            long long ans;
            if (!(cin >> ans)) {
                finish(_wa, "Invalid answer format");
            }

            if (ans == min_val) {
                finish(_ok, format("Correct! Found min %lld in %d queries", min_val, queries));
            } else {
                finish(_wa, format("Wrong answer. Expected %lld, found %lld", min_val, ans));
            }

        } else {
            finish(_wa, format("Unknown request type: %s", type.c_str()));
        }
    }

    return 0;
}

#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 n;
    long long seed_val = 0;
    if (argc > 1) seed_val = atoll(argv[1]);

    if (seed_val == 1) {
        n = 1;
    } else if (seed_val == 2) {
        n = 10000;
    } else if (seed_val == 3) {
        n = 100;
    } else {
        n = rnd.next(1, 10000);
    }

    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        a[i] = rnd.next(0LL, 4294967295LL);
    }

    if (mode == "non") {

        cout << n << endl;
        for (int i = 0; i < n; i++) {
            cout << a[i] << (i == n - 1 ? "" : " ");
        }
        cout << endl;
    } else {

        cout << n << endl;
        for (int i = 0; i < n; i++) {
            cout << a[i] << (i == n - 1 ? "" : " ");
        }
        cout << endl;
    }

    return 0;
}


This is an interactive problem.

Bob lives in a square grid of size $n \times n$, with rows numbered $1$ through $n$ from top to bottom, and columns numbered $1$ through $n$ from left to right. Every cell is either allowed or blocked, but you don't know the exact description of the grid. You are given only an integer $n$.

Bob can move through allowed cells but only in some limited directions. When Bob is in an allowed cell in the grid, he can move down or right to an adjacent cell, if it is allowed.

You can ask at most $4 \cdot n$ queries of form "? $r_1$ $c_1$ $r_2$ $c_2$" ($1 \le r_1 \le r_2 \le n$, $1 \le c_1 \le c_2 \le n$). The answer will be "YES" if Bob can get from a cell $(r_1, c_1)$ to a cell $(r_2, c_2)$, and "NO" otherwise. In particular, if one of the two cells (or both) is a blocked cell then the answer is "NO" for sure. Since Bob doesn't like short trips, you can only ask queries with the manhattan distance between the two cells at least $n - 1$, i.e. the following condition must be satisfied: $(r_2 - r_1) + (c_2 - c_1) \ge n - 1$.

It's guaranteed that Bob can get from the top-left corner $(1, 1)$ to the bottom-right corner $(n, n)$ and your task is to find a way to do it. You should print the answer in form "! S" where $S$ is a string of length $2 \cdot n - 2$ consisting of characters 'D' and 'R', denoting moves down and right respectively. The down move increases the first coordinate by $1$, the right move increases the second coordinate by $1$. If there are multiple solutions, any of them will be accepted. You should terminate immediately after printing the solution.

Input

The only line of the input contains an integer $n$ ($2 \le n \le 500$) — the size of the grid.

Output

When you are ready to print the answer, print a single line containing "! S" where where $S$ is a string of length $2 \cdot n - 2$ consisting of characters 'D' and 'R', denoting moves down and right respectively. The path should be a valid path going from the cell $(1, 1)$ to the cell $(n, n)$ passing only through allowed cells.

Interaction

You can ask at most $4 \cdot n$ queries. To ask a query, print a line containing "? $r_1$ $c_1$ $r_2$ $c_2$" ($1 \le r_1 \le r_2 \le n$, $1 \le c_1 \le c_2 \le n$). After that you should read a single line containing "YES" or "NO" depending on the answer of the query. "YES" means Bob can go from the cell $(r_1, c_1)$ to the cell $(r_2, c_2)$, "NO" means the opposite.

Note that the grid is fixed before the start of your solution and does not depend on your queries.

After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded or other negative 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 documentation for other languages.

Answer "BAD" instead of "YES" or "NO" means that you made an invalid query. Exit immediately after receiving "BAD" and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

Example

Input

```
4

YES

NO

YES

YES

```

Output

```

? 1 1 4 4

? 1 2 4 3

? 4 1 4 4

? 1 4 4 4

! RDRRDD
```

Note

The first example is shown on the picture below.

 ![](https://espresso.codeforces.com/4bcdeab2ec8d0a3c5e6d71bf5a192d3236646d15.png)

#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 inline uint64_t maskRangeBits(int l, int r) {
    if (l > r) return 0;
    uint64_t left = (l == 0) ? ~0ull : (~0ull << l);
    uint64_t right = (r == 63) ? ~0ull : ((1ull << (r + 1)) - 1ull);
    return left & right;
}

static void propagateRightWords(const uint64_t* A, const uint64_t* M, uint64_t* B, int W) {
    bool carry = false;
    for (int w = 0; w < W; w++) {
        uint64_t mw = M[w];
        uint64_t aw = A[w] & mw;
        uint64_t bw = 0;

        uint64_t x = mw;
        while (x) {
            int start = __builtin_ctzll(x);
            uint64_t y = x >> start;

            int len;
            uint64_t noty = ~y;
            if (noty == 0ull) len = 64 - start;
            else len = __builtin_ctzll(noty);

            uint64_t segMask;
            if (len == 64) segMask = ~0ull;
            else segMask = ((1ull << len) - 1ull) << start;

            uint64_t segA = aw & segMask;
            bool segSeedFromCarry = carry && (start == 0);

            if (segA || segSeedFromCarry) {
                int seedPos = segSeedFromCarry ? 0 : __builtin_ctzll(segA);
                uint64_t fillMask = segMask & (~0ull << seedPos);
                bw |= fillMask;
            }

            x &= ~segMask;
        }

        B[w] = bw;
        carry = ((bw >> 63) & 1ull) != 0;
    }
}

struct GridJudge {
    int n = 0;
    int W = 0;
    vector<array<uint64_t, 8>> rowAllowed;

    bool cellAllowed(int r, int c) const {
        int idx = c - 1;
        int w = idx >> 6;
        int b = idx & 63;
        return (rowAllowed[r][w] >> b) & 1ull;
    }

    bool reachableRect(int r1, int c1, int r2, int c2) const {
        if (r1 > r2 || c1 > c2) return false;
        if (!cellAllowed(r1, c1) || !cellAllowed(r2, c2)) return false;

        uint64_t interval[8] = {0, 0, 0, 0, 0, 0, 0, 0};
        int lIdx = c1 - 1;
        int rIdx = c2 - 1;
        int sw = lIdx >> 6, sb = lIdx & 63;
        int ew = rIdx >> 6, eb = rIdx & 63;

        if (sw == ew) {
            interval[sw] = maskRangeBits(sb, eb);
        } else {
            interval[sw] = maskRangeBits(sb, 63);
            for (int w = sw + 1; w <= ew - 1; w++) interval[w] = ~0ull;
            interval[ew] = maskRangeBits(0, eb);
        }
        for (int w = W; w < 8; w++) interval[w] = 0;

        uint64_t prev[8] = {0, 0, 0, 0, 0, 0, 0, 0};
        uint64_t cur[8] = {0, 0, 0, 0, 0, 0, 0, 0};
        uint64_t A[8] = {0, 0, 0, 0, 0, 0, 0, 0};
        uint64_t M[8] = {0, 0, 0, 0, 0, 0, 0, 0};

        for (int r = r1; r <= r2; r++) {
            for (int w = 0; w < W; w++) M[w] = rowAllowed[r][w] & interval[w];
            for (int w = W; w < 8; w++) M[w] = 0;

            if (r == r1) {
                for (int w = 0; w < 8; w++) A[w] = 0;
                int idx = c1 - 1;
                int ww = idx >> 6;
                int bb = idx & 63;
                A[ww] |= (1ull << bb);
                for (int w = 0; w < W; w++) A[w] &= M[w];
            } else {
                for (int w = 0; w < W; w++) A[w] = prev[w] & M[w];
                for (int w = W; w < 8; w++) A[w] = 0;
            }

            propagateRightWords(A, M, cur, W);

            for (int w = 0; w < 8; w++) prev[w] = cur[w];
        }

        int idx = c2 - 1;
        int ww = idx >> 6;
        int bb = idx & 63;
        return ((prev[ww] >> bb) & 1ull) != 0;
    }
};

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

    GridJudge J;
    J.n = inf.readInt(2, 500, "n");
    J.W = (J.n + 63) / 64;
    J.rowAllowed.assign(J.n + 1, array<uint64_t, 8>{0, 0, 0, 0, 0, 0, 0, 0});

    for (int r = 1; r <= J.n; r++) {
        string s = inf.readToken();
        if ((int)s.size() != J.n) finish(_pe, "invalid hidden grid row length");
        for (int c = 1; c <= J.n; c++) {
            char ch = s[c - 1];
            if (ch != '.' && ch != '#') finish(_pe, "invalid hidden grid char");
            if (ch == '.') {
                int idx = c - 1;
                int w = idx >> 6;
                int b = idx & 63;
                J.rowAllowed[r][w] |= (1ull << b);
            }
        }
    }

    query_limit = 4LL * J.n;
    long long hard_cap = max(200000LL, 10LL * query_limit);

    log_metrics();

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

    while (true) {
        string t;
        if (!(cin >> t)) finish(_pe, "unexpected EOF from contestant");

        if (t == "?") {
            long long r1, c1, r2, c2;
            if (!(cin >> r1 >> c1 >> r2 >> c2)) finish(_pe, "malformed query");
            queries++;
            if (queries > hard_cap) finish(_pe, "hard cap exceeded");

            bool ok = true;
            ok = ok && (1 <= r1 && r1 <= r2 && r2 <= J.n);
            ok = ok && (1 <= c1 && c1 <= c2 && c2 <= J.n);
            ok = ok && ((r2 - r1) + (c2 - c1) >= (J.n - 1));

            if (!ok) {
                cout << "BAD" << endl;
                cout.flush();
                finish(_pe, "invalid query constraints");
            }

            bool ans = J.reachableRect((int)r1, (int)c1, (int)r2, (int)c2);
            cout << (ans ? "YES" : "NO") << endl;
            cout.flush();
        } else if (t == "!") {
            string s;
            if (!(cin >> s)) finish(_pe, "missing answer string");

            if ((int)s.size() != 2 * J.n - 2) finish(_wa, "wrong answer length");

            int r = 1, c = 1;
            if (!J.cellAllowed(r, c)) finish(_wa, "start cell blocked");
            for (char ch : s) {
                if (ch == 'D') r++;
                else if (ch == 'R') c++;
                else finish(_wa, "invalid move character");
                if (r < 1 || r > J.n || c < 1 || c > J.n) finish(_wa, "moved out of grid");
                if (!J.cellAllowed(r, c)) finish(_wa, "stepped on blocked cell");
            }
            if (r != J.n || c != J.n) finish(_wa, "did not reach target");

            finish(_ok, "correct");
        } else {
            finish(_pe, "unknown token from contestant");
        }
    }
}

#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());
}

struct Corridor {

    int n = 0;
    int mode = 0;

    bool allowed(int r, int c) const {
        if (mode == 0) return (r == 1) || (c == n);
        return (c == 1) || (r == n);
    }

    int pos(int r, int c) const {

        if (!allowed(r, c)) return -1;
        if (mode == 0) {

            if (r == 1) return c - 1;

            return (n - 1) + (r - 1);
        } else {

            if (c == 1) return r - 1;

            return (n - 1) + (c - 1);
        }
    }

    string canonicalPathString() const {
        string s;
        s.reserve(2 * n - 2);
        if (mode == 0) {
            s.append(n - 1, 'R');
            s.append(n - 1, 'D');
        } else {
            s.append(n - 1, 'D');
            s.append(n - 1, 'R');
        }
        return s;
    }
};

static bool readToken(string& tok) {
    if (!(cin >> tok)) return false;
    return true;
}

static bool readIntLL(long long& x) {
    if (!(cin >> x)) return false;
    return true;
}

static bool isAllDR(const string& s) {
    for (char ch : s) if (ch != 'D' && ch != 'R') return false;
    return true;
}

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

    int n = inf.readInt();
    if (n < 2 || n > 500) {
        query_limit = 0;
        log_metrics();
        finish(_pe, "invalid n in input");
    }

    query_limit = 4LL * n;

    log_metrics();

    cout << n << "\n";
    cout.flush();

    Corridor corr;
    corr.n = n;
    corr.mode = 0;
    bool modeChosen = false;

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

    while (true) {
        string t;
        if (!readToken(t)) {
            finish(_pe, "unexpected EOF from contestant");
        }

        if (t == "?") {
            long long r1, c1, r2, c2;
            if (!readIntLL(r1) || !readIntLL(c1) || !readIntLL(r2) || !readIntLL(c2)) {
                cout << "BAD\n";
                cout.flush();
                finish(_pe, "malformed query: expected 4 integers");
            }

            if (r1 < 1 || r1 > n || r2 < 1 || r2 > n || c1 < 1 || c1 > n || c2 < 1 || c2 > n) {
                cout << "BAD\n";
                cout.flush();
                finish(_pe, "query out of bounds");
            }
            if (r1 > r2 || c1 > c2) {
                cout << "BAD\n";
                cout.flush();
                finish(_pe, "query violates r1<=r2 and c1<=c2");
            }

            long long dist = (r2 - r1) + (c2 - c1);
            if (dist < n - 1) {
                cout << "BAD\n";
                cout.flush();
                finish(_pe, "query violates manhattan distance restriction");
            }

            queries++;
            if (queries > hard_cap) {
                cout << "BAD\n";
                cout.flush();
                finish(_pe, "too many queries (hard cap exceeded)");
            }

            if (!modeChosen) {

                unsigned long long h = 1469598103934665603ULL;
                auto mix = [&](unsigned long long v) {
                    h ^= v + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
                };
                mix((unsigned long long)r1);
                mix((unsigned long long)c1);
                mix((unsigned long long)r2);
                mix((unsigned long long)c2);
                corr.mode = (int)(h & 1ULL);
                modeChosen = true;
            }

            int p1 = corr.pos((int)r1, (int)c1);
            int p2 = corr.pos((int)r2, (int)c2);

            if (p1 == -1 || p2 == -1 || p1 > p2) {
                cout << "NO\n";
            } else {
                cout << "YES\n";
            }
            cout.flush();
            continue;
        }

        if (t == "!") {
            string s;
            if (!readToken(s)) {
                finish(_pe, "malformed answer: expected a path string");
            }

            if (!modeChosen) {

                corr.mode = 0;
                modeChosen = true;
            }

            if ((int)s.size() != 2 * n - 2) finish(_wa, "wrong path length");
            if (!isAllDR(s)) finish(_wa, "path contains invalid characters");

            int cntR = 0, cntD = 0;
            for (char ch : s) (ch == 'R' ? cntR : cntD)++;
            if (cntR != n - 1 || cntD != n - 1) finish(_wa, "wrong number of moves");

            int r = 1, c = 1;
            if (!corr.allowed(r, c)) finish(_wa, "start cell blocked (internal error)");
            for (char ch : s) {
                if (ch == 'D') r++;
                else c++;
                if (r < 1 || r > n || c < 1 || c > n) finish(_wa, "path goes out of bounds");
                if (!corr.allowed(r, c)) finish(_wa, "path steps on a blocked cell");
            }
            if (r != n || c != n) finish(_wa, "path does not end at (n,n)");

            finish(_ok, "OK");
        }

        finish(_pe, "unexpected token from contestant");
    }
}

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

static bool hasOptName(int argc, char** argv, const string& longName, const string& shortName) {
    string longKey = "--" + longName;
    string shortKey = "-" + shortName;
    for (int i = 2; i < argc; i++) {
        string s = argv[i];
        if (s == longKey || s == shortKey) return true;
        if (s.rfind(longKey + "=", 0) == 0) return true;
    }
    return false;
}

static string buildAlternatingRunsPath(int n, int maxRun) {
    int remR = n - 1, remD = n - 1;
    string p;
    p.reserve(2 * n - 2);

    char dir = rnd.next(0, 1) ? 'R' : 'D';
    while (remR > 0 || remD > 0) {
        if (dir == 'R' && remR == 0) dir = 'D';
        if (dir == 'D' && remD == 0) dir = 'R';

        int& rem = (dir == 'R' ? remR : remD);
        int runMax = min(maxRun, rem);
        int runLen = rnd.next(1, runMax);
        p.append(runLen, dir);
        rem -= runLen;

        if (remR == 0 && remD == 0) break;

        bool canFlip = (dir == 'R' ? (remD > 0) : (remR > 0));
        if (!canFlip) continue;

        bool flip = true;
        if (rnd.next(1, 10) == 1) flip = false;
        if (flip) dir = (dir == 'R' ? 'D' : 'R');
    }

    if ((int)p.size() != 2 * n - 2) {

        p.clear();
        p.append(n - 1, 'R');
        p.append(n - 1, 'D');
    }
    return p;
}

static vector<string> gridFromPath(int n, const string& path) {
    vector<string> g(n, string(n, '#'));
    int r = 0, c = 0;
    g[r][c] = '.';
    for (char ch : path) {
        if (ch == 'D') r++;
        else c++;
        if (r < 0 || r >= n || c < 0 || c >= n) break;
        g[r][c] = '.';
    }

    g[0][0] = '.';
    g[n - 1][n - 1] = '.';
    return g;
}

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

    string mode = opt<string>("mode", "non");
    int nOpt = opt<int>("n", 500);
    int maxRun = opt<int>("maxRun", 7);

    bool nForced = hasOptName(argc, argv, "n", "n");
    int seed = atoi(argv[1]);

    int n = nOpt;
    if (!nForced) {
        if (seed == 1) n = 2;
        else if (seed == 2) n = 500;
        else if (seed == 3) n = 7;
        else n = min(500, max(2, nOpt));
    }
    n = min(500, max(2, n));
    maxRun = min(50, max(1, maxRun));

    if (mode == "adp") {

        cout << n << "\n";
        return 0;
    }

    if (mode != "non") {

        mode = "non";
    }

    string path;
    if (seed == 1) {
        path = "RD";
    } else if (seed == 2) {
        path.assign(n - 1, 'R');
        path.append(n - 1, 'D');
    } else if (seed == 3) {

        path = "RDRDRD";
        int usedR = 3, usedD = 3;
        int remR = (n - 1) - usedR;
        int remD = (n - 1) - usedD;

        char dir = 'R';
        while (remR > 0 || remD > 0) {
            if (dir == 'R' && remR == 0) dir = 'D';
            if (dir == 'D' && remD == 0) dir = 'R';
            if (dir == 'R') {
                path.push_back('R');
                remR--;
                dir = 'D';
            } else {
                path.push_back('D');
                remD--;
                dir = 'R';
            }
        }
    } else {

        path = buildAlternatingRunsPath(n, maxRun);

        if (n >= 10 && (int)path.size() == 2 * n - 2) {
            int block = min(20, 2 * n - 2);
            int pos = rnd.next(0, (2 * n - 2) - block);
            string sub = path.substr(pos, block);
            stable_sort(sub.begin(), sub.end(), [&](char a, char b) {

                bool rFirst = (rnd.next(0, 1) == 1);
                if (a == b) return false;
                if (rFirst) return a == 'R' && b == 'D';
                return a == 'D' && b == 'R';
            });
            for (int i = 0; i < block; i++) path[pos + i] = sub[i];
        }
    }

    int cntR = 0, cntD = 0;
    for (char ch : path) (ch == 'R' ? cntR : cntD)++;
    if ((int)path.size() != 2 * n - 2 || cntR != n - 1 || cntD != n - 1) {
        path.assign(n - 1, 'R');
        path.append(n - 1, 'D');
    }

    vector<string> g = gridFromPath(n, path);

    cout << n << "\n";
    for (int i = 0; i < n; i++) cout << g[i] << "\n";
    return 0;
}


Imperial Chance & Play Casino offers games using a slot machine that has $n$ wheels arranged next to each other. Each of the wheels has $n$ distinct symbols on it, and these symbols appear in the same order on each wheel. Each wheel shows one of its symbols through a window on the front of the machine, which results in a sequence of $n$ symbols being shown next to each other.

![](images/d11cceeb619ece1f029930fcf0fc4a93e1c49676c6791b6f0856c419b6201179.jpg)
Figure I.1: The initial configuration in Sample Interaction 1.

You are standing behind the machine and notice that a maintenance panel has been left open. When you stick your hand inside, you are able to secretly rotate any of the wheels by any number of steps, thus changing the symbol shown on that wheel. You want to win a jackpot, which will happen if all the wheels show the same symbol at the same time. Unfortunately, you cannot see the symbols from your position, so you asked your good friend to help you. The friend is standing in front of the machine and she tells you the number of distinct symbols in the sequence she can currently see. Can you win the jackpot by manipulating the wheels if your friend updates the information after every action you make?

# Interaction

The first line of input contains an integer $n$ $3 \leq n \leq 5 0$ ), giving the number of wheels and symbols in the machine.

Interaction then proceeds in rounds. In each round, one line of input becomes available, containing an integer $k$ $( 1 \leq k \leq n )$ , the number of distinct symbols in the current sequence. If $k > 1$ , output two integers $i$ and $j$ $\ 1 \leq i \leq n$ ; $- 1 0 ^ { 9 } \le j \le 1 0 ^ { 9 } )$ , representing your action: rotating the $i ^ { \mathrm { t h } }$ wheel by $j$ positions, where negative numbers indicate rotating in the opposite direction. Otherwise, if $k = 1$ , indicating that all wheels show the same symbol, your program must exit without printing more output.

At most 10 000 actions are allowed – if your submission uses more rounds, it will not be accepted. It is guaranteed that the initial configuration of wheels does not already have all wheels showing the same symbol ( $k > 1$ in the first round).

The judge program will not behave in an adversarial way, which means the initial configuration is fixed before the first action.

A testing tool is provided to help you develop and test your solution.

ICPC International Collegiate Programming Contest

# The 2025 ICPC World Finals Baku

31 August - 5 September 2025 //hosted by ADA University

Read                                     Sample Interaction 1                                     Write5

5

	11

3

	42

3

	31

3

	31

2

	54

1

Read                                     Sample Interaction 2                                     Write33

33

	2-1

2

	3-1

2

	2-1

1

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

static long long queries = 0;

static const long long QUERY_LIMIT = 10000;

static const long long HARD_CAP = 200000;

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 count_distinct(int n, const vector<int>& s) {

    static vector<bool> seen;
    if (seen.size() < (size_t)n) seen.resize(n);
    fill(seen.begin(), seen.begin() + n, false);

    int distinct = 0;
    for (int x : s) {
        if (x >= 0 && x < n) {
            if (!seen[x]) {
                seen[x] = true;
                distinct++;
            }
        }
    }
    return distinct;
}

int main(int argc, char** argv) {

    registerInteraction(argc, argv);

    atexit(log_metrics);

    int n = inf.readInt();

    if (n < 1) quitf(_fail, "Invalid n in input file");

    vector<int> s(n);
    for (int i = 0; i < n; i++) {
        s[i] = inf.readInt();
    }

    cout << n << endl;

    while (true) {

        int k = count_distinct(n, s);
        cout << k << endl;

        if (k == 1) {
            finish(_ok, "All wheels show the same symbol.");
        }

        if (queries >= HARD_CAP) {
            finish(_pe, "Hard query limit exceeded (possible infinite loop).");
        }

        if (queries > 0 && queries % 10 == 0) {
            log_metrics();
        }

        string token;
        if (!(cin >> token)) {
            finish(_wa, "Unexpected EOF while waiting for action");
        }

        if (token == "?") {
            if (!(cin >> token)) {
                finish(_wa, "Expected index i after '?'");
            }
        } else if (!token.empty() && token[0] == '!') {

            finish(_wa, "Unexpected token '!'. Protocol expects actions until k=1.");
        }

        int i_idx = -1;
        try {
            size_t end_pos = 0;
            long long val = stoll(token, &end_pos);
            if (end_pos != token.size()) finish(_wa, "Garbage found in index token");

            if (val < 1 || val > n) {
                finish(_wa, "Index i is out of bounds [1, " + to_string(n) + "]");
            }
            i_idx = (int)val;
        } catch (...) {
            finish(_wa, "Invalid integer format for index i");
        }

        long long j_steps = 0;
        if (!(cin >> j_steps)) {
            finish(_wa, "Expected integer for rotation j");
        }

        if (abs(j_steps) > 1000000000LL) {
             finish(_wa, "Rotation j out of range [-10^9, 10^9]");
        }

        queries++;

        int v_idx = i_idx - 1;
        long long current_val = s[v_idx];

        long long next_val = (current_val + j_steps) % n;
        if (next_val < 0) next_val += n;

        s[v_idx] = (int)next_val;
    }

    return 0;
}

#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 specified_n = opt<int>("n", -1);

    int n;
    int seed_val = atoi(argv[1]);

    if (specified_n != -1) {
        n = specified_n;
    } else {
        if (seed_val == 1) n = 3;
        else if (seed_val == 2) n = 50;
        else if (seed_val == 3) n = 4;
        else if (seed_val == 4) n = 5;
        else n = rnd.next(3, 50);
    }

    if (mode == "adp") {
        cout << n << endl;
        return 0;
    }

    vector<int> s(n);

    int strategy = rnd.next(0, 4);

    if (strategy == 0) {

        for (int i = 0; i < n; i++) {
            s[i] = rnd.next(0, n - 1);
        }
    } else if (strategy == 1) {

        for (int i = 0; i < n; i++) s[i] = i;

        for (int i = 1; i < n; i++) {
            int j = rnd.next(0, i);
            swap(s[i], s[j]);
        }
    } else if (strategy == 2) {

        int v1 = rnd.next(0, n - 1);
        int v2 = rnd.next(0, n - 1);
        while (v1 == v2) v2 = rnd.next(0, n - 1);
        for (int i = 0; i < n; i++) {
            s[i] = (rnd.next(2) ? v1 : v2);
        }
    } else if (strategy == 3) {

        int common = rnd.next(0, n - 1);
        fill(s.begin(), s.end(), common);
        int idx = rnd.next(0, n - 1);
        int diff = rnd.next(0, n - 1);
        while (diff == common) diff = rnd.next(0, n - 1);
        s[idx] = diff;
    } else {

        for (int i = 0; i < n; i++) {
            s[i] = rnd.next(0, n - 1);
        }
    }

    bool all_same = true;
    for (int i = 1; i < n; i++) {
        if (s[i] != s[0]) {
            all_same = false;
            break;
        }
    }

    if (all_same) {
        int idx = rnd.next(0, n - 1);
        s[idx] = (s[idx] + 1) % n;
    }

    cout << n << endl;
    for (int i = 0; i < n; i++) {
        cout << s[i] << (i == n - 1 ? "" : " ");
    }
    cout << endl;

    return 0;
}


The two versions have different constraints on $k$, $c$. Solving one of the two versions does not necessarily solve the other. You may want to read both versions.

This is an interactive problem.

During the time of the Ottoman Empire, many scientists were at work. You decided to study their daily life and stumbled upon an interesting game that was popular at that time. One scientist would think of an integer $x$, such that $1 \leq x \leq c$. Another scientist would try to guess it. He could specify the base of the numeral system $2 \leq b \leq c$ and would receive as a response the sum of the digits of the number $x$ in base $b$, if $x \geq b$, or $-1$ if $x < b$.

You wrote a program that can think of a number $x$ and respond to the question. Now you want to play with it and learn to guess using $\leq k$ queries.

Input

The first line contains three integers $t$, $k$, $c$ ($1 \leq t \leq 10^4$, $\mathbf{k = 4}$, $\mathbf{c = 4 \cdot 10^{18}}$). You must guess the number from $1$ to $c$ $t$ times, using $\leq k$ queries. All $t$ games will be played consecutively and are independent.

Interaction

To make a query, you must output ? $b$ for some $2 \leq b \leq c$. Then you must read the response — an integer $a$ ($-1 \leq a < c$) — the response to the query.

To provide the guessed number, you must output ! $x$, where $1 \leq x \leq c$ — your answer. Then you must read the result $r$ ($0 \leq r \leq 1$). If $r = 0$ — your answer is incorrect, you must immediately terminate the program in this case. Otherwise, if $r = 1$ — your answer is correct, you can proceed to the next game or terminate the program if $t$ games have already been played.

It is guaranteed that the interactor is not adaptive. That is, all chosen numbers are fixed in advance and do not change during the games.

After printing each query do not forget to output the end of line and flush$^{\text{∗}}$ the output. Otherwise, you will get Idleness limit exceeded verdict.

If, at any interaction step, you read -2 instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.

$^{\text{∗}}$To flush, use:

* fflush(stdout) or cout.flush() in C++;
* sys.stdout.flush() in Python;
* see the documentation for other languages.

Example

Input

```
3 4 4000000000000000000

1

1

7

1

-1

1

-1

6

6

1
```

Output

```
? 10

? 1000

? 2

! 1000000

? 2

! 1

? 100

? 10

? 5

! 42
```

Note

In the first game, the hidden number is $1000000_{10} = 100_{1000} = 11110100001001000000_2$. The sums of digits are $1$, $1$, and $7$ in these bases.

In the second game, the hidden number is $1$. The query with $b = 2$ returns $-1$.

In the third game, the hidden number is $42_{10} = 132_{5}$.

#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 = 200000;

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

    }
}

template <typename... Args>
void finish(TResult result, const char* fmt, Args... args) {
    log_metrics();
    quitf(result, fmt, args...);
}

long long get_digit_sum(long long x, long long b) {
    if (x < b) return -1;
    long long s = 0;
    long long temp = x;
    while (temp > 0) {
        s += temp % b;
        temp /= b;
    }
    return s;
}

int main(int argc, char** argv) {

    registerInteraction(argc, argv);

    atexit(log_metrics);

    int t = inf.readInt();
    int k = inf.readInt();
    long long c = inf.readLong();

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

    log_metrics();

    cout << t << " " << k << " " << c << endl;

    for (int i = 0; i < t; i++) {
        long long hidden_x = inf.readLong();
        bool solved = false;

        while (!solved) {

            if (queries > hard_cap) {
                finish(_pe, "Hard query limit exceeded (>%lld)", hard_cap);
            }

            string token;
            if (!(cin >> token)) {
                finish(_pe, "Unexpected EOF from solution (test case %d)", i + 1);
            }

            if (token == "!") {

                long long guess;
                if (!(cin >> guess)) {
                    finish(_pe, "Unexpected EOF reading guess");
                }

                if (guess == hidden_x) {
                    cout << 1 << endl;
                    solved = true;
                } else {
                    cout << 0 << endl;
                    finish(_wa, "Incorrect guess on case %d: expected %lld, got %lld", i + 1, hidden_x, guess);
                }
            } else {

                long long b;
                if (token == "?") {

                    if (!(cin >> b)) {
                        finish(_pe, "Unexpected EOF reading base b");
                    }
                } else {

                    try {
                        size_t pos;
                        b = stoll(token, &pos);
                        if (pos != token.size()) throw exception();
                    } catch (...) {
                        finish(_pe, "Invalid token '%s' (expected '?', '!', or integer)", token.c_str());
                    }
                }

                queries++;

                if (queries % 50 == 0) log_metrics();

                if (b < 2 || b > c) {
                    finish(_wa, "Query base b=%lld out of range [2, %lld]", b, c);
                }

                long long ans = get_digit_sum(hidden_x, b);
                cout << ans << endl;
            }
        }
    }

    finish(_ok, "All %d test cases passed. Total queries: %lld", t, queries);
    return 0;
}

#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", 100);

    long long c = 4000000000000000000LL;
    int k = 4;

    cout << t << " " << k << " " << c << "\n";

    if (mode == "adp") {

        for (int i = 0; i < t; ++i) {
            cout << "-1\n";
        }
    } else {

        vector<long long> cases;

        if (t >= 4) {
            cases.push_back(1);
            cases.push_back(c);
            cases.push_back(2);
            cases.push_back(c - 1);
        }

        while ((int)cases.size() < t) {
            int type = rnd.next(1, 100);
            long long x = 1;

            if (type <= 10) {

                x = rnd.next(1LL, 2000LL);
            } else if (type <= 20) {

                x = rnd.next(c - 2000LL, c);
            } else if (type <= 35) {

                int p = rnd.next(0, 61);
                x = (1LL << p);
            } else if (type <= 50) {

                int p = rnd.next(0, 18);
                long long p10 = 1;
                for (int j = 0; j < p; ++j) p10 *= 10;
                x = p10;
            } else if (type <= 60) {

                long long sq = 2000000000LL;
                x = rnd.next(sq - 100000LL, sq + 100000LL);
            } else {

                x = rnd.next(1LL, c);
            }

            if (type <= 50 && rnd.next(3) == 0) {
                x += rnd.next(-10, 10);
            }

            if (x < 1) x = 1;
            if (x > c) x = c;

            cases.push_back(x);
        }

        if (t < 4) {
            cases.clear();
            for (int i = 0; i < t; ++i) cases.push_back(rnd.next(1LL, c));
        }

        shuffle(cases.begin(), cases.end());

        for (long long val : cases) {
            cout << val << "\n";
        }
    }

    return 0;
}