'''
先遍历求解每一个点到根节点的距离ids,
然后对于每一个询问a, b
求出a, b的最近公共足祖先c
ans = dis(a) + dis(b) - dis(c) * 2
'''
import math
from collections import deque
class LCA:
# edges 为所有树中的无向边,max_node_num为节点数,节点编号为0, 1, 2, ...... (max_node_num-1)
# 0号节点是整个树的根
def __init__(self, edges, max_node_num):
self.max_node_num = max_node_num
link = [[] for i in range(max_node_num)]
self.__max_dep = 0
for a, b in edges:
link[a].append(b)
link[b].append(a)
self.dep = [0] * self.max_node_num # 每个节点的深度
fa = [0] * self.max_node_num # 每个节点的父节点
# bfs一次把每个节点的父节点和深度求出
def __bfs():
que = deque()
que.append(0)
visit = [0] * self.max_node_num
visit[0] = 1
depth = 0
while len(que) > 0:
node_num = len(que)
for _ in range(node_num):
cur = que.popleft()
self.dep[cur] = depth
self.__max_dep = max(self.__max_dep, depth)
for child in link[cur]:
if visit[child] == 0:
visit[child] = 1
fa[child] = cur
que.append(child)
depth += 1
__bfs()
# f[i][j] 表示节点向上跳跃2^j步后的祖先节点标号
self.max_j = int(math.log2(self.__max_dep)) + 1
self.f = [[0] * (self.max_j + 1) for _ in range(self.max_node_num)]
for i in range(self.max_node_num):
self.f[i][0] = fa[i]
for j in range(1, self.max_j+1):
for i in range(self.max_node_num):
self.f[i][j] = self.f[ self.f[i][j-1] ][j-1]
# 获取a, b 两个节点的最近公共祖先编号
def get_lca(self, a, b):
if self.dep[a] > self.dep[b]:
a, b = b, a
sub_dep = self.dep[b] - self.dep[a]
if sub_dep > 0:
# b 移动到和 a 同一层
j = 0
while sub_dep:
if sub_dep & 1 != 0:
b = self.f[b][j]
sub_dep >>= 1
j += 1
# a, b两个点本来就是祖孙关系,直接返回
if a == b:
return a
# 两个节点同时上移
j = min(int(math.log2(self.dep[a])) + 1, self.max_j)
while j >= 0:
aa, bb = self.f[a][j], self.f[b][j]
if aa != bb:
a, b = aa, bb
j -= 1
return self.f[a][0]
n, m = map(int, input().split())
edges = []
link = [[] for i in range(n)]
for i in range(n-1):
a, b, w = map(int, input().split())
a, b, = a-1, b-1
link[a].append((b, w))
link[b].append((a, w))
edges.append((a, b))
# bfs 求每一个点到根节点0的距离
dis = [0] * n
def bfs():
que = deque()
que.append((0, 0))
visit = [0] * n
visit[0] = 1
while len(que) > 0:
cur_node, cur_dis = que.popleft()
for child, edge_len in link[cur_node]:
if visit[child] == 0:
visit[child] = 1
dis[child] = cur_dis + edge_len
que.append((child, dis[child]))
bfs()
lca = LCA(edges, max_node_num=n)
for i in range(m):
a, b = map(int, input().split())
a, b = a-1, b-1
c = lca.get_lca(a, b)
print(dis[a] + dis[b] - dis[c]* 2)