#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c==‘-‘) f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-‘0‘;
return x*f;
}
typedef long long ll;
const int maxn=300010;
const int maxnode=maxn*20;
struct Query {int k,id,next;}Q[maxn];
int n,m,first2[maxn],first[maxn],next[maxn<<1],to[maxn<<1],dep[maxn],siz[maxn],e,cnt;
void AddEdge(int u,int v) {
to[++e]=v;next[e]=first[u];first[u]=e;
to[++e]=u;next[e]=first[v];first[v]=e;
}
void AddQuery(int x,int k,int id) {
Q[++cnt]=(Query){k,id,first2[x]};first2[x]=cnt;
}
ll s[maxnode],ans[maxn];
int ls[maxnode],rs[maxnode],root[maxn],ToT;
void build(int& o,int l,int r,int p,int val) {
if(!o) o=++ToT;s[o]+=val;if(l==r) return;
int mid=l+r>>1;
if(p<=mid) build(ls[o],l,mid,p,val);
else build(rs[o],mid+1,r,p,val);
}
int merge(int x,int y) {
if(!x) return y;
if(!y) return x;
s[x]+=s[y];
ls[x]=merge(ls[x],ls[y]);
rs[x]=merge(rs[x],rs[y]);
return x;
}
ll query(int o,int l,int r,int x) {
if(!o) return 0;
if(l==r) return s[o];
int mid=l+r>>1;
if(x<=mid) return query(ls[o],l,mid,x);
return s[ls[o]]+query(rs[o],mid+1,r,x);
}
void dfs(int x,int fa) {
siz[x]=1;dep[x]=dep[fa]+1;
ren if(to[i]!=fa) dfs(to[i],x),siz[x]+=siz[to[i]],root[x]=merge(root[x],root[to[i]]);
for(int i=first2[x];i;i=Q[i].next) {
int k=Q[i].k,j=Q[i].id;
ans[j]=(ll)(siz[x]-1)*min(k,dep[x]-1);
ans[j]+=query(root[x],1,n,min(n,dep[x]+k));
}
build(root[x],1,n,dep[x],siz[x]-1);
}
int main() {
n=read();m=read();
rep(i,2,n) AddEdge(read(),read());
rep(i,1,m) {
int p=read(),k=read();
AddQuery(p,k,i);
}
dfs(1,0);
rep(i,1,m) printf("%lld\n",ans[i]);
return 0;
}