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;
const int max_n = 1111222, inf = 100111222;
const int max_v = 2 * max_n;
struct state {
  int max_suf, max_pref, max_h, res;
  state() : max_suf(0), max_pref(0), max_h(0), res(0) {}
  state(int max_suf, int max_pref, int max_h, int res)
      : max_suf(max_suf), max_pref(max_pref), max_h(max_h), res(res) {}
};
struct node {
  state a[2];
  int min_h;
  node() {}
  node(int h, int col) {
    min_h = h;
    a[col] = {-h, -h, h, 0};
    a[col ^ 1] = {-inf, -inf, -inf, -inf};
  }
  void flip() { swap(a[0], a[1]); }
  int get_res() const { return max(a[0].res, a[1].res); }
};
state merge(const state &a, const state &b, int amnh, int bmnh) {
  return {
      max(b.max_suf, max(a.max_suf, a.max_h - 2 * bmnh)),
      max(a.max_pref, max(b.max_pref, b.max_h - 2 * amnh)),
      max(a.max_h, b.max_h),
      max(a.res, max(b.res, max(a.max_suf + b.max_h, a.max_h + b.max_pref)))};
}
node merge(const node &a, const node &b) {
  node res;
  res.min_h = min(a.min_h, b.min_h);
  res.a[0] = merge(a.a[0], b.a[0], a.min_h, b.min_h);
  res.a[1] = merge(a.a[1], b.a[1], a.min_h, b.min_h);
  return res;
}
ostream &operator<<(ostream &os, const state &s) {
  os << s.max_suf << " " << s.max_pref << " " << s.max_h << " " << s.res;
  return os;
}
ostream &operator<<(ostream &os, const node &nd) {
  os << nd.min_h << "col0 = " << nd.a[0] << " "
     << "col1 = " << nd.a[1];
  return os;
}
struct tree {
  node a[max_v];
  bool f[max_v];
  void build(int v, int l, int r, const vector<int> &h,
             const vector<int> &col) {
    if (l == r) {
      assert(v < max_v);
      a[v] = node(h[l], col[l]);
      f[v] = 0;
      return;
    }
    int mid = (l + r) / 2;
    build(2 * v, l, mid, h, col);
    build(2 * v + 1, mid + 1, r, h, col);
    a[v] = merge(a[2 * v], a[2 * v + 1]);
  }
  void push(int v) {
    if (f[v]) {
      f[2 * v] ^= 1;
      f[2 * v + 1] ^= 1;
      a[2 * v].flip();
      a[2 * v + 1].flip();
      f[v] = 0;
    }
  }
  void update(int v, int tl, int tr, int l, int r) {
    if (tl == l && tr == r) {
      f[v] ^= 1;
      a[v].flip();
      return;
    }
    push(v);
    int mid = (tl + tr) / 2;
    if (r <= mid) {
      update(2 * v, tl, mid, l, r);
    } else if (l > mid) {
      update(2 * v + 1, mid + 1, tr, l, r);
    } else {
      update(2 * v, tl, mid, l, mid);
      update(2 * v + 1, mid + 1, tr, mid + 1, r);
    }
    a[v] = merge(a[2 * v], a[2 * v + 1]);
  }
};
int n, m, col[max_n], h[max_n], l[max_n], r[max_n];
int U[max_n], V[max_n], x[max_n];
vector<int> g[max_n], order_h, order_col;
tree t;
void dfs(int v, int p) {
  l[v] = order_h.size();
  order_h.push_back(h[v]);
  order_col.push_back(col[v]);
  for (int id : g[v]) {
    const int to = U[id] ^ V[id] ^ v;
    if (to == p) {
      continue;
    }
    if (to != V[id]) {
      swap(U[id], V[id]);
    }
    col[to] = col[v] ^ x[id];
    h[to] = h[v] + 1;
    dfs(to, v);
    order_h.push_back(h[v]);
    order_col.push_back(col[v]);
  }
  r[v] = order_h.size() - 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d%d", &U[i], &V[i], &x[i]);
    --U[i];
    --V[i];
    g[U[i]].push_back(i);
    g[V[i]].push_back(i);
  }
  dfs(0, -1);
  t.build(1, 0, order_h.size() - 1, order_h, order_col);
  scanf("%d", &m);
  while (m--) {
    int id;
    scanf("%d", &id);
    t.update(1, 0, order_h.size() - 1, l[V[id]], r[V[id]]);
    int ans = t.a[1].get_res();
    ans = max(ans, 0);
    printf("%d\n", ans);
  }
  return 0;
}