#include using namespace std; template struct graph { struct edge { int from; int to; T cost; }; vector edges; vector> g; int n; graph(int m) : n(m) { g.resize(n); } virtual int add(int from, int to, T cost) = 0; }; template struct tree : graph { using graph::edges; using graph::g; using graph::n; tree(int m) : graph(m) {} int add(int from, int to, T cost = 1) { assert(0 <= from && from < n && 0 <= to && to < n); int id = (int)edges.size(); assert(id < n - 1); g[from].push_back(id); g[to].push_back(id); edges.push_back({from, to, cost}); return id; } }; template struct dfs_tree : tree { using tree::edges; using tree::g; using tree::n; vector pv; vector pe; vector order; vector pos; vector end; vector sz; vector root; vector depth; vector dist; dfs_tree(int m) : tree(m) {} void init() { pv = vector(n, -1); pe = vector(n, -1); order.clear(); pos = vector(n, -1); end = vector(n, -1); sz = vector(n, 0); root = vector(n, -1); depth = vector(n, -1); dist = vector(n); } void clear() { pv.clear(); pe.clear(); order.clear(); pos.clear(); end.clear(); sz.clear(); root.clear(); depth.clear(); dist.clear(); } void dfs(int v, bool clear_order = true) { if(pv.empty()) init(); else if(clear_order) order.clear(); dfs_from(v); } private: void _dfs(int v) { pos[v] = (int)order.size(); order.push_back(v); sz[v] = 1; for(int id : g[v]) { if(id == pe[v]) continue; auto &e = edges[id]; int to = e.from ^ e.to ^ v; depth[to] = depth[v] + 1; dist[to] = dist[v] + e.cost; pv[to] = v; pe[to] = id; root[to] = (root[v] != -1 ? root[v] : to); _dfs(to); sz[v] += sz[to]; } end[v] = (int)order.size() - 1; } void dfs_from(int v) { depth[v] = 0; dist[v] = T{}; root[v] = v; pv[v] = pe[v] = -1; _dfs(v); } }; template struct lca_tree : dfs_tree { using dfs_tree::edges; using dfs_tree::g; using dfs_tree::n; using dfs_tree::pv; using dfs_tree::pos; using dfs_tree::end; using dfs_tree::depth; int h; vector> pr; lca_tree(int m) : dfs_tree(m) {} inline void build_lca() { assert(!pv.empty()); int max_depth = 0; for(int i = 0; i < n; i++) max_depth = max(max_depth, depth[i]); h = 1; while((1 << h) <= max_depth) h++; pr.resize(n); for(int i = 0; i < n; i++) { pr[i].resize(h); pr[i][0] = pv[i]; } for(int j = 1; j < h; j++) { for(int i = 0; i < n; i++) { pr[i][j] = (pr[i][j - 1] == -1 ? -1 : pr[pr[i][j - 1]][j - 1]); } } } inline bool anc(int x, int y) { return (pos[x] <= pos[y] && end[y] <= end[x]); } inline int go_up(int x, int up) { assert(!pr.empty()); up = min(up, (1 << h) - 1); for(int j = h - 1; j >= 0; j--) { if(up & (1 << j)) { x = pr[x][j]; if(x == -1) break; } } return x; } inline int lca(int x, int y) { assert(!pr.empty()); if(anc(x, y)) return x; if(anc(y, x)) return y; for(int j = h - 1; j >= 0; j--) { if(pr[x][j] != -1 && !anc(pr[x][j], y)) { x = pr[x][j]; } } return pr[x][0]; } }; int main() { int n; cin >> n; lca_tree g(n); for(int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; g.add(u, v, w); } g.dfs(0); g.build_lca(); int q; cin >> q; auto dist = [&](int x, int y) -> long long { return g.dist[x] + g.dist[y] - 2 * g.dist[g.lca(x, y)]; }; while(q--) { int x, y, z; cin >> x >> y >> z; long long sm = dist(x, y) + dist(y, z) + dist(z, x); cout << sm/2 << endl; } }