#include using namespace std; typedef long long signed int LL; typedef long long unsigned int LU; #define incID(i, l, r) for(LL i = (l) ; i < (r); ++i) #define incII(i, l, r) for(LL i = (l) ; i <= (r); ++i) #define decID(i, l, r) for(LL i = (r) - 1; i >= (l); --i) #define decII(i, l, r) for(LL i = (r) ; i >= (l); --i) #define inc(i, n) incID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec(i, n) decID(i, 0, n) #define dec1(i, n) decII(i, 1, n) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() template bool setmin (T & a, T b) { if(b < a) { a = b; return true; } else { return false; } } template bool setmax (T & a, T b) { if(b > a) { a = b; return true; } else { return false; } } template bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } } template bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } } LL mo(LL a, LL b) { assert(b > 0); a %= b; if(a < 0) { a += b; } return a; } LL fl(LL a, LL b) { assert(b > 0); return (a > 0 ? a / b : (a - b + 1) / b); } LL ce(LL a, LL b) { assert(b > 0); return (a < 0 ? a / b : (a + b - 1) / b); } template T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); } template T lcm(T a, T b) { return a / gcd(a, b) * b; } #define bit(b, i) (((b) >> (i)) & 1) #define BC __builtin_popcountll #define SC static_cast #define SI(v) SC(v.size()) #define SL(v) SC(v.size()) #define RF(e, v) for(auto & e: v) #define ef else if #define UR assert(false) // ---- ---- const int N = 100000; const int B = 17; int n, q; vector> g[N]; LL s[N]; int h[N], p[B][N]; void dfs(int v, int pp, int hh, LL ss) { s[v] = ss; h[v] = hh; p[0][v] = pp; for(auto && e: g[v]) { if(e.FI == pp) { continue; } dfs(e.FI, v, h[v] + 1, s[v] + e.SE); } } void pre() { dfs(0, -1, 0, 0); inc(k, B - 1) { inc(i, n) { p[k + 1][i] = p[k][p[k][i]]; } } } int lca(int a, int b) { if(! (h[a] <= h[b])) { swap(a, b); } int d = h[b] - h[a]; inc(k, B) { if(bit(d, k)) { b = p[k][b]; } } if(a == b) { return a; } dec(k, B) { int pa = p[k][a]; int pb = p[k][b]; if(pa != pb) { a = pa; b = pb; } } assert(p[0][a] == p[0][b]); return p[0][a]; } int main() { cin >> n; inc(i, n - 1) { LL a, b, c; cin >> a >> b >> c; g[a].EB(b, c); g[b].EB(a, c); } pre(); cin >> q; inc(qq, q) { int x, y, z; cin >> x >> y >> z; int X = lca(y, z); int Y = lca(z, x); int Z = lca(x, y); cout << s[x] + s[y] + s[z] - s[X] - s[Y] - s[Z] << "\n"; } return 0; }