#include #include #include #include #include #include #include #include #include #include #include #include #define vll vector #define vvv vector #define vvi vector > #define vvl vector > #define vv(a, b, c, d) vector >(a, vector(b, c)) #define vvvl(a, b, c, d) vector(a, vvl(b, vll (c, d))); #define rep(c, a, b) for(ll c=a;c depth(MAX_V); void dfs(int v, int p, int d){ parent[0][v] = p, depth[v] = d; for(int i=0;idepth[v]) swap(u, v); for(int k=0;k<30;k++)if((depth[v]-depth[u])>>k & 1) v = parent[k][v]; if(u==v) return u; for(int k=30-1;k>=0;k--){ if(parent[k][u] != parent[k][v]){ u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } struct edge{ll to, cost;}; vector> T(MAX_V, vector(0)); vll d(MAX_V, 0); void calc(ll now, ll from, ll dist){ d[now] = dist; for(int i=0;i> n; re(i, n-1){ std::cin >> a >> b >> c; G[a].push_back(b);G[b].push_back(a); T[a].push_back(edge{b, c});T[b].push_back(edge{a, c}); } root = 0; init();calc(0, -1, 0); ll q;std::cin >> q; re(i, q){ std::cin >> a >> b >> c; ll A = lca(a, b), B = lca(b, c), C = lca(a, c), D = lca(A, c); std::cout << d[a]+d[b]+d[c]-d[A]-d[B]-d[C]+d[D] << '\n'; } return 0; }