# bzoj2125: 最短路

//minamoto
#include<bits/stdc++.h>
#define ll long long
#define fp(i,a,b) for(register int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(register int i=a,I=b-1;i>I;--i)
inline int max(const int &x,const int &y){return x>y?x:y;}
inline int min(const int &x,const int &y){return x<y?x:y;}
inline int abs(int &x){return x<0?-x:x;}
char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
int res,f=1;char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=100005;
struct eg{int v,nx,w;};
struct TR{
inline void add(int u,int v,int w){
}
}T,G;int n;
struct sl{
int fa[N],sz[N],son[N],top[N],dep[N],dis[N];
int dfn[N],rk[N],cir[N],tim;bool vis[N];
void dfs1(int u){
sz[u]=1,dep[u]=dep[fa[u]]+1;
go(T,u)if(v!=fa[u]){
fa[v]=u,dis[v]=dis[u]+T.e[i].w;
dfs1(v),sz[u]+=sz[v];
if(sz[v]>sz[son[u]])son[u]=v;
}
}
void dfs2(int u,int t){
top[u]=t,dfn[u]=++tim,rk[tim]=u;if(!son[u])return;
dfs2(son[u],t);
go(T,u)if(v!=fa[u]&&v!=son[u])dfs2(v,v);
}
inline int LCA(int u,int v){
while(top[u]!=top[v])dep[top[u]]<dep[top[v]]?v=fa[top[v]]:u=fa[top[u]];
return dep[u]<dep[v]?u:v;
}
int jump(int u,int lca){
int res;while(top[u]!=top[lca])res=top[u],u=fa[res];
return u==lca?res:rk[dfn[lca]+1];
}
int query(int u,int v){
int lca=LCA(u,v);if(lca<=n)return dis[u]+dis[v]-2*dis[lca];
int uu=jump(u,lca),vv=jump(v,lca);
int d1=dis[uu]-dis[lca],d2=dis[vv]-dis[lca];
if(!vis[uu])d1=cir[lca]-d1;if(!vis[vv])d2=cir[lca]-d2;
return dis[u]-dis[uu]+dis[v]-dis[vv]+min(abs(d1-d2),cir[lca]-abs(d1-d2));
}
}QAQ;
int dfn[N],low[N],dep[N],fa[N],tim;ll dis[N];
int st[N],tot,m,q,u,v,w;
void build(int u,int y,int d){
int top=dep[y]-dep[u]+1,sum=d,Dis=0;
for(register int i=y;i!=u;i=fa[i])st[top--]=i,sum+=dis[i]-dis[fa[i]];
QAQ.cir[++tot]=sum,st[1]=u,top=dep[y]-dep[u]+1;
fp(i,1,top){
QAQ.vis[st[i]]=(D==Dis),Dis+=dis[st[i+1]]-dis[st[i]];
}
}
void tarjan(int u,int fat){
dfn[u]=low[u]=++tim,dep[u]=dep[fat]+1,fa[u]=fat;
go(G,u)if(v!=fat){
if(!dfn[v])dis[v]=dis[u]+G.e[i].w,tarjan(v,u),low[u]=min(low[u],low[v]);
else low[u]=min(low[u],dfn[v]);
}