#include<bits/stdc++.h>
using namespace std;
const int N = 210000;
const int L;
int n, q, arr[N], l[N], cnt, v[N];
vector<pair<int,int> > g[N];
int blk[N], sum[N], ans[N], num[N];
void dfs(int x, int fa) {
l[x] = ++cnt, arr[cnt] = x;
for (auto p: g[x]) {
int y = p.first, w = p.second;
if (y == fa) continue;
v[y] = w;
dfs(y, x);
}
arr[++cnt] = x;
}
struct query {
int l, r, id;
friend bool operator < (query a, query b) {
if (a.l / L != b.l / L) return a.l / L < b.l / L;
if ((a.l / L) & 1) return a.r > b.r;
else return a.r < b.r;
}
}a[N];
void dec(int x) {
x = arr[x];
int w = v[x];
if (v[x] > n) return;
num[x]++;
if (num[x] % 2 == 0) {
blk[w]--;
if (blk[w] == 0) sum[w / L]--;
}
else {
blk[w]++;
if (blk[w] == 1) sum[w / L]++;
}
}
int main() {
cin >> n >> q;
L=sqrt(n);
for(int i=1;i<n;i++) {
int x, y, w;
cin >> x >> y >> w;
g[x].push_back({y,w});
g[y].push_back({x,w});
}
v[1] = n+1;
dfs(1, 0);
for(int i=1;i<=q;i++) {
int x, y;
cin >> x >> y;
if (l[x] > l[y]) swap(x, y);
a[i] = {l[x], l[y], i};
}
sort(a + 1, a + q + 1);
int l = 1, r = 1;
for(int i=1;i<=q;i++) {
while (r < a[i].r) dec(++r);
while (l > a[i].l) dec(l--);
while (r > a[i].r) dec(r--);
while (l < a[i].l) dec(++l);
for (int j = 0; ; j++)
if (sum[j] != L)
{
int cur = j * L;
while (blk[cur]) cur++;
ans[a[i].id] = cur;
break;
}
}
for(int i=1;i<=q;i++)
cout << ans[i] << "\n";
return 0;
}