#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
const int maxn = 2e5;
vector<pair<int, int>> g[maxn];
vector<int> que[maxn];
ordered_set e[maxn];
int knum[maxn];
int ans[maxn];
void dfs(int v = 1, int p = 0)
{
for(auto it: g[v])
{
int u = it.first;
int c = it.second;
if(u == p)
continue;
e[v].insert({c, u});
dfs(u, v);
if(e[v].size() < e[u].size())
e[v].swap(e[u]);
for(auto it: e[u])
e[v].insert(it);
}
for(auto it: que[v])
{
if(knum[it] > e[v].size())
ans[it] = 1e9;
else
ans[it] = e[v].find_by_order(knum[it] - 1) -> first;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for(int i = 0; i < n - 1; i++)
{
int a, b, c;
cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
for(int i = 1; i <= q; i++)
{
int u, k;
cin >> u >> k;
knum[i] = k;
que[u].push_back(i);
}
dfs();
for(int i = 1; i <= q; i++)
{
if(ans[i] == 1e9)
cout << "nan";
else
cout << ans[i];
cout << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIG5hbWVzcGFjZSBfX2dudV9wYmRzOwoKdHlwZWRlZiB0cmVlPApwYWlyPGludCwgaW50PiwKbnVsbF90eXBlLApsZXNzPHBhaXI8aW50LCBpbnQ+PiwKcmJfdHJlZV90YWcsCnRyZWVfb3JkZXJfc3RhdGlzdGljc19ub2RlX3VwZGF0ZT4gb3JkZXJlZF9zZXQ7Cgpjb25zdCBpbnQgbWF4biA9IDJlNTsKCnZlY3RvcjxwYWlyPGludCwgaW50Pj4gZ1tt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