#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define bug(a) cerr << #a << " : " << a << endl;
#define FastRead ios_base::sync_with_stdio(false);cin.tie(NULL);
const ll MAX = 1e5+10;
vector<ll>adj[MAX] , ans[MAX] , cnt[MAX] , subb[MAX];
ll n , dep[MAX] , T[MAX] , P[MAX][30] , cum[MAX][30] , root;
map<pii,ll>cost;
void DFS(ll src,ll par,ll lev)
{
dep[src] = lev;
T[src] = par;
for(ll i=0;i<adj[src].size();i++)
{
ll x = adj[src][i];
if(x == par)
continue;
cum[x][0] = cost[{src,x}];
DFS(x,src,lev+1);
}
}
void initLCA()
{
memset(P,-1,sizeof P);
for(ll i=1;i<=n;i++)
P[i][0] = T[i];
for(ll j=1; 1<<j <n;j++)
{
for(ll i=1;i<=n;i++)
{
if(P[i][j-1] != -1)
{
P[i][j] = P[P[i][j-1]][j-1];
cum[i][j] = cum[i][j-1] + cum[P[i][j-1]][j-1];
}
}
}
}
ll lca_query(ll u,ll v)
{
if(dep[u] < dep[v])
swap(u,v);
ll log = log2(dep[u]);
ll sum = 0;
for(ll i=log;i>=0;i--)
{
if(dep[u]-(1<<i) >= dep[v])
{
sum += cum[u][i];
u = P[u][i];
}
}
if(u == v)
return sum;
for(ll i=log;i>=0;i--)
{
if(P[u][i] != -1 && P[u][i] != P[v][i])
{
sum += cum[u][i]+cum[v][i];
u = P[u][i];
v = P[v][i];
}
}
return sum + cum[u][0] + cum[v][0];
}
ll dist(ll u,ll v)
{
return lca_query(u,v);
}
struct CentroidDecomposition
{
ll path[MAX] , sub[MAX];
bool vis[MAX];
CentroidDecomposition()
{
memset(vis,0,sizeof vis);
memset(path,0,sizeof path);
}
void subDFS(ll src,ll par)
{
sub[src] = 1;
for(auto i : adj[src])
{
if(i == par || vis[i])
continue;
subDFS(i,src);
sub[src] += sub[i];
}
}
ll centroid(ll src,ll par,ll sz)
{
for(auto i : adj[src])
{
if(i == par || vis[i])
continue;
else if(sub[i] > sz)
return centroid(i,src,sz);
}
return src;
}
ll decompose(ll src,ll par)
{
subDFS(src,-1);
ll c = centroid(src,-1,sub[src]/2);
vis[c] = 1;
path[c] = par;
for(auto i : adj[c])
{
if(!vis[i])
{
ll child = decompose(i,c);
for(auto j : subb[child])
subb[c].push_back(j);
}
}
subb[c].push_back(c);
for(auto i : subb[c])
{
ans[c].push_back(dist(c,i));
if(par != -1)
cnt[c].push_back(dist(par,i));
}
return c;
}
} tree;
struct QueryHandler
{
ll query(ll u,ll l)
{
ll cur = u;
ll ret = upper_bound(ans[u].begin(),ans[u].end(),l)-ans[u].begin();
while(1)
{
if(tree.path[cur] == -1)
break;
ll dd = l-dist(u,tree.path[cur]);
ll add = upper_bound(ans[tree.path[cur]].begin(),ans[tree.path[cur]].end(),dd)-ans[tree.path[cur]].begin();
ll sub = upper_bound(cnt[u].begin(),cnt[u].end(),dd)-cnt[u].begin();
//cout << ">>> " << add << " - " << sub << endl;
ret += add-sub;
cur = tree.path[cur];
}
return ret;
}
} ds;
int main()
{
#ifdef Aaman007
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif // Aaman007
FastRead
ll u,v,q;
ll l,w;
cin >> n >> q;
for(ll i=0;i<n-1;i++)
{
cin >> u >> v >> w;
adj[u].push_back(v);
adj[v].push_back(u);
cost[{u,v}] = cost[{v,u}] = w;
}
DFS(1,1,0);
initLCA();
tree.decompose(1,-1);
for(ll i=1;i<=n;i++)
{
sort(ans[i].begin(),ans[i].end());
sort(cnt[i].begin(),cnt[i].end());
}
while(q--)
{
cin >> u >> l;
cout << ds.query(u,l) << endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgbGwgICAgICAgICAgIGxvbmcgbG9uZwojZGVmaW5lIHBpaSAgICAgICAgICBwYWlyPGxsLGxsPgojZGVmaW5lIGJ1ZyhhKSAgICAgICBjZXJyIDw8ICNhIDw8ICIgOiAiIDw8IGEgPDwgZW5kbDsKI2RlZmluZSBGYXN0UmVhZCAgICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7Y2luLnRpZShOVUxMKTsKCmNvbnN0IGxsIE1BWCA9IDFlNSsxMDsKCnZlY3RvcjxsbD5hZGpbTUFYXSAsIGFuc1tNQVhdICwgY250W01BWF0gLCBzdWJiW01BWF07CmxsIG4gLCBkZXBbTUFYXSAsIFRbTUFYXSAsIFBbTUFYXVszMF0gLCBjdW1bTUFYXVszMF0gLCByb290OwptYXA8cGlpLGxsPmNvc3Q7Cgp2b2lkIERGUyhsbCBzcmMsbGwgcGFyLGxsIGxldikKewogICAgZGVwW3NyY10gPSBsZXY7CiAgICBUW3NyY10gPSBwYXI7CgogICAgZm9yKGxsIGk9MDtpPGFkaltzcmNdLnNpemUoKTtpKyspCiAgICB7CiAgICAgICAgbGwgeCA9IGFkaltzcmNdW2ldOwoKICAgICAgICBpZih4ID09IHBhcikKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgY3VtW3hdWzBdID0gY29zdFt7c3JjLHh9XTsKICAgICAgICBERlMoeCxzcmMsbGV2KzEpOwogICAgfQp9CnZvaWQgaW5pdExDQSgpCnsKICAgIG1lbXNldChQLC0xLHNpemVvZiBQKTsKCiAgICBmb3IobGwgaT0xO2k8PW47aSsrKQogICAgICAgIFBbaV1bMF0gPSBUW2ldOwogICAgZm9yKGxsIGo9MTsgMTw8aiA8bjtqKyspCiAgICB7CiAgICAgICAgZm9yKGxsIGk9MTtpPD1uO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKFBbaV1bai0xXSAhPSAtMSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgUFtpXVtqXSA9IFBbUFtpXVtqLTFdXVtqLTFdOwogICAgICAgICAgICAgICAgY3VtW2ldW2pdID0gY3VtW2ldW2otMV0gKyBjdW1bUFtpXVtqLTFdXVtqLTFdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CmxsIGxjYV9xdWVyeShsbCB1LGxsIHYpCnsKICAgIGlmKGRlcFt1XSA8IGRlcFt2XSkKICAgICAgICBzd2FwKHUsdik7CgogICAgbGwgbG9nID0gbG9nMihkZXBbdV0pOwogICAgbGwgc3VtID0gMDsKICAgIGZvcihsbCBpPWxvZztpPj0wO2ktLSkKICAgIHsKICAgICAgICBpZihkZXBbdV0tKDE8PGkpID49IGRlcFt2XSkKICAgICAgICB7CiAgICAgICAgICAgIHN1bSArPSBjdW1bdV1baV07CiAgICAgICAgICAgIHUgPSBQW3VdW2ldOwogICAgICAgIH0KICAgIH0KICAgIGlmKHUgPT0gdikKICAgICAgICByZXR1cm4gc3VtOwogICAgZm9yKGxsIGk9bG9nO2k+PTA7aS0tKQogICAgewogICAgICAgIGlmKFBbdV1baV0gIT0gLTEgJiYgUFt1XVtpXSAhPSBQW3ZdW2ldKQogICAgICAgIHsKICAgICAgICAgICAgc3VtICs9IGN1bVt1XVtpXStjdW1bdl1baV07CiAgICAgICAgICAgIHUgPSBQW3VdW2ldOwogICAgICAgICAgICB2ID0gUFt2XVtpXTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gc3VtICsgY3VtW3VdWzBdICsgY3VtW3ZdWzBdOwp9CmxsIGRpc3QobGwgdSxsbCB2KQp7CiAgICByZXR1cm4gbGNhX3F1ZXJ5KHUsdik7Cn0Kc3RydWN0IENlbnRyb2lkRGVjb21wb3NpdGlvbgp7CiAgICBsbCBwYXRoW01BWF0gLCBzdWJbTUFYXTsKICAgIGJvb2wgdmlzW01BWF07CgogICAgQ2VudHJvaWREZWNvbXBvc2l0aW9uKCkKICAgIHsKICAgICAgICBtZW1zZXQodmlzLDAsc2l6ZW9mIHZpcyk7CiAgICAgICAgbWVtc2V0KHBhdGgsMCxzaXplb2YgcGF0aCk7CiAgICB9CgogICAgdm9pZCBzdWJERlMobGwgc3JjLGxsIHBhcikKICAgIHsKICAgICAgICBzdWJbc3JjXSA9IDE7CgogICAgICAgIGZvcihhdXRvIGkgOiBhZGpbc3JjXSkKICAgICAgICB7CiAgICAgICAgICAgIGlmKGkgPT0gcGFyIHx8IHZpc1tpXSkKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwoKICAgICAgICAgICAgc3ViREZTKGksc3JjKTsKICAgICAgICAgICAgc3ViW3NyY10gKz0gc3ViW2ldOwogICAgICAgIH0KICAgIH0KICAgIGxsIGNlbnRyb2lkKGxsIHNyYyxsbCBwYXIsbGwgc3opCiAgICB7CiAgICAgICAgZm9yKGF1dG8gaSA6IGFkaltzcmNdKQogICAgICAgIHsKICAgICAgICAgICAgaWYoaSA9PSBwYXIgfHwgdmlzW2ldKQogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIGVsc2UgaWYoc3ViW2ldID4gc3opCiAgICAgICAgICAgICAgICByZXR1cm4gY2VudHJvaWQoaSxzcmMsc3opOwogICAgICAgIH0KICAgICAgICByZXR1cm4gc3JjOwogICAgfQogICAgbGwgZGVjb21wb3NlKGxsIHNyYyxsbCBwYXIpCiAgICB7CiAgICAgICAgc3ViREZTKHNyYywtMSk7CiAgICAgICAgbGwgYyA9IGNlbnRyb2lkKHNyYywtMSxzdWJbc3JjXS8yKTsKICAgICAgICB2aXNbY10gPSAxOwogICAgICAgIHBhdGhbY10gPSBwYXI7CgogICAgICAgIGZvcihhdXRvIGkgOiBhZGpbY10pCiAgICAgICAgewogICAgICAgICAgICBpZighdmlzW2ldKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBsbCBjaGlsZCA9IGRlY29tcG9zZShpLGMpOwogICAgICAgICAgICAgICAgZm9yKGF1dG8gaiA6IHN1YmJbY2hpbGRdKQogICAgICAgICAgICAgICAgICAgIHN1YmJbY10ucHVzaF9iYWNrKGopOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHN1YmJbY10ucHVzaF9iYWNrKGMpOwogICAgICAgIGZvcihhdXRvIGkgOiBzdWJiW2NdKQogICAgICAgIHsKICAgICAgICAgICAgYW5zW2NdLnB1c2hfYmFjayhkaXN0KGMsaSkpOwogICAgICAgICAgICBpZihwYXIgIT0gLTEpCiAgICAgICAgICAgICAgICBjbnRbY10ucHVzaF9iYWNrKGRpc3QocGFyLGkpKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGM7CiAgICB9Cn0gdHJlZTsKCnN0cnVjdCBRdWVyeUhhbmRsZXIKewogICAgbGwgcXVlcnkobGwgdSxsbCBsKQogICAgewogICAgICAgIGxsIGN1ciA9IHU7CiAgICAgICAgbGwgcmV0ID0gdXBwZXJfYm91bmQoYW5zW3VdLmJlZ2luKCksYW5zW3VdLmVuZCgpLGwpLWFuc1t1XS5iZWdpbigpOwoKICAgICAgICB3aGlsZSgxKQogICAgICAgIHsKICAgICAgICAgICAgaWYodHJlZS5wYXRoW2N1cl0gPT0gLTEpCiAgICAgICAgICAgICAgICBicmVhazsKCiAgICAgICAgICAgIGxsIGRkID0gbC1kaXN0KHUsdHJlZS5wYXRoW2N1cl0pOwogICAgICAgICAgICBsbCBhZGQgPSB1cHBlcl9ib3VuZChhbnNbdHJlZS5wYXRoW2N1cl1dLmJlZ2luKCksYW5zW3RyZWUucGF0aFtjdXJdXS5lbmQoKSxkZCktYW5zW3RyZWUucGF0aFtjdXJdXS5iZWdpbigpOwogICAgICAgICAgICBsbCBzdWIgPSB1cHBlcl9ib3VuZChjbnRbdV0uYmVnaW4oKSxjbnRbdV0uZW5kKCksZGQpLWNudFt1XS5iZWdpbigpOwoKICAgICAgICAgICAgLy9jb3V0IDw8ICI+Pj4gIiA8PCBhZGQgPDwgIiAtICIgPDwgc3ViIDw8IGVuZGw7CgogICAgICAgICAgICByZXQgKz0gYWRkLXN1YjsKICAgICAgICAgICAgY3VyID0gdHJlZS5wYXRoW2N1cl07CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICB9Cn0gZHM7CgppbnQgbWFpbigpCnsKI2lmZGVmIEFhbWFuMDA3CiAgICBmcmVvcGVuKCJpbnB1dC50eHQiLC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