1 #include<iostream>
2 #include<vector>
3 using namespace std;
4 typedef unsigned long long ll;
5 const int maxn=50005,maxm=75005;
6
7
8 vector<int> G[maxn],dist[maxn],u[maxn],f[maxn];
9 bool vis[maxn],vt[maxn];
10 int fa[maxn],w[maxm],n;
11 ll anc[maxn];
12
13 int find(int x)
14 {
15 if(!fa[x])return x;
16 return fa[x]=find(fa[x]);
17 }
18
19 void uni(int x,int y)
20 {
21 int b=find(y);
22 if(b!=x)fa[b]=x;
23 }
24
25 void Tarjan(int x,ll k)
26 {
27 anc[x]=k;
28 for(int i=0;i<G[x].size();i++)
29 if(!vt[G[x][i]])
30 {
31 int v=G[x][i];
32 vt[v]=true;
33 Tarjan(v,k+dist[x][i]);
34 uni(x,v);
35 }
36 vis[x]=true;
37 for(int i=0;i<u[x].size();i++)
38 if(vis[u[x][i]])
39 {
40 w[f[x][i]]=anc[x]+anc[u[x][i]]-2*anc[find(u[x][i])];
41 }
42 }
43 int main()
44 {
45 int a,b,c,m;
46 cin>>n;
47 for(int i=1;i<=n-1;i++)
48 {
49 cin>>a>>b>>c;
50 a++,b++;
51 G[a].push_back(b),dist[a].push_back(c);
52 G[b].push_back(a),dist[b].push_back(c);
53 }
54 cin>>m;
55 for(int i=1;i<=m;i++)
56 {
57 cin>>a>>b;
58 a++,b++;
59 u[a].push_back(b),u[b].push_back(a);
60 f[a].push_back(i),f[b].push_back(i);
61 }
62 vt[1]=true;
63 Tarjan(1,0);
64 //for(int i=1;i<=n;i++)cout<<anc[i]<<endl;
65 for(int i=1;i<=m;i++)
66 cout<<w[i]<<endl;
67 }