https://vjudge.net/contest/279738#problem/I
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
struct str{
int t,w,n;
}e[200000];
int t,tt,t1,i,x,y,z,j,k,n,m,h[100000],hh[100000],dis[100000],f[100000][20];
void add(int u,int v,int w)
{
t1++;
e[t1].t=v;
e[t1].w=w;
e[t1].n=hh[u];
hh[u]=t1;
}
void bfs()
{
queue<int>q;
q.push(1);
memset(h,0,sizeof(h));
h[1]=1;
while (q.size())
{
int t1=q.front();
q.pop();
for (int i=hh[t1];i;i=e[i].n)
{
int t2=e[i].t;
if (h[t2]) continue;
h[t2]=h[t1]+1;
dis[t2]=dis[t1]+e[i].w;
f[t2][0]=t1;
for (int j=1;j<=t;j++)
f[t2][j]=f[f[t2][j-1]][j-1];
q.push(t2);
}
}
}
int lca(int x,int y)
{
if (h[x]>h[y]) swap(x,y);
for (int i=t; i>=0; i--)
if (h[f[y][i]]>=h[x]) y=f[y][i];
if (x==y) return x;
for (int i=t; i>=0; i--)
if (f[y][i]!=f[x][i])
{
y=f[y][i];
x=f[x][i];
}
return f[x][0];
}
int main()
{
scanf("%d",&tt);
while (tt--)
{
memset(f,0,sizeof(f));
memset(e,0,sizeof(e));
memset(hh,0,sizeof(hh));
memset(dis,0,sizeof(dis));
scanf("%d%d",&n,&m);
t1=0;
t=log(n)/log(2)+1;
for (i=1; i<n; i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
bfs();
for (i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
printf("%d\n",dis[x]+dis[y]-2*dis[lca(x,y)]);
}
}
}