#include <cstdio>
#include <iostream>
using namespace std;
const int N = 2e4 + 5;
struct Edge{
int v;
int w;
int next;
}edge[N << 1];
int head[N], cnt, idx, arr[N];
int n, m;
struct Node{
int father;
int son;
int id;
int top;
int depth;
int size;
int dis;
}nodes[N];
int tree[N << 2];
void bulidTree(int node, int lef, int rig)
{
if (lef == rig)
tree[node] = arr[lef];
else
{
int leftNode = node << 1;
int rightNode = node << 1 | 1;
int mid = (lef + rig) >> 1;
bulidTree(leftNode, lef, mid);
bulidTree(rightNode, mid+1, rig);
tree[node] = tree[leftNode] + tree[rightNode];
}
}
void add(int u, int v, int w)
{
edge[++cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt;
}
int dfs1(int u, int fa)
{
nodes[u].father = fa;
nodes[u].size = 1;
nodes[u].depth = nodes[fa].depth + 1;
int maxSize = -1;
for (int i = head[u]; i; i = edge[i].next)
{
int v = edge[i].v;
if (v == fa)
continue;
nodes[v].dis = edge[i].w;
int tmpSize = dfs1(v, u);
nodes[u].size += tmpSize;
if (tmpSize > maxSize)
{
maxSize = tmpSize;
nodes[u].son = v;
}
}
return nodes[u].size;
}
void dfs2(int u, int tp)
{
nodes[u].id = ++idx;
arr[idx] = nodes[u].dis;
nodes[u].top = tp;
if (!nodes[u].son)
return;
dfs2(nodes[u].son, tp);
for (int i = head[u]; i; i = edge[i].next)
{
int v = edge[i].v;
if (v == nodes[u].father || v == nodes[u].son)
continue;
dfs2(v, v);
}
}
int queryTree(int node, int lef, int rig, int L, int R)
{
if (L <= lef && rig <= R)
return tree[node];
int res = 0;
int leftNode = node << 1;
int rightNode = node << 1 | 1;
int mid = (lef + rig) >> 1;
if (L <= mid)
res += queryTree(leftNode, lef, mid, L, R);
if (R > mid)
res += queryTree(rightNode, mid+1, rig, L, R);
return res;
}
int queryRange(int u, int v)
{
int res = 0;
while (nodes[u].top != nodes[v].top)
{
if (nodes[nodes[u].top].depth < nodes[nodes[v].top].depth)
swap(u, v);
res += queryTree(1, 1, n, nodes[nodes[u].top].id, nodes[u].id);
u = nodes[nodes[u].top].father;
}
if (nodes[u].depth > nodes[v].depth)
swap(u, v);
res += queryTree(1, 1, n, nodes[u].id+1, nodes[v].id);
return res;
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 0; i < n-1; ++i)
{
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
int root = 1;
dfs1(root, 0);
dfs2(root, root);
// for (int i = 1; i <= n; ++i)
// {
// Node &s = nodes[i];
// printf("father:%d, son:%d, id:%d, top:%d, depth:%d, size:%d, dis:%d\n",
// s.father, s.son, s.id, s.top, s.depth, s.size, s.dis);
// }
bulidTree(1, 1, n);
while (m--)
{
int u, v;
scanf("%d %d", &u, &v);
printf("%d\n", queryRange(u, v));
}
return 0;
}
orz