#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i=0;i--) #define Rep(i,sta,n) for(int i=sta;i=sta;i--) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair LP; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; int n; vector

G[100010]; int depth[100010],par[100010][17]; ll len[100010],table[100010][17]; void dfs(int s,int p,ll w){ //cout << s << endl; if(p!=-1){ len[s]=len[p]+w; depth[s]=depth[p]+1; } par[s][0]=p; rep(k,16){ if(par[s][k]!=-1) par[s][k+1]=par[par[s][k]][k]; else par[s][k+1]=-1; //cout << s << " " << par[s][k+1] << endl; } for(P pa:G[s]){ int t=pa.first;ll w_=pa.second; //cout << t << " " << s << " " << w_ << endl; if(t==p || t==-1) continue; dfs(t,s,w_); } } int lca(int x,int y){ //cout << x << " " << y << endl; //cout << depth[x] << " " << depth[y] << endl; if(depth[x]depth[t]) swap(s,t); if(depth[s]>depth[x]) return false; if(lca(t,x)==x) return true; return false; } void make_table(){ rep(i,n) table[i][0]=INF; rep(j,16){ rep(i,n){ ll res=INF; if(par[i][j+1]==-1){ table[i][j+1]=INF; continue; } for(P p:G[par[i][j]]){ int t=p.first;ll l=p.second; if(in_pass(par[i][j+1],i,t)) continue; //int q=lca(i,t); //cout << par[i][j+1] << " " << i << " " << t << " " << q << " " << in_pass(par[i][j+1],i,t) << endl; res=l; break; } table[i][j+1]=min(res,min(table[i][j],table[par[i][j]][j])); } } } void solve(){ cin >> n; rep(i,n-1){ int a,b,c;cin >> a >> b >> c;a--;b--; G[a].push_back(P(b,c)); G[b].push_back(P(a,c)); } rep(i,n) G[i].push_back(P(-1,INF)); rep(i,n) sort(G[i].begin(),G[i].end(),[](P a,P b){return a.second> q; rep(_,q){ int u,v;cin >> u >> v;u--;v--; int l=lca(u,v); ll D=len[u]+len[v]-2ll*len[l]; //cout << u << " " << v << endl; if(u!=l && v!=l){ ll res=INF; int u_=u,v_=v; per(i,17){ if((depth[u_]-depth[l])&(1 << i)){ res=min(res,table[u_][i]); u_=par[u_][i]; for(P p:G[u_]){ int t=p.first;ll d=p.second; if(in_pass(u,l,t)) continue; res=min(res,d); break; } } } per(i,17){ if((depth[v_]-depth[l])&(1 << i)){ res=min(res,table[v_][i]); v_=par[v_][i]; for(P p:G[v_]){ int t=p.first;ll d=p.second; if(in_pass(v,l,t)) continue; res=min(res,d); break; } } } for(P p:G[u]){ int t=p.first;ll d=p.second; if(in_pass(u,l,t)) continue; res=min(res,d); break; } for(P p:G[v]){ int t=p.first;ll d=p.second; if(in_pass(v,l,t)) continue; res=min(res,d); break; } for(P p:G[l]){ int t=p.first;ll d=p.second; if(in_pass(u,l,t) || in_pass(v,l,t)) continue; res=min(res,d); break; } if(res==INF) cout << -1 << endl; else cout << 2ll*res+D << endl; continue; } if(v==l) swap(u,v); int v_=v; ll res=INF; per(i,17){ if((depth[v_]-depth[l])&(1 << i)) { res=min(res,table[v_][i]); v_=par[v_][i]; for(P p:G[v_]){ int t=p.first;ll d=p.second; if(in_pass(v,l,t)) continue; res=min(res,d); break; } } } //cout << res << endl; for(P p:G[u]){ int t=p.first;ll d=p.second; //cout << v << " " << l << " " << t << " " << d << endl; if(in_pass(u,v,t)) continue; res=min(res,d); break; } for(P p:G[v]){ int t=p.first;ll d=p.second; //cout << v << " " << l << " " << t << " " << d << endl; if(in_pass(u,v,t)) continue; res=min(res,d); break; } if(res==INF) cout << -1 << endl; else cout << 2ll*res+D << endl; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); solve(); }