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;
int n, m;
struct E {
  int v, w;
};
vector<E> e[500005];
inline void addEdge(int u, int v, int w) { e[u].push_back(E{v, w}); }
struct SegmentTree {
  struct T {
    int l, r, mid, tag, v[2];
  } t[500005 << 2];
  int L[500005], R[500005], dep[500005], rk[500005], a[500005], tot;
  inline void pushup(int rt) {
    t[rt].v[0] = max(t[(rt << 1)].v[0], t[(rt << 1 | 1)].v[0]),
    t[rt].v[1] = max(t[(rt << 1)].v[1], t[(rt << 1 | 1)].v[1]);
  }
  inline void pushdown(int rt) {
    if (t[rt].tag)
      t[(rt << 1)].tag ^= 1, t[(rt << 1 | 1)].tag ^= 1,
          swap(t[(rt << 1)].v[0], t[(rt << 1)].v[1]),
          swap(t[(rt << 1 | 1)].v[0], t[(rt << 1 | 1)].v[1]), t[rt].tag = 0;
  }
  void dfs(int u, int fa) {
    L[u] = ++tot, rk[tot] = u, dep[u] = dep[fa] + 1;
    for (auto it : e[u])
      if (it.v != fa) a[it.v] = a[u] ^ it.w, dfs(it.v, u);
    R[u] = tot;
  }
  void build(int rt, int l, int r) {
    int mid = (l + r) >> 1;
    t[rt].l = l, t[rt].r = r, t[rt].mid = mid;
    if (l == r) {
      t[rt].v[a[rk[l]]] = dep[rk[l]];
      return;
    }
    build((rt << 1), l, mid), build((rt << 1 | 1), mid + 1, r), pushup(rt);
  }
  void update(int rt, int l, int r) {
    if (l <= t[rt].l && t[rt].r <= r) {
      t[rt].tag ^= 1, swap(t[rt].v[0], t[rt].v[1]);
      return;
    }
    pushdown(rt);
    if (l <= t[rt].mid) update((rt << 1), l, r);
    if (t[rt].mid < r) update((rt << 1 | 1), l, r);
    pushup(rt);
  }
} T0, T1;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
    if (ch == -1) return 0;
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
int ans, pos;
int dis[500005];
struct node {
  int u, v, w;
} g[500005];
int qwq;
void dfs(int u, int fa) {
  if (ans < dis[u]) ans = dis[u], pos = u;
  for (auto it : e[u])
    if (it.v != fa) dis[it.v] = dis[u] + 1, dfs(it.v, u);
}
int main() {
  n = read();
  int pos1, pos2;
  for (int i = 1, u, v, w; i < n; i++)
    u = read(), v = read(), w = read(), addEdge(u, v, w), addEdge(v, u, w),
    g[++qwq] = {u, v, w};
  dfs(1, 0), pos1 = pos, ans = 0, dis[pos] = 0, dfs(pos, 0), pos2 = pos;
  T0.dfs(pos1, 0), T0.build(1, 1, n), T1.dfs(pos2, 0), T1.build(1, 1, n),
      m = read();
  while (m--) {
    int x, y, id;
    id = read(), x = g[id].u, y = g[id].v;
    if (T0.dep[x] < T0.dep[y]) swap(x, y);
    T0.update(1, T0.L[x], T0.R[x]);
    if (T1.dep[x] < T1.dep[y]) swap(x, y);
    T1.update(1, T1.L[x], T1.R[x]);
    printf("%d\n", max(T0.t[1].v[0], T1.t[1].v[0]) - 1);
  }
  return 0;
}