#include "validator.h"

void run() {
    int maxn = toml<int>("MAXN");
    int maxq = toml<int>("MAXQ");
    string mode = toml<string>("MODE");
    if(!set<string>{"","qonly","moveonce","incmoves","movesfirst"}.count(mode))
        die_line("unknown mode: " + mode);

    int n = Int(2, maxn);
    Space();
    int q = Int(1, maxq);
    Endl();
    SpacedInts(n, 1, 1'000'000'000);
    bool query = false;
    std::set<int> movedElevators;
    int lastMoved = 0;
    for (int i = 0; i < q; i++) {
        char c = _peek1();
        if (c == '?') {
            query = true;
            Char('?');
            Space();
            Int(1, n - 1);
            Endl();
        } else if (c == '!') {
            if (mode == "qonly") die_line("qonly mode, but ! seen");
            if (mode == "movesfirst" && query) die_line("movesfirst mode, but ? seen before !");
            Char('!');
            Space();
            int elev = Int(0, n - 1);
            Space();
            Int(1, 1'000'000'000);
            Endl();
            if (mode == "moveonce" && movedElevators.count(elev) != 0) die_line(to_string(elev) + " has already moved.");
            if (mode == "incmoves" && elev < lastMoved) die_line("moves are not increasing");
            movedElevators.insert(elev);
            lastMoved = elev;
        } else die_line("not valid query");
    }
    if (!query) die_line("no ?");
    Eof();
    return;
}
