#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nl '\n'
typedef pair<int, int> pii;
struct info
{
ll sum = 0;
};
struct segtree
{
int n;
vector<info> t;
void init(int n, vector<ll> &a)
{
t.clear();
this->n = n;
t.resize(n * 4);
build(1, 0, n - 1, a);
}
info Merge(info l, info r)
{
info ret;
ret.sum = max(l.sum, r.sum);
return ret;
}
void build(int node, int l, int r, vector<ll> &a)
{
if (l == r)
{
t[node].sum = a[l];
return;
}
int mid = (l + r) / 2;
build(node * 2, l, mid, a);
build(node * 2 + 1, mid + 1, r, a);
t[node] = Merge(t[node * 2], t[node * 2 + 1]);
}
info query(int node, int l, int r, int i, int j)
{
if (l > j || r < i)
{
info nul;
return nul;
}
if (l >= i && r <= j)
return t[node];
int mid = (l + r) / 2;
return Merge(query(node * 2, l, mid, i, j), query(node * 2 + 1, mid + 1, r, i, j));
}
ll query(int l, int r)
{
auto ret = query(1, 0, n - 1, l, r);
return ret.sum;
}
};
struct HLD
{
vector<int> parent, depth, heavy, head, pos;
int cur_pos;
segtree seg;
int dfs(int v, vector<vector<pii>> const &adj, vector<ll> &a)
{
int size = 1;
int max_c_size = 0;
for (auto [c, w] : adj[v])
{
if (c != parent[v])
{
a[c] = w;
parent[c] = v, depth[c] = depth[v] + 1;
int c_size = dfs(c, adj, a);
size += c_size;
if (c_size > max_c_size)
max_c_size = c_size, heavy[v] = c;
}
}
return size;
}
void decompose(int v, int h, vector<vector<pii>> const &adj)
{
head[v] = h, pos[v] = cur_pos++;
if (heavy[v] != -1)
decompose(heavy[v], h, adj);
for (auto xx : adj[v])
{
int c = xx.first;
if (c != parent[v] && c != heavy[v])
decompose(c, c, adj);
}
}
void init(vector<vector<pii>> const &adj)
{
int n = adj.size();
parent = vector<int>(n);
depth = vector<int>(n);
heavy = vector<int>(n, -1);
head = vector<int>(n);
pos = vector<int>(n);
cur_pos = 0;
vector<ll> tmp(n), a(n);
dfs(0, adj, a);
decompose(0, 0, adj);
for (int i = 0; i < n; i++)
tmp[pos[i]] = a[i];
seg.init(n, tmp);
}
ll query(int a, int b)
{
ll res = 0;
for (; head[a] != head[b]; b = parent[head[b]])
{
if (depth[head[a]] > depth[head[b]])
swap(a, b);
ll cur_heavy_path_max = seg.query(pos[head[b]], pos[b]);
res = max(res, cur_heavy_path_max);
}
if (depth[a] > depth[b])
swap(a, b);
ll last_heavy_path_max = seg.query(pos[a] + 1, pos[b]);
res = max(res, last_heavy_path_max);
return res;
}
};
int main() {
int n, q; cin >> n >> q;
vector<vector<pii>> adj;
adj.resize(n);
for(int i=0;i<n-1;++i){
int u,v,w; cin >> u >> v >> w; u--,v--;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
HLD hd;
hd.init(adj);
while(q--){
int u,v; cin >> u >> v; u--,v--;
cout << hd.query(u,v) << nl;
}
}