#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>usingnamespace std;structnode{node(){first =-1;}long dis;long first;};structroad{long next, to, dis;};//并查集个人模板longDSet[50005];voidinit(long n){for( long i =0 ; i <= n ; i ++)DSet[i] = i;}longfindP( long id){if(DSet[id] != id)DSet[id] =findP(DSet[id]);returnDSet[id];}//联合元素,这里修改为把b的父节点设为a,但是要在下面使用的时候注意不能搞反顺序longUnionEle( long a,long b){ a =findP(a); b =findP(b);DSet[b] = a;return a;}voidlca(long pos,long dis);boolischeck[50005] = {false};longres[20005] = {0};longque[50005];node point[50005];road rd[50005*2];road question[50005*2];voidinsertR(long from,long to,long len,long&n){rd[n].to = to;rd[n].dis = len;rd[n].next =point[from].first;point[from].first = n; n ++;}voidinsertQ(long from,long to,long index,long&n){question[n].to = to;question[n].dis = index;question[n].next =que[from];que[from] = n; n ++;}intmain(){long n, m, i, j, k, from, to, len, rdl =0, quel =0;char c;scanf("%ld%ld",&n,&m); //Input roadsfor(i =0; i < m; i ++) {scanf("%ld%ld%ld%c",&from,&to,&len,&c);insertR(from, to, len, rdl);insertR(to, from, len, rdl); }memset(que,-1,sizeof(que));scanf("%ld",&k); //Input questionfor(i =0; i < k; i ++) {scanf("%ld%ld",&from,&to);insertQ(from, to, i, quel);insertQ(to, from, i, quel); }init(n);ischeck[1] =true;lca(1,0);for(i =0; i < k; i ++)printf("%ld\n",res[i]);return0;}voidlca(long pos,long dis){point[pos].dis = dis;long i, p;ischeck[pos] =true;for(i =point[pos].first; i !=-1; i =rd[i].next) {if(ischeck[rd[i].to] ==false) {lca(rd[i].to, dis +rd[i].dis);UnionEle(pos,rd[i].to); } }for(i =que[pos]; i !=-1; i =question[i].next) {if(ischeck[question[i].to]) { p =findP(question[i].to);res[question[i].dis] = dis +point[question[i].to].dis -2*point[p].dis; } }}