# Code

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)
#define fill(x,t) memset(x,t,sizeof(x))
#define fi first
#define se second

typedef std:: pair <int,int> pair;
const int N=200005;
const int E=500005;

struct Graph {
struct edge {int x,y,w,next;} e[E];

int ls[N],edCnt;

Graph() {
fill(ls,0);
edCnt=1;
}

void add_edge(int x,int y,int w) {
e[++edCnt]=(edge) {x,y,w,ls[x]}; ls[x]=edCnt;
e[++edCnt]=(edge) {y,x,w,ls[y]}; ls[y]=edCnt;
}
} G,T;

std:: stack <int> stack;
std:: vector <int> scc[N];
std:: set <pair> zhx[N];

int dis[N],dfn[N],low[N],tot;
int size[N],bl[N],dep[N],top[N],sum[N];
int lxf[21][N],fa[21][N],bel[N],n;

bool vis[N],mark[N];

int x=0,v=1; char ch=getchar();
for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
return x*v;
}

void spfa(int st) {
std:: queue <int> que; que.push(st);
dis[st]=0;
for (;!que.empty();) {
int now=que.front(); que.pop();
for (int i=G.ls[now];i;i=G.e[i].next) {
if (mark[G.e[i].y]&&dis[now]+G.e[i].w<dis[G.e[i].y]) {
dis[G.e[i].y]=dis[now]+G.e[i].w;
if (!vis[G.e[i].y]) {
vis[G.e[i].y]=true;
que.push(G.e[i].y);
}
}
} vis[now]=false;
}
}

void dfs1(int now,int from) {
dfn[now]=low[now]=++dfn[0]; vis[now]=true;
for (int i=G.ls[now];i;i=G.e[i].next) {
if ((i^1)==from) continue;
if (!dfn[G.e[i].y]) {
stack.push(i);
dfs1(G.e[i].y,i);
low[now]=std:: min(low[now],low[G.e[i].y]);
if (dfn[now]<=low[G.e[i].y]) {
int y=0,fir=0; tot++;
while (y!=i) {
y=stack.top(); stack.pop();
if (!fir) fir=y;
scc[tot].push_back(G.e[y].y);
bel[G.e[y].y]=tot;
sum[tot]+=G.e[y].w; zhx[tot].insert(pair(G.e[y].x,sum[tot]));
}
bel[now]=tot;
for (int j=G.ls[G.e[fir].y];j;j=G.e[j].next) {
if (G.e[j].y==now) {
sum[tot]+=G.e[j].w;
zhx[tot].insert(pair(G.e[j].x,sum[tot]));
}
}
top[tot]=now;
}
} else if (vis[G.e[i].y]) low[now]=std:: min(low[now],dfn[G.e[i].y]);
}
vis[now]=false;
}

void dfs2(int now) {
rep(i,1,20) fa[i][now]=fa[i-1][fa[i-1][now]];
rep(i,1,20) lxf[i][now]=lxf[i-1][now]+lxf[i-1][fa[i-1][now]];
for (int i=T.ls[now];i;i=T.e[i].next) {
if (T.e[i].y==fa[0][now]) continue;
fa[0][T.e[i].y]=now; dep[T.e[i].y]=dep[now]+1;
if (now>=n) lxf[0][T.e[i].y]=dis[T.e[i].y];
// else lxf[0][T.e[i].y]=T.e[i].w;
dfs2(T.e[i].y);
}
}

int solve(int x,int y) {
int ret=0;
if (dep[x]<dep[y]) std:: swap(x,y);
drp(i,20,0) if (dep[fa[i][x]]>=dep[y]) {
ret+=lxf[i][x];
x=fa[i][x];
}
if (x==y) return ret;
drp(i,20,0) if (fa[i][x]!=fa[i][y]) {
ret+=lxf[i][x]+lxf[i][y];
x=fa[i][x],y=fa[i][y];
}
int wjp=lxf[0][x]+lxf[0][y];
if (fa[0][x]<=n) return ret+wjp;
pair chp=*zhx[fa[0][x]].lower_bound(pair(x,0));
pair lyh=*zhx[fa[0][x]].lower_bound(pair(y,0));
wjp=abs(chp.se-lyh.se);
wjp=std:: min(wjp,sum[fa[0][x]]-wjp);
return ret+wjp;
}

int main(void) {
freopen("data.in","r",stdin);
freopen("myp.out","w",stdout);
rep(i,1,m) {
} tot=n; dfs1(1,0);
rep(i,n+1,tot) {
for (std:: vector <int>:: iterator j=scc[i].begin();j!=scc[i].end();j++) {
mark[*j]=true;
dis[*j]=0x3f3f3f3f;
}
mark[top[i]]=true;
// printf("%d %d\n", top[i],i);
for (std:: vector <int>:: iterator j=scc[i].begin();j!=scc[i].end();j++) {
// printf("%d %d\n", i,*j);
mark[*j]=false;
}
mark[top[i]]=false;
}
dfs2(dep[1]=1);
for (;Q--;) {