/*
* Copyright (c) 2024 Emon Thakur
* All rights reserved.
*/
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using minheap = priority_queue<long long, vector<long long>, greater<long long>>;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
#define ll long long
#define ld long double
#define MOD 1000000007
#define pie 2*(acos(0.0))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define endl '\n'
#define lcm(a,b) (a*b)/(__gcd<ll>(a,b))
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define life_is_a_race ios::sync_with_stdio(false); cin.tie(nullptr);
const int N = 1e5 + 10;
vector<pair<int,int>>g[N];
int table[N + 1][22];
int level[N];
int tin[N],tout[N];
int maxLen[N + 1][22];
int n, lg, Time = 0, INF = 1e9 + 10;
void dfs(int v, int par = -1, int dep = 0,int mx = -1){
// cout << v << endl;
tin[v] = ++Time;
table[v][0] = par;
level[v] = dep;
maxLen[v][0] = mx;
for(int i = 1; i <= lg; i++){
if(table[v][i - 1] != -1){
table[v][i] = table[table[v][i - 1]][i - 1];
maxLen[v][i] = max(maxLen[v][i - 1], maxLen[table[v][i - 1]][i - 1]);
}
}
for(auto &child : g[v]){
if(child.first == par)continue;
dfs(child.first, v, dep + 1, child.second);
}
tout[v] = ++Time;
}
int lca_query(int a, int b){
if(level[a] < level[b])swap(a, b);
int dis = level[a] - level[b];
int mx = 0;
while(dis) {
int i = log2l(dis);
mx = max(mx, maxLen[a][i]);
a = table[a][i], dis -= (1 << i);
}
if(a == b) return mx;
for(int i = lg; i >= 0; i--){
if(table[a][i] != -1 && table[a][i] != table[b][i]){
mx = max(mx, maxLen[a][i]);
mx = max(mx, maxLen[b][i]);
a = table[a][i], b = table[b][i];
}
}
mx = max(mx, maxLen[a][0]);
mx = max(mx, maxLen[b][0]);
return mx;
}
void solve(int tc)
{
//cout<<"Case "<<tc<<": ";
int n, q;
cin >> n >> q;
lg = log2l(n) + 1;
for(int i = 0; i <= n; i++){
for(int j = 0; j <= lg; j++){
table[i][j] = -1;
maxLen[i][j] = -1;
}
}
for(int i = 1; i < n; i++){
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dfs(1);
while(q--){
int a, b;
cin >> a >> b;
cout << lca_query(a, b) << endl;
}
}
int main()
{
life_is_a_race
int t=1;
// cin>>t;
for(int i=1;i<=t;i++) solve(i);
}