#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; using gra = vector>; typedef unsigned long long ull; template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template T lcm(T a, T b) { return a / gcd(a, b) * b; } template T dup(T a, T b) { return (a + b - 1) / b; } template T modpow(T x, T p, T m) { T a=1; while(p){ if(p%2)a =a*x%m; x = x+x%m; p/=2; } return a; } // template void prim(T a) { for(ll i=0;i= 0; --i) #define rrep1(i, n) for (int i = n; i > 0; --i) #define pri(x) cout << (x) << "\n" #define pri2(x, y) cout << (x) << " " << (y) << "\n" #define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n" //#define modpow(x, p, m) { ll a=1; while(p){ if(p%2)a =a*x%m; x = x+x%m; p/=2; } return a; } #define m0(h) memset(h, 0, sizeof(h)) #define m1(h) memset(h, 1, sizeof(h)) #define m(h, n) memset(h, (n), sizeof(h)) #define all(n) (n).begin(),(n).end() #define bit(n) (1LL << (n)) #define pb push_back //#ifdef LOCAL // //#define dpri(...) fprintf(stderr, __VA_ARGS__) //#define pria(g) rep(i,g.size()){ cout << i << "{"; rep(j,g[i].size()) cout << g[i][j] << " "; cout << "}" << "\n"; } //#else //#define dpri(...) 42 //#define pria(g) 42 //#endif int main() { ios::sync_with_stdio(false); cin.tie(0); // --------------------- // コーディング開始 // --------------------- ll n; cin >> n; // 番号 0親 1重さ ll tree[n][2]; ll u,v,w; rep(i,n-1){ cin >> u >> v >> w; tree[max(u,v)][0]=min(u,v); tree[max(u,v)][1]=w; } tree[0][0]=0; tree[0][1]=0; ll q; cin >> q; rep(i,q){ int len[n]; m0(len); len[0]=1; ll next; ll ans =0; rep(f,3){ cin >> next; // 既にみた while(len[next]==0){ ans+=tree[next][1]; len[next]=1; next=tree[next][0]; } if(len[next]==0){ ans+=tree[next][1]; } } pri(ans); } return 0; }