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 {
  void link(node* u, node* v) {
    access(v);
    access(u);
    set_link(v, u, 0);
    v->update();
  }
  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; }
  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();
  }
};
const int inf = -1e8;
const array<int, 4> ainf = {0, inf, 0, inf};
struct node : splay_tree<node*> {
  bool val, len, sval;
  int slen;
  int x;
  array<int, 4> pp;
  priority_queue<int> vx, dvx;
  array<priority_queue<int>, 2> vp, dvp;
  node() : splay_tree() {
    val = len = sval = 0;
    slen = x = 0;
    pp = ainf;
  }
  template <typename T>
  void fix(priority_queue<T>& p, priority_queue<T>& d) {
    while (!d.empty() && p.top() == d.top()) p.pop(), d.pop();
  }
  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 (!vx.empty()) {
      x = max(x, vx.top());
    }
    if (!vp[0].empty()) {
      v1[0] = vp[0].top();
      v1[1] = vp[1].top();
      if (!val) {
        vp[0].pop();
        vp[1].pop();
        fix(vp[0], dvp[0]);
        fix(vp[1], dvp[1]);
        if (!vp[0].empty()) {
          v2[0] = vp[0].top();
          v2[1] = vp[1].top();
        }
        vp[0].push(v1[0]);
        vp[1].push(v1[1]);
      }
    }
    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) {
      vx.push(v->x);
      vp[0].push(v->pp[0]);
      vp[1].push(v->pp[1]);
    } else {
      dvx.push(v->x);
      dvp[0].push(v->pp[0]);
      dvp[1].push(v->pp[1]);
      fix(vx, dvx);
      fix(vp[0], dvp[0]);
      fix(vp[1], dvp[1]);
    }
  }
};
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;
}