#include "bits/stdc++.h"

using namespace std;

void dfs(int i, int p, vector<vector<pair<int, int>>>& g, vector<long long>& calc, int x) {
    if (p != -1) calc[x]++;

    for (auto [j, c] : g[i]) {
        if (j == p) continue;

        dfs(j, i, g, calc, x ^ c);
    }
}

int main(int argc, char *argv[]) {
    int n; cin >> n;
    vector<vector<pair<int, int>>> g(n);
    for (int i = 0; i < n  - 1; ++i) {
        int a, b, c; cin >> a >> b >> c;
        --a; --b;

        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }

    vector<long long> calc(1 << 20);
    for (int i = 0; i < n; ++i) {
        dfs(i, -1, g, calc, 0);
    }

    int q; cin >> q;
    while (q--) {
        int a; cin >> a;

        if (a == 0) {
            cout << calc[a] / 2 << "\n";
        } else cout << calc[a] / 2 << "\n";
    }
}
