#include "Anyalib.h"
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
int n;
vector<int> depth, dcount, vect, dist, w, pd;
vector<vector<pii> > graph;
void dfs(int node, int p, int d){
depth[node]=d;
++dcount[d%10];
for (auto num:graph[node])if (num.fi!=p)dfs(num.fi, node, d+1);
}
void InitAnya(int N , int A[] , int B[]){
n=N;
graph.resize(n);
pd.resize(n);
depth.resize(n);
w.resize(n-1, 0);
dcount.resize(10, 0);
for (int i=0; i<n-1; ++i){
graph[A[i]].pb(mp(B[i], i));
graph[B[i]].pb(mp(A[i], i));
}
dfs(0, -1, 0);
int mn=2*n, id=-1;
for (int i=0; i<10; ++i)if (dcount[i]<mn)mn=dcount[i], id=i;
for (int i=0; i<n; ++i)if (depth[i]%10==id)vect.pb(i);
}
void dfs2(int node, int p, int d, int t){
pd[node]=t;
dist[node]=d;
for (auto num:graph[node])if (num.fi!=p)dfs2(num.fi, node, d+w[num.se], w[num.se]);
}
void Anya(int C[]){
int id=0;
dist.resize(n);
for (int i=0; i<n-1; ++i)w[i]=C[i];
dfs2(0, -1, 0, 0);
for (auto c:vect)for (int i=0; i<9; ++i)Save(id, !!(dist[c]&(1<<i))), ++id;
for (int i=0; i<n; ++i)Save(i+500, pd[i]);
}
#include "Borislib.h"
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
int n;
vector<int> depth, dcount, vect, dist, in, par;
vector<vector<pii> > graph;
void dfs(int node, int p, int d){
par[node]=p;
depth[node]=d;
++dcount[d%10];
for (auto num:graph[node])if (num.fi!=p)dfs(num.fi, node, d+1);
}
void InitBoris(int N , int A[] , int B[]){
n=N;
graph.resize(n);
par.resize(n);
in.resize(n, 0);
depth.resize(n);
dcount.resize(10, 0);
for (int i=0; i<n-1; ++i){
graph[A[i]].pb(mp(B[i], i));
graph[B[i]].pb(mp(A[i], i));
}
dfs(0, -1, 0);
int mn=2*n, id=-1;
for (int i=0; i<10; ++i)if (dcount[i]<mn)mn=dcount[i], id=i;
for (int i=0; i<n; ++i)if (depth[i]%10==id)vect.pb(i), in[i]=1;
}
int Boris(int a){
int ans=0;
while (a&&!in[a])ans+=Ask(a+500), a=par[a];
if (a){
int id=0;
for (;id<vect.size();++id)if (vect[id]==a)break;
for (int i=0; i<9; ++i)ans+=(1<<i)*Ask(9*id+i);
}
return ans;
}