#include "bits/stdc++.h"
#include "testlib.h"

using namespace std;

void dfs(int i, vector<vector<int>>& g, vector<bool>& was) {
    if (was[i]) return;
    was[i] = 1;
    
    for (auto j : g[i]) dfs(j, g, was);
}

int main(int argc, char *argv[]) {
    registerValidation(argc, argv);
    int n = inf.readInt(2, 2e5, "n");
    inf.readEoln();
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; ++i) {
        int a, b, c;
        a = inf.readInt(1, n, "v_i");
        inf.readSpace();
        b = inf.readInt(1, n, "u_i");
        inf.readSpace();
        inf.readInt(0, (1 << 20) - 1, "c_i");
        inf.readEoln();

        g[a - 1].push_back(b - 1);
        g[b - 1].push_back(a - 1);
    }

    vector<bool> was(n);
    dfs(0, g, was);

    ensuref(find(was.begin(), was.end(), false) == was.end(), "Given graph is not a tree");
    
    int q = inf.readInt(1, 2e5, "q");
    inf.readEoln();
    while (q--) {
        inf.readInt(0, (1 << 20) - 1, "t_i");
        inf.readEoln();
    }
    inf.readEof();
}
