#include <bits/stdc++.h>

using namespace std;

struct Edge{
  int u, v, w;
 };

void dfs(int u, int p, int len, vector<vector<Edge>> &G, vector<int> &depth){
  depth[u] = len;
  for (auto e : G[u]){
    if (e.v == p)
      continue;
    dfs(e.v, u, len ^ e.w, G, depth);
  }
}

vector<int> p;
vector<unordered_map<int, int>*> List;
long long ans = 0;

int get(int u){
  if (u == p[u])
    return u;
  return (p[u] = get(p[u]));
}

void merge(int u, int v){
  u = get(u);
  v = get(v);
  if (u == v)
    return;
  if (List[u]->size() > List[v]->size())
    swap(u, v);
  for (auto x : (*List[u])){
    if (List[v]->find(x.first) == List[v]->end())
      (*List[v])[x.first] = 0;
    ans += x.second * 1ll * (*List[v])[x.first];
    (*List[v])[x.first] += x.second;
  }
  p[u] = v;
}

int main(){
  ios_base::sync_with_stdio(false), cin.tie(0);
  int n;
  cin >> n;
  vector<vector<Edge>> G(n);
  vector<Edge> edges;
  for (int i = 1; i < n; ++i){
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    G[u].push_back({u, v, w});
    G[v].push_back({v, u, w});
    edges.push_back({u, v, w});
  }
  vector<int> depth(n);
  dfs(0, -1, 0, G, depth);
  p.resize(n);
  for (int i = 0; i < n; ++i)
    p[i] = i;
  List.resize(n);
  vector<unordered_map<int, int>> Lists(n);
  for (int i = 0; i < n; ++i){
    List[i] = &Lists[i];
    Lists[i][depth[i]] = 1;
  }
  vector<int> order(n - 1);
  for (int i = 0; i + 1 < n; ++i)
    cin >> order[i];
  reverse(order.begin(), order.end());
  vector<long long> res(n);
  for (int i = 0; i + 1 < n; ++i){
    merge(edges[order[i] - 1].u, edges[order[i] - 1].v);
    res[i + 1] = ans;
  }
  reverse(res.begin(), res.end());
  for (long long x : res)
    cout << x << "\n";
  return 0;
}