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;
using ll = long long;
enum seg_type { seg_not_lazy, seg_lazy, seg_beats };
template <class node, seg_type type>
struct segment_tree {
  using node_container = typename node::node_container;
  using lazy_container = typename node::lazy_container;

 private:
  template <const bool T = (type != seg_not_lazy),
            typename enable_if<T, bool>::type = 0>
  inline void push(int x, int b, int e, int m) {
    if (st[x].lazy()) {
      st[x + 1].apply(b, m, st[x].lazy);
      st[x + ((m - b) << 1)].apply(m, e, st[x].lazy);
      st[x].lazy = lazy_container();
    }
  }
  template <const bool T = (type != seg_not_lazy),
            typename enable_if<!T, bool>::type = 0>
  inline void push(int x, int b, int e, int m) {}
  template <typename RAIter>
  void build(int x, int b, int e, const RAIter &a) {
    if (b + 1 == e) {
      st[x].nod.build(a[b]);
      return;
    }
    int m = (b + e) >> 1;
    int y = x + ((m - b) << 1);
    build(x + 1, b, m, a);
    build(y, m, e, a);
    st[x].nod = st[x + 1].nod + st[y].nod;
  }
  template <const bool T = (type == seg_beats),
            typename enable_if<!T, bool>::type = 0>
  void update_(int x, int b, int e) {
    if (lo <= b && e <= hi) {
      st[x].apply(b, e, lazy);
      return;
    }
    int m = (b + e) >> 1;
    int y = x + ((m - b) << 1);
    push(x, b, e, m);
    if (lo < m) update_(x + 1, b, m);
    if (m < hi) update_(y, m, e);
    st[x].nod = st[x + 1].nod + st[y].nod;
  }
  template <const bool T = (type == seg_beats),
            typename enable_if<T, bool>::type = 0>
  void update_(int x, int b, int e) {
    if (st[x].break_condition(lazy)) return;
    if (lo <= b && e <= hi && st[x].tag_condition(lazy)) {
      st[x].apply(b, e, lazy);
      return;
    }
    int m = (b + e) >> 1;
    int y = x + ((m - b) << 1);
    push(x, b, e, m);
    if (lo < m) update_(x + 1, b, m);
    if (m < hi) update_(y, m, e);
    st[x].nod = st[x + 1].nod + st[y].nod;
  }
  node_container query(int x, int b, int e) {
    if (lo <= b && e <= hi) return st[x].nod;
    int m = (b + e) >> 1;
    int y = x + ((m - b) << 1);
    push(x, b, e, m);
    if (m >= hi) return query(x + 1, b, m);
    if (m <= lo) return query(y, m, e);
    return query(x + 1, b, m) + query(y, m, e);
  }
  template <class P>
  int find_first(int x, int b, int e, const P &f) {
    if (b + 1 == e) return f(st[x]) ? b : -1;
    int m = (b + e) >> 1;
    int y = x + ((m - b) << 1);
    push(x, b, e, m);
    if (lo < m && f(st[x + 1])) {
      auto t = find_first(x + 1, b, m, f);
      if (t != -1) return t;
    }
    if (m < hi && f(st[y])) return find_first(y, m, e, f);
    return -1;
  }
  template <class P>
  int find_last(int x, int b, int e, const P &f) {
    if (b + 1 == e) return f(st[x]) ? b : -1;
    int m = (b + e) >> 1;
    int y = x + ((m - b) << 1);
    push(x, b, e, m);
    if (m < hi && f(st[y])) {
      auto t = find_last(y, m, e, f);
      if (t != -1) return t;
    }
    if (lo < m && f(st[x + 1])) return find_last(x + 1, b, m, f);
    return -1;
  }
  lazy_container lazy;
  int n, lo, hi;
  vector<node> st;

 public:
  template <typename RAIter>
  void build(const RAIter &a) {
    build(0, 0, n, a);
  }
  void update(int l, int r, const lazy_container &x) {
    lo = l, hi = r, lazy = x, update_(0, 0, n);
  }
  node_container query(int l, int r) { return lo = l, hi = r, query(0, 0, n); }
  template <class P>
  int find_first(int l, int r, const P &f) {
    return lo = l, hi = r, find_first(0, 0, n, f);
  }
  template <class P>
  int find_last(int l, int r, const P &f) {
    return lo = l, hi = r, find_last(0, 0, n, f);
  }
  segment_tree(int n) : n(n), st(2 * n - 1) {}
};
struct node {
  struct node_container {
    struct data {
      int a, b, c, ab, bc, abc;
      data() { a = b = c = ab = bc = abc = -(1 << 20); }
      friend data operator+(const data &l, const data &r) {
        data res;
        res.a = max(l.a, r.a);
        res.b = max(l.b, r.b);
        res.c = max(l.c, r.c);
        res.ab = max({l.ab, r.ab, l.a + r.b});
        res.bc = max({l.bc, r.bc, l.b + r.c});
        res.abc = max({l.abc, r.abc, l.a + r.bc, l.ab + r.c});
        return res;
      }
    };
    array<data, 2> d;
    template <typename T>
    inline void build(T a) {
      int t = a >= 1 << 30;
      if (t) a -= 1 << 30;
      d[0].b = d[1].b = -2 * a;
      d[t].a = d[t].c = a;
      d[t].ab = d[t].bc = -a;
      d[t].abc = 0;
    }
    friend node_container operator+(node_container l, const node_container &r) {
      node_container a;
      a.d = {l.d[0] + r.d[0], l.d[1] + r.d[1]};
      return a;
    }
    node_container() {}
  } nod;
  struct lazy_container {
    int rev = 0;
    inline bool operator()() { return rev; }
    lazy_container(int rev = 0) : rev(rev) {}
  } lazy;
  inline void apply(int l, int r, lazy_container &p) {
    lazy.rev ^= 1;
    swap(nod.d[0], nod.d[1]);
  }
};
struct diameter {
  int n;
  vector<array<int, 3>> edges;
  vector<vector<array<int, 2>>> g;
  vector<int> h, tin, tout, tour;
  segment_tree<node, seg_lazy> st;
  diameter(vector<array<int, 3>> el)
      : n(el.size() + 1),
        edges(el),
        g(n),
        h(n),
        tin(n),
        tout(n),
        st(2 * n - 1) {
    for (auto &[x, y, z] : el) {
      g[x].push_back({y, z});
      g[y].push_back({x, z});
    }
    dfs(0, 0);
    for (auto &[x, y, z] : edges)
      if (tin[x] > tin[y]) swap(x, y);
    st.build(tour.begin());
  }
  void dfs(int v, int dep) {
    tin[v] = tour.size();
    tour.push_back(dep + (h[v] << 30));
    for (auto &[i, w] : g[v]) {
      g[i].erase(find(g[i].begin(), g[i].end(), array<int, 2>{v, w}));
      h[i] = h[v] ^ w;
      dfs(i, dep + 1);
      tour.push_back(dep + (h[v] << 30));
    }
    tout[v] = tour.size();
  }
  int update(int v) {
    v = edges[v][1];
    st.update(tin[v], tout[v], node::lazy_container(1));
    auto t = st.query(0, 2 * n - 1);
    return max(t.d[0].abc, t.d[1].abc);
  }
};
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, x, y;
  cin >> n;
  vector<array<int, 3>> er(n - 1);
  for (auto &i : er) cin >> i[0] >> i[1] >> i[2], --i[0], --i[1];
  diameter d(er);
  for (cin >> x; x--;) {
    cin >> y;
    cout << d.update(--y) << '\n';
  }
  return 0;
}