Problem: In the Land of Fire there are n villages and n-1 bidirectional road, and there is a path between any pair of villages by roads. There are only two types of roads: stone ones and sand ones. Since the Land of Fire is constantly renovating, every morning workers choose a single road and flip its type (so it becomes a stone road if it was a sand road and vice versa). Also everyone here loves ramen, that's why every morning a ramen pavilion is set in the middle of every stone road, and at the end of each day all the pavilions are removed.

For each of the following m days, after another road is flipped, Naruto and Jiraiya choose a simple path — that is, a route which starts in a village and ends in a (possibly, the same) village, and doesn't contain any road twice. Since Naruto and Jiraiya also love ramen very much, they buy a single cup of ramen on each stone road and one of them eats it. Since they don't want to offend each other, they only choose routes where they can eat equal number of ramen cups. Since they both like traveling, they choose any longest possible path. After every renovation find the maximal possible length of a path (that is, the number of roads in it) they can follow.

Input

The first line contains the only positive integer n (2 ≤ n ≤ 500 000) standing for the number of villages in the Land of Fire.

Each of the following (n-1) lines contains a description of another road, represented as three positive integers u, v and t (1 ≤ u, v ≤ n, t ∈ \{0,1\}). The first two numbers denote the villages connected by the road, and the third denotes the initial type of the road: 0 for the sand one and 1 for the stone one. Roads are numbered from 1 to (n-1) in the order from the input.

The following line contains a positive integer m (1 ≤ m ≤ 500 000) standing for the number of days Naruto and Jiraiya travel for.

Each of the following m lines contains the single integer id (1 ≤ id ≤ n-1) standing for the index of the road whose type is flipped on the morning of corresponding day.

It is guaranteed that there is a road path between any pair of villages.

Output

Output m lines. In the i-th of them print the only integer denoting the maximal possible length of any valid path on the i-th day.

Example

Input


5
1 2 0
1 3 0
3 5 0
3 4 0
5
3
4
1
3
4


Output


3
2
3
3
2

Note

After the renovation of the 3-rd road the longest path consists of the roads 1, 2 and 4.

After the renovation of the 4-th road one of the longest paths consists of the roads 1 and 2.

After the renovation of the 1-st road one of the longest paths consists of the roads 1, 2 and 3.

After the renovation of the 3-rd road the longest path consists of the roads 1, 2 and 4.

After the renovation of the 4-rd road one of the longest paths consists of the roads 2 and 4.

Solution (in CPP):

Solution:
#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) {
      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; }
  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() {
    const pnode& self = static_cast<pnode>(this);
    while (!is_root() && !p->is_root()) {
      p->p->push(), p->push(), self->push();
      dir() == p->dir() ? p->rotate() : self->rotate();
      self->rotate();
    }
    if (!is_root()) p->push(), self->push(), self->rotate();
    self->push();
    self->update();
  }
};
template <typename pnode, class splay_tree_vchs>
struct splay_tree_lct : splay_tree<pnode> {
  splay_tree_vchs vnode;
  using splay_tree<pnode>::ch;
  splay_tree_vchs* root;
  splay_tree_lct() : splay_tree<pnode>() { root = NULL; }
  void update() {}
  void push() {}
  void rotate() {
    swap(vnode, this->p->vnode);
    splay_tree<pnode>::rotate();
  }
  void splay() {
    auto v = &vnode;
    bool r = !v->key;
    splay_tree<pnode>::splay();
    if (r && v->key) {
      if (this->p->root->key == NULL) this->p->root = v;
      if (v->p) v->p->ch[v->p->ch[1] == &v->key->vnode] = v;
      if (v->ch[0]) v->ch[0]->p = v;
      if (v->ch[1]) v->ch[1]->p = v;
      v->key = static_cast<pnode>(this);
    }
  }
  void reverse() {}
  void update_vsub(pnode v, bool add) {
    auto& u = root;
    if (add) {
      v->vnode = splay_tree_vchs(v);
      if (!u) {
        u = &v->vnode;
        return;
      }
      while (u->ch[1]) u = u->ch[1];
      u->ch[1] = &v->vnode;
      u->ch[1]->p = u;
      u = u->ch[1];
      u->splay();
    } else {
      auto x = &v->vnode;
      x->splay();
      if (!x->ch[0]) {
        u = x->ch[1];
      } else if (!x->ch[1]) {
        u = x->ch[0];
      } else {
        u = x->ch[0];
        u->p = NULL;
        while (u->ch[1]) u = u->ch[1];
        u->ch[1] = x->ch[1];
        x->ch[1]->p = u;
        u->splay();
      }
      if (u) u->p = NULL;
      x->key = NULL;
    }
  }
};
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() : key(NULL){};
  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() {
    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() {}
};
const array<int, 4> ainf = {0, inf, 0, inf};
struct node : splay_tree_lct<node*, splay_tree_vchs<node*>> {
  bool val, len, sval;
  int slen;
  int x;
  array<int, 4> pp;
  node() : splay_tree_lct() {
    val = len = sval = 0;
    slen = x = 0;
    pp = ainf;
  }
  void update() {
    splay_tree_lct::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 push() {}
  void reverse() {}
};
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  link_cut_tree<node> lct(2 * n);
  vector<vector<tuple<int, bool, int>>> adj(n);
  for (int i = 1, u, v, w; i < n; ++i) {
    cin >> u >> v >> w;
    --u, --v;
    adj[u].push_back({v, w, i});
    adj[v].push_back({u, w, i});
  }
  function<void(int, int)> dfs = [&](int u, int p) {
    for (auto [v, w, i] : adj[u])
      if (v != p) {
        dfs(v, u);
        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]);
      }
  };
  dfs(0, -1);
  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;
}