#include <bits/stdc++.h>   using namespace std;   template <class node>   struct link_cut_tree {     bool connected(node* u, node* v) { return lca(u, v) != NULL; }     int depth(node* u) {       access(u);       return get_sz(u->ch[0]);     }     node* get_root(node* u) {       access(u);       while (u->ch[0]) u = u->ch[0], u->push();       return access(u), u;     }     node* ancestor(node* u, int k) {       k = depth(u) - k;       assert(k >= 0);       for (;; u->push()) {         int sz = get_sz(u->ch[0]);         if (sz == k) return access(u), u;         if (sz < k)           k -= sz + 1, u = u->ch[1];         else           u = u->ch[0];       }       assert(0);     }     node* lca(node* u, node* v) {       if (u == v) return u;       access(u);       access(v);       if (!u->p) return NULL;       u->splay();       return u->p ?: u;     }     void link(node* u, node* v) {       make_root(v);       access(u);       set_link(v, u, 0);       v->update();     }     void cut(node* u) {       access(u);       u->ch[0]->p = NULL;       u->ch[0] = NULL;       u->update();     }     void cut(node* u, node* v) { cut(depth(u) > depth(v) ? u : v); }     void make_root(node* u) {       access(u);       u->reverse();       access(u);       assert(!u->ch[0] && !u->ch[1]);     }     void access(node* u) {       for (node *v = u, *pre = NULL; v; v = v->p) {         node* tmp = v;         while (!tmp->is_root()) tmp = tmp->p;         swap(v->parent, tmp->parent);         v->splay();         if (pre) v->update_vsub(pre, false);         if (v->ch[1]) v->update_vsub(v->ch[1], true);         v->ch[1] = pre;         v->update();         pre = v;       }       u->splay();       assert(!u->ch[1]);     }     node* operator[](int i) { return &data[i]; }     int operator[](node* i) { return i - &data[0]; }     vector<node> data;     link_cut_tree(int n) : data(n) {}   };   template <typename pnode>   struct splay_tree {     pnode ch[2], p;     splay_tree() { ch[0] = ch[1] = p = NULL; }     virtual void update() {}     virtual void push() {}     int dir() {       if (!p) return -2;       if (p->ch[0] == this) return 0;       if (p->ch[1] == this) return 1;       return -1;     }     bool is_root() { return dir() < 0; }     friend void set_link(pnode u, pnode v, int d) {       if (v) v->p = u;       if (d >= 0) u->ch[d] = v;     }     void rotate() {       assert(!is_root());       int x = dir();       pnode g = p;       set_link(g->p, static_cast<pnode>(this), g->dir());       set_link(g, ch[x ^ 1], x);       set_link(static_cast<pnode>(this), g, x ^ 1);       g->update();     }     void splay() {       while (!is_root() && !p->is_root()) {         p->p->push(), p->push(), push();         dir() == p->dir() ? p->rotate() : rotate();         rotate();       }       if (!is_root()) p->push(), push(), rotate();       push();       update();     }   };   template <typename pnode>   struct splay_tree_lct : splay_tree<pnode> {     using splay_tree<pnode>::ch;     bool rev;     splay_tree_lct() : splay_tree<pnode>() { rev = 0; }     virtual void update() override {}     virtual void push() override {       if (rev) {         if (ch[0]) ch[0]->reverse();         if (ch[1]) ch[1]->reverse();         rev = 0;       }     }     virtual void reverse() {       rev ^= 1;       swap(ch[0], ch[1]);     }   };   const int inf = -1e8;   template <typename pnode>   struct splay_tree_vchs : splay_tree<splay_tree_vchs<pnode>*> {     using splay_tree<splay_tree_vchs<pnode>*>::ch;     pnode key;     int x;     array<int, 4> y;     splay_tree_vchs(const pnode& key)         : splay_tree<splay_tree_vchs<pnode>*>(), key(key) {       build();     }     void build() {       x = key->x;       y[0] = key->pp[0];       y[1] = 0;       y[2] = key->pp[1];       y[3] = inf;     }     void update() override {       build();       for (int c = 0; c < 2; ++c)         if (ch[c]) {           x = max(x, ch[c]->x);           for (int i = 0; i < 4; i += 2) {             if (ch[c]->y[i] > y[i]) {               y[i + 1] = y[i];               y[i] = ch[c]->y[i];               if (ch[c]->y[i + 1] > y[i + 1]) y[i + 1] = ch[c]->y[i + 1];             } else if (ch[c]->y[i] > y[i + 1])               y[i + 1] = ch[c]->y[i];           }         }     }     void push() override {}     static void push_back(splay_tree_vchs*& u, const pnode& key) {       if (!u) {         u = new splay_tree_vchs(key);         return;       }       while (u->ch[1]) u = u->ch[1];       u->ch[1] = new splay_tree_vchs(key);       u->ch[1]->p = u;       u = u->ch[1];       u->splay();     }     static void erase(splay_tree_vchs*& u, splay_tree_vchs* v) {       v->splay();       if (!v->ch[0]) {         u = v->ch[1];       } else if (!v->ch[1]) {         u = v->ch[0];       } else {         u = v->ch[0];         u->p = NULL;         while (u->ch[1]) u = u->ch[1];         u->ch[1] = v->ch[1];         v->ch[1]->p = u;         u->splay();       }       if (u) u->p = NULL;       delete v;     }   };   const array<int, 4> ainf = {0, inf, 0, inf};   struct node : splay_tree_lct<node*> {     bool val, len, sval;     int slen;     int x;     array<int, 4> pp;     splay_tree_vchs<node*>*root, *parent;     node() : splay_tree_lct() {       root = parent = NULL;       val = len = sval = 0;       slen = x = 0;       pp = ainf;     }     void update() override {       splay_tree::update();       x = 0;       sval = val;       slen = len;       array<int, 4> up = ainf, dw = ainf;       array<int, 2> v1 = {0, inf}, v2 = v1;       bool upsval = val;       int upslen = len;       bool dwsval = val;       int dwslen = len;       if (ch[0]) {         x = max(x, ch[0]->x);         up = ch[0]->pp;         sval ^= ch[0]->sval;         slen += ch[0]->slen;         upsval ^= ch[0]->sval;         upslen += ch[0]->slen;       }       if (ch[1]) {         x = max(x, ch[1]->x);         dw = ch[1]->pp;         sval ^= ch[1]->sval;         slen += ch[1]->slen;         dwsval ^= ch[1]->sval;         dwslen += ch[1]->slen;       }       if (root) {         x = max(x, root->x);         v1[0] = root->y[0];         v1[1] = root->y[2];         v2[0] = root->y[1];         v2[1] = root->y[3];       }       pp[0] = up[0], pp[1] = up[1];       pp[2] = dw[2], pp[3] = dw[3];       for (int i = 0; i < 2; ++i) {         x = max(x, len + max({up[2 + i] + dw[i ^ val], up[2 + i] + v1[i ^ val],                               dw[i] + v1[i ^ val], v1[i] + v2[i ^ val]}));         pp[i ^ upsval] = max(pp[i ^ upsval], upslen + max(dw[i], v1[i]));         pp[2 + (i ^ dwsval)] =             max(pp[2 + (i ^ dwsval)], dwslen + max(up[2 + i], v1[i]));       }     }     void update_vsub(node* v, bool add) {       if (add) {         splay_tree_vchs<node*>::push_back(root, v);         v->parent = root;       } else {         splay_tree_vchs<node*>::erase(root, v->parent);         v->parent = NULL;       }     }     void push() override { splay_tree_lct::push(); }     void reverse() override {       splay_tree_lct::reverse();       swap(pp[0], pp[2]);       swap(pp[1], pp[3]);     }   };   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     int n;     cin >> n;     link_cut_tree<node> lct(2 * n);     for (int i = 1, u, v, w; i < n; ++i) {       cin >> u >> v >> w;       --u, --v;       lct[n + i]->val = w;       lct[n + i]->len = true;       lct[n + i]->update();       lct.link(lct[u], lct[n + i]);       lct.link(lct[n + i], lct[v]);     }     int q;     cin >> q;     for (int i = 0, u; i < q; ++i) {       cin >> u;       lct.access(lct[n + u]);       lct[n + u]->val ^= 1;       lct[n + u]->update();       cout << lct[n + u]->x <<   n ;     }     return 0;   }