//Tanuj Khattar
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> II;
typedef vector< II > VII;
typedef vector<int> VI;
typedef vector< VI > VVI;
typedef long long int LL;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
//FILE *fin = freopen("in","r",stdin);
//FILE *fout = freopen("out","w",stdout);
typedef struct node{
int val,prior,size;
struct node *l,*r;
}node;
typedef node* pnode;
int sz(pnode t){
return t?t->size:0;
}
void upd_sz(pnode t){
if(t)t->size = sz(t->l)+1+sz(t->r);
}
void split(pnode t,pnode &l,pnode &r,int key){
if(!t)l=r=NULL;
else if(t->val<=key)split(t->r,t->r,r,key),l=t;//elem=key comes in l
else split(t->l,l,t->l,key),r=t;
upd_sz(t);
}
void merge(pnode &t,pnode l,pnode r){
if(!l || !r)t=l?l:r;
else if(l->prior > r->prior)merge(l->r,l->r,r),t=l;
else merge(r->l,l,r->l),t=r;
upd_sz(t);
}
void insert(pnode &t,pnode it){
if(!t) t=it;
else if(it->prior>t->prior)split(t,it->l,it->r,it->val),t=it;
else insert(t->val<it->val?t->r:t->l,it);
upd_sz(t);
}
void erase(pnode &t,int key){
if(!t)return;
else if(t->val==key){pnode temp=t;merge(t,t->l,t->r);free(temp);}
else erase(t->val<key?t->r:t->l,key);
upd_sz(t);
}
pnode init(int val){
pnode ret = (pnode)malloc(sizeof(node));
ret->val=val;ret->size=1;ret->prior=rand();ret->l=ret->r=NULL;
return ret;
}
const int INF = int(1e9);
int kth(pnode t,int k)
{
if(!t)return INF;
if(sz(t->l) == k-1)
return t->val;
else if(sz(t->l)+1 < k)
return kth(t->r,k - sz(t->l)-1);
else if(sz(t->l) >= k)
return kth(t->l,k);
}
const int N = int(1e5)+10;
VII g[N];
int T;
int Start[N],End[N];
int E[N];
pair<II,II> Q[N];
void dfs(int u,int p,int ww)
{
++T;
Start[u]=T;
E[T]=ww;
for(int i=0;i<SZ(g[u]);i++)
{
int w = g[u][i].F;
if(w==p)continue;
dfs(w,u,g[u][i].S);
}
End[u]=T;
}
int sqt;
bool cmp(pair<II,II> a,pair<II,II> b)
{
if(a.F.F/sqt != b.F.F/sqt)
return a.F.F/sqt < b.F.F/sqt;
return a.F.S<b.F.S;
}
void print(pnode t)
{
if(!t)return;
print(t->l);
printf("%d ",t->val);
print(t->r);
}
int ans[N];
int main()
{
int n,q;
si(n);si(q);
for(int i=0;i<n-1;i++)
{
int u,v,w;
si(u);si(v);si(w);
g[u].PB(MP(v,w));
g[v].PB(MP(u,w));
}
dfs(1,1,0);
for(int i=0;i<q;i++)
{
int u,k;si(u);si(k);
Q[i].F.F = 1+Start[u];
Q[i].F.S = End[u];
Q[i].S.F = k;
Q[i].S.S = i;
}
sqt = sqrt(2*n);
sort(Q,Q+q,cmp);
int L = 2,R = 2;
pnode head=NULL;
insert(head,init(E[L]));
for(int i=0;i<q;i++)
{
int l = Q[i].F.F;
int r = Q[i].F.S;
int k = Q[i].S.F;
int pos = Q[i].S.S;
if(l>r)
{
ans[pos]=INF;
continue;
}
while(L<l)
{
if(!(l<=L && L<=r))erase(head,E[L]);
L++;
}
while(L>l)
{
L--;
if(l<=L && L<=r)insert(head,init(E[L]));
}
while(R<r)
{
R++;
if(l<=R && R<=r)insert(head,init(E[R]));
}
while(R>r)
{
if(!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