#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <cmath>
using namespace std;
const int MAXN = 100000;
const int LOG = 17;
vector<pair<int, int>> adj[MAXN + 1];
int up[MAXN + 1][LOG];
int maxEdge[MAXN + 1][LOG];
int depth[MAXN + 1];
void dfs(int node, int parent, int weight) {
up[node][0] = parent;
maxEdge[node][0] = weight;
for (int i = 1; i < LOG; i++) {
up[node][i] = up[up[node][i-1]][i-1];
maxEdge[node][i] = max(maxEdge[node][i-1], maxEdge[up[node][i-1]][i-1]);
}
for (auto &[next, w] : adj[node]) {
if (next != parent) {
depth[next] = depth[node] + 1;
dfs(next, node, w);
}
}
}
int query(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
int maxRes = INT_MIN;
for (int i = LOG - 1; i >= 0; i--) {
if (depth[u] - (1 << i) >= depth[v]) {
maxRes = max(maxRes, maxEdge[u][i]);
u = up[u][i];
}
}
if (u == v) return maxRes;
for (int i = LOG - 1; i >= 0; i--) {
if (up[u][i] != up[v][i]) {
maxRes = max({maxRes, maxEdge[u][i], maxEdge[v][i]});
u = up[u][i];
v = up[v][i];
}
}
maxRes = max({maxRes, maxEdge[u][0], maxEdge[v][0]});
return maxRes;
}
int main() {
int N;
cin >> N;
int K; cin >> K;
for (int i = 1; i < N; i++) {
int A, B, C;
cin >> A >> B >> C;
adj[A].emplace_back(B, C);
adj[B].emplace_back(A, C);
}
for (int i = 1; i <= N; i++) {
for (int j = 0; j < LOG; j++) {
up[i][j] = -1;
maxEdge[i][j] = INT_MIN;
}
}
// Preprocess
depth[1] = 0;
dfs(1, -1, 0);
// Queries
while (K--) {
int D, E;
cin >> D >> E;
auto maxLength = query(D, E);
cout << maxLength << "\n";
}
return 0;
}