#include "checkpoint.h"

#include <iostream>
#include <random>

std::vector<int> guess(int n);

namespace grader_h7ac {

enum class RunResult {
    Success,
    IncorrectArgument,
    TooManyQueries,
};

using u32 = uint32_t;
constexpr static u32 HASH_BASE = 653;
constexpr static u32 SECRET_SEED = 02'11'2025;
struct Secret {
    std::mt19937 gen;
    u32 hash;

    Secret() : gen(SECRET_SEED), hash(0u) {}

    u32 Protect(u32 x) {
        x ^= gen();
        hash = hash * HASH_BASE + x;
        return x;
    }
};

struct SegmentTree {
    int n;
    std::vector<int> t;

    void Init(std::vector<int> p) {
        n = p.size();
        t.assign(2*n, 0);
        for (int i = 0; i < n; ++i) t[i+n] = p[i];
        for (int i = n; i--; ) t[i] = std::min(t[2*i], t[2*i+1]);
    }

    int Query(int l, int r) {
        int res = n;
        for (l += n, r += n - 1; l <= r; l >>= 1, r >>= 1) {
            if ((l&1) == 1) res = std::min(res, t[l++]);
            if ((r&1) == 0) res = std::min(res, t[r--]);
        }
        return res;
    }
};

static u32 permutation_size = 0;
static u32 query_count = 0;
static u32 max_query_count = 0;
static SegmentTree sgt;
static std::vector<int> position;

void Terminate(RunResult result) {
    std::cout << static_cast<u32>(result) << '\n';
    exit(0);
}

int MinValue(int i, int j) {
    ++query_count;
    if (query_count > max_query_count)
        Terminate(RunResult::TooManyQueries);
    if (i < 1 || i > static_cast<int>(permutation_size))
        Terminate(RunResult::IncorrectArgument);
    if (j < 1 || j > static_cast<int>(permutation_size))
        Terminate(RunResult::IncorrectArgument);

    i = position[i], j = position[j];
    if (i > j) std::swap(i, j);
    int res = permutation_size;

    if (j - i > permutation_size - j + i) {
        res = std::min(sgt.Query(j, permutation_size), sgt.Query(0, i+1));
    } else {
        res = sgt.Query(i, j+1);
    }

    return res;
}


std::vector<int> RunTestcase(Secret& secret_input) {
    int n;
    std::cin >> n;
    permutation_size = n; // global variable

    std::vector<int> p(n);
    for (int& x : p) {
        u32 y;
        std::cin >> y;
        y = secret_input.Protect(y);
        x = static_cast<int>(y);
    }

    sgt.Init(p);

    position.resize(n+1);
    for (int i = 0; i < n; ++i)
        position[p[i]] = i;
    return guess(n);
}

void Run() {
    int testcase_count;
    std::cin >> testcase_count;
    std::cin >> max_query_count; // global variable

    Secret secret_input;
    Secret secret_output;

    for (int testid = 1; testid <= testcase_count; ++testid) {
        auto output = RunTestcase(secret_input);
        
        std::cout << static_cast<u32>(RunResult::Success) << '\n';
        for (int x : output)
            std::cout << secret_output.Protect(static_cast<u32>(x)) << ' ';
        std::cout << '\n';
    }

    std::cout << secret_output.Protect(query_count) << '\n';
    std::cout << secret_input.hash << ' ' << secret_output.hash << '\n';
}

}

int min_value(int i, int j) {
    int x= grader_h7ac::MinValue(i, j);
    return x;
}

int main(int argc, char** argv) {
    grader_h7ac::Run();
}