思路:搞了一发链剖
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 88888
int n,m,first[N],next[N],v[N],w[N],tot,xx,yy,zz,k;
int top[N],size[N],deep[N],son[N],fa[N],weight[N];
void add(int x,int y,int z){
w[tot]=z,v[tot]=y;
next[tot]=first[x],first[x]=tot++;
}
void dfs(int x){
size[x]=1;
for(int i=first[x];~i;i=next[i])
if(v[i]!=fa[x]){
fa[v[i]]=x;
deep[v[i]]=deep[x]+1;
weight[v[i]]=weight[x]+w[i];
dfs(v[i]);
size[x]+=size[v[i]];
if(size[son[x]]<size[v[i]])son[x]=v[i];
}
}
void dfs2(int x,int tp){
top[x]=tp;
if(son[x])dfs2(son[x],tp);
for(int i=first[x];~i;i=next[i]){
if(v[i]!=fa[x]&&v[i]!=son[x]){
dfs2(v[i],v[i]);
}
}
}
int lca(int x,int y){
int fx=top[x],fy=top[y];
while(fx!=fy){
if(deep[fx]<deep[fy])
swap(fx,fy),swap(x,y);
x=fa[fx],fx=top[x];
}
if(deep[x]<deep[y])return x;
return y;
}
int main(){
memset(first,-1,sizeof(first));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&xx,&yy,&zz),getchar(),getchar(),getchar();
add(xx,yy,zz),add(yy,xx,zz);
}
dfs(1),dfs2(1,1);
scanf("%d",&k);
for(int i=1;i<=k;i++){
scanf("%d%d",&xx,&yy);
printf("%d
",weight[xx]+weight[yy]-2*weight[lca(xx,yy)]);
}
}