How far away
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
struct Edge{
int v,next;
}edge[N];
int head[N],tot,dep[N],top[N],fa[N],son[N],sz[N],id[N],idx,w[N];
void add(int u,int v){
edge[++tot]={v,head[u]};
head[u]=tot;
}
int ed[N][3];
void dfs1(int u,int father,int depth){
sz[u]=1;fa[u]=father;dep[u]=depth;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v;
if(v!=father){
dfs1(v,u,depth+1);
sz[u]+=sz[v];
if(sz[son[u]]<sz[v]){
son[u]=v;
}
}
}
}
void dfs2(int u,int topf){
top[u]=topf;id[u]=++idx;
if(!son[u])return ;
dfs2(son[u],topf);
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v;
if(v!=fa[u]&&v!=son[u])dfs2(v,v);
}
}
struct Node{
int l,r;
LL sum;
}tr[N<<3];
void push_up(int u){
tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
}
void build(int u,int l,int r){
tr[u]={l,r,0};
if(l==r){
tr[u].sum=w[l];
return ;
}
int mid=l+r>>1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
push_up(u);
}
LL query(int u,int l,int r){
if(l<=tr[u].l&&tr[u].r<=r){
return tr[u].sum;
}
int mid=tr[u].l+tr[u].r>>1;
LL res=0;
if(l<=mid)res+=query(u<<1,l,r);
if(r>mid) res+=query(u<<1|1,l,r);
return res;
}
LL query(int u,int v){
LL res=0;
while(top[u]^top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
res+=query(1,id[top[u]],id[u]);
u=fa[top[u]];
}
if(dep[u]<dep[v])swap(u,v);
if(u!=v)res+=query(1,id[v]+1,id[u]);
return res;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
tot=0,idx=0;
memset(head,0,sizeof head);
memset(son,0,sizeof son);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v);
add(v,u);
ed[i][0]=u,ed[i][1]=v,ed[i][2]=w;
}
dfs1(1,1,1);
dfs2(1,1);
for(int i=1;i<n;i++){
if(dep[ed[i][0]]<dep[ed[i][1]])swap(ed[i][0],ed[i][1]);
w[id[ed[i][0]]]=ed[i][2];
}
build(1,1,n);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
printf("%lld\n",query(u,v));
}
}
return 0;
}