#include<bits/stdc++.h>
#include"tree.h"
using namespace std;
typedef vector<int> ve;
int pl[1005],d[1005],n,fa[1005],a[1005],dis[1005],f[1005],h[1005],sz[1005],is[1005],rt;
vector<int> g[1005];
mt19937_64 rng(time(0));
void dp1(int x,int fa){
sz[x]=is[x],f[x]=0;
for(int y:g[x]){
if(y==fa)continue;
dp1(y,x);
sz[x]+=sz[y];
f[x]+=f[y]+sz[y];
}
}
void dp2(int x,int fa){
for(int y:g[x]){
if(y==fa)continue;
h[y]=h[x]+f[x]-f[y]-sz[y]+sz[rt]-sz[y];
dp2(y,x);
}
}
void solver(int n,int A,int B){
if(n==1)return ;
rt=rng()%n+1;
pl[1]=rt;
int ttt=0;
for(int i=1;i<=n;i++)if(i!=rt)d[i]=ask(rt,(ve){i}),pl[++tot]=i;
sort(pl+2,pl+n+1,[](int x,int y){return d[x]<d[y];});
int tot=0;
for(int i=2;i<=n;i++){
int x=pl[i];
ve might;
for(int j=1;j<i;j++)if(d[pl[j]]==d[x]-1)might.push_back(pl[j]);
while(might.size()>1){
ve ls;
for(int i=1;i<=n;i++)is[i]=0;
for(int i=0;i<might.size()/2;i++)ls.push_back(might[i]),is[might[i]]=1;
int d=ask(x,ls);
ttt+=ls.size();
if(ttt>100000) exit(1);
dp1(rt,0),dp2(rt,0);
ve nw;
for(int i:might)if(f[i]+h[i]+ls.size()==d)nw.push_back(i);
might=nw;
}
fa[x]=might[0];
g[fa[x]].push_back(x);
g[x].push_back(fa[x]);
answer(x,fa[x]);
}
}