#include "dango3.h"
#include <bits/stdc++.h>
#include <vector>
using namespace std;
namespace {
int variable_example = 1;
int n, m;
} // namespace
// excluding exc, find first pos in [l, r] which returns 1
int func(int l, int r, const vector<int>& exc = {}, const vector<int>& inc = {}) {
static vector<int> ban;
static int timer = 1;
static vector<int> seen;
if ((int)seen.size() != n * m) {
seen.assign(n * m, 0);
timer = 1;
}
++timer;
if (timer == INT_MAX) {
fill(seen.begin(), seen.end(), 0);
timer = 1;
}
for (int x : exc) seen[x] = timer;
for (int x : inc) seen[x] = timer;
vector<int> cand;
cand.reserve(r - l + 1);
for (int i = l; i <= r; i++) {
if (seen[i] != timer) cand.push_back(i);
}
if (cand.empty()) return l;
auto ask = [&](int upto) {
vector<int> qry;
qry.reserve((int)inc.size() + upto + 1);
for (int x : inc) qry.push_back(x + 1);
for (int i = 0; i <= upto; i++) qry.push_back(cand[i] + 1);
return Query(qry);
};
int lo = -1;
int hi = (int)cand.size() - 1;
while (hi > lo + 1) {
int mid = (lo + hi) / 2;
if (ask(mid) > 0) hi = mid;
else lo = mid;
}
return cand[hi];
}
void Solve(int N, int M) {
n = N; m = M;
vector<vector<int>> block(N);
int rp = n * m - 1;
vector<int> inctmp;
for (int i = 0; i < N; i++) {
int f = func(0, rp, {}, inctmp);
block[i].push_back(f);
inctmp.push_back(f);
rp = f - 1;
}
for (int i = 0; i < N; i++) {
for (int j = 1; j < M; j++) {
vector<int> dis;
dis.reserve(block[i].size());
for (int x : block[i]) dis.push_back(x);
vector<int> inc;
inc.reserve(N - 1);
for (int other = 0; other < N; other++) {
if (other == i) continue;
inc.push_back(block[other][0]);
}
int nxt = func(0, n * m - 1, dis, inc);
block[i].push_back(nxt);
}
}
for (int i = 0; i < M; i++) {
vector<int> cur;
cur.reserve(N);
for (int j = 0; j < N; j++) {
cur.push_back(block[j][i] + 1);
}
Answer(cur);
}
}