#include #define fi first #define se second #define eb emplace_back #define mp make_pair using namespace std; typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef __int128 i128; template T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);} template T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);} template bool chmax(T &a,const S b) {return (a bool chmin(T &a,const S b) {return (a>b?a=b,1:0);} int popcnt(int x) {return __builtin_popcount(x);} int popcnt(ll x) {return __builtin_popcountll(x);} int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));} int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));} int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));} int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));} #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) typedef pair pii; typedef vector vi; typedef vector vp; int read() { int x=0,w=1; char c=getchar(); while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();} while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} return x*w; } const int N=2e5+9; int n,q,dfn[N],f[N][25],d[N],tick,sz[N]; vi e[N]; void dfs(int u,int fa) { dfn[u]=++tick, sz[u]=1; d[u]=d[fa]+1, f[u][0]=fa; rep(h,1,20) f[u][h]=f[f[u][h-1]][h-1]; for(int v:e[u]) if(v!=fa) { dfs(v,u), sz[u]+=sz[v]; } } int lca(int u,int v) { assert(d[u]>=d[v]); per(h,20,0) if(d[f[u][h]]>=d[v]) u=f[u][h]; per(h,20,0) if(f[u][h]!=f[v][h]) u=f[u][h], v=f[v][h]; return u==v?u:f[u][0]; } int find(int u,int x) { per(h,20,0) if(x&(1<d[v]) { return sz[p]-sz[fu]; } else { int fv=find(v,hd-1); return n-sz[fu]-sz[fv]; } } signed main() { n=read(), q=read(); rep(i,2,n) { int u=read(), v=read(); e[u].eb(v), e[v].eb(u); } dfs(1,0); rep(i,1,q) { int x=read(), y=read(); printf("%d\n",calc(x,y)); } return 0; }