#include<bits/stdc++.h>
#include "island.h"
using namespace std;
struct DSU{
vector<int> e;
DSU(int N) {e=vector<int>(N+1, -1);}
int get(int x) {return e[x] < 0 ? x : e[x]=get(e[x]);}
int size(int x) {return -e[get(x)];}
bool unite(int a, int b) {
a=get(a); b=get(b);
if(a == b) return false;
if(e[a] > e[b]) swap(a, b);
e[a]+=e[b]; e[b]=a;
return true;
}
};
void solve(int N, int L) {
vector<int> first(N+1), second(N+1);
vector<pair<int, int>> ans;
DSU dsu(N+1);
for(int i=1; i<=N; i++) {
first[i]=query(i, 1);
ans.push_back({min(first[i], i), max(first[i], i)});
dsu.unite(i, first[i]);
second[i]=query(i, 2);
}
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
if(second[j] == i && dsu.unite(j, i)) {
ans.push_back({min(j, i), max(j, i)});
}
}
}
sort(ans.begin(), ans.end());
for(int i=0; i<ans.size(); i++) {
if(i != 0 && ans[i] == ans[i-1]) continue;
// cerr << ans[i].first << ' ' << ans[i].second << endl;
answer(ans[i].first, ans[i].second);
}
}