#include using namespace std; using ll = long long; template using vec = vector; template using vvec = vector>; struct edge{ int to,id; ll dist; edge(int to,ll dist=1,int id=1):to(to),id(id),dist(dist){}; }; class LCA{ private: vvec v; vvec parent; vec depth,ord; vec dist; int k; void dfs(int cur,int par,int d){ parent[0][cur] = par; depth[cur] = d; ord[cur] = k++; for(auto& e:v[cur]) if(e.to!=par){ dist[e.to] = dist[cur]+e.dist; dfs(e.to,cur,d+1); } } public: LCA(int N,int root,vvec& tree){ v = tree; parent = vvec(20,vec(N,0)); depth = ord = vec(N,0); dist = vec(N,0); k = 0; dfs(root,-1,0); for(int j=0;j+1<20;j++){ for(int i=0;idepth[m]) swap(n,m); for(int j=0;j<20;j++){ if((depth[m]-depth[n]) >> j&1) m = parent[j][m]; } if(n==m) return n; for(int j=19;j>=0;j--){ if(parent[j][n]!=parent[j][m]){ n = parent[j][n]; m = parent[j][m]; } } return parent[0][n]; } int dep(int n){return depth[n];} ll getdist(int n){return dist[n];} int getord(int n){return ord[n];} }; int main(){ int N; cin >> N; vvec g(N); for(int i=0;i> a >> b >> c; g[a].push_back({b,c}); g[b].push_back({a,c}); } LCA lca(N,0,g); int Q; cin >> Q; auto calc = [&](int a,int b){ return lca.getdist(a)+lca.getdist(b)-2*lca.getdist(lca.lca(a,b)); }; for(int q=0;q> k; vec X(k); for(int i=0;i> X[i]; sort(X.begin(),X.end(),[&](int a,int b){ return lca.getord(a)