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;
struct Edge {
  int t, v, next;
  Edge() {}
  Edge(int a, int b, int c) : t(a), v(b), next(c) {}
};
Edge e[1000005];
int head[500005], bel[500005];
pair<int, int> a[500005];
namespace Tree {
int dfn[500005], dfn1[500005], ed1[500005], dep[500005];
int minn[1000005][20], dfs_cnt, sz;
bool f[500005];
void dfs(int x, int fa) {
  dfn[x] = ++dfs_cnt;
  minn[dfs_cnt][0] = x;
  dfn1[x] = ++sz;
  a[sz] = pair<int, int>(x, f[x]);
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].t != fa) {
      int u = e[i].t;
      bel[(i + 1) >> 1] = u;
      f[u] = f[x] ^ e[i].v;
      dep[u] = dep[x] + 1;
      dfs(u, x);
      minn[++dfs_cnt][0] = x;
    }
  ed1[x] = sz;
}
int cmp(int x, int y) { return (dep[x] < dep[y]) ? x : y; }
int bit[1000005];
void build(int n) {
  dfs(1, 0);
  bit[0] = -1;
  for (int i = 1; i <= dfs_cnt; i++) bit[i] = bit[i >> 1] + 1;
  for (int i = 1; i < 20; i++)
    for (int j = 1; j <= dfs_cnt - (1 << i) + 1; j++)
      minn[j][i] = cmp(minn[j][i - 1], minn[j + (1 << (i - 1))][i - 1]);
}
int lca(int x, int y) {
  if (dfn[x] > dfn[y]) swap(x, y);
  int k = bit[dfn[y] - dfn[x] + 1];
  return cmp(minn[dfn[x]][k], minn[dfn[y] - (1 << k) + 1][k]);
}
int dis(int x, int y) { return dep[x] + dep[y] - 2 * dep[lca(x, y)]; }
}  // namespace Tree
struct Data {
  int x0, y0, x1, y1, d0, d1;
  Data() {}
  Data(int a, int b, int c, int d, int e, int f)
      : x0(a), y0(b), x1(c), y1(d), d0(e), d1(f) {}
};
Data merge(Data x, Data y) {
  Data t;
  int v;
  if (x.d0 > y.d0) {
    t.x0 = x.x0;
    t.y0 = x.y0;
    t.d0 = x.d0;
  } else {
    t.x0 = y.x0;
    t.y0 = y.y0;
    t.d0 = y.d0;
  }
  if (x.x0 != -1 && y.x0 != -1 && (v = Tree::dis(x.x0, y.x0)) > t.d0) {
    t.x0 = x.x0;
    t.y0 = y.x0;
    t.d0 = v;
  }
  if (x.y0 != -1 && y.x0 != -1 && (v = Tree::dis(x.y0, y.x0)) > t.d0) {
    t.x0 = x.y0;
    t.y0 = y.x0;
    t.d0 = v;
  }
  if (x.x0 != -1 && y.y0 != -1 && (v = Tree::dis(x.x0, y.y0)) > t.d0) {
    t.x0 = x.x0;
    t.y0 = y.y0;
    t.d0 = v;
  }
  if (x.y0 != -1 && y.y0 != -1 && (v = Tree::dis(x.y0, y.y0)) > t.d0) {
    t.x0 = x.y0;
    t.y0 = y.y0;
    t.d0 = v;
  }
  if (x.d1 > y.d1) {
    t.x1 = x.x1;
    t.y1 = x.y1;
    t.d1 = x.d1;
  } else {
    t.x1 = y.x1;
    t.y1 = y.y1;
    t.d1 = y.d1;
  }
  if (x.x1 != -1 && y.x1 != -1 && (v = Tree::dis(x.x1, y.x1)) > t.d1) {
    t.x1 = x.x1;
    t.y1 = y.x1;
    t.d1 = v;
  }
  if (x.y1 != -1 && y.x1 != -1 && (v = Tree::dis(x.y1, y.x1)) > t.d1) {
    t.x1 = x.y1;
    t.y1 = y.x1;
    t.d1 = v;
  }
  if (x.x1 != -1 && y.y1 != -1 && (v = Tree::dis(x.x1, y.y1)) > t.d1) {
    t.x1 = x.x1;
    t.y1 = y.y1;
    t.d1 = v;
  }
  if (x.y1 != -1 && y.y1 != -1 && (v = Tree::dis(x.y1, y.y1)) > t.d1) {
    t.x1 = x.y1;
    t.y1 = y.y1;
    t.d1 = v;
  }
  return t;
}
namespace SGT {
Data maxn[2000000];
bool rev[2000000];
inline void pushdown(int o) {
  if (rev[o]) {
    rev[o * 2] ^= 1;
    rev[o * 2 + 1] ^= 1;
    swap(maxn[o * 2].x0, maxn[o * 2].x1);
    swap(maxn[o * 2].y0, maxn[o * 2].y1);
    swap(maxn[o * 2].d0, maxn[o * 2].d1);
    swap(maxn[o * 2 + 1].x0, maxn[o * 2 + 1].x1);
    swap(maxn[o * 2 + 1].y0, maxn[o * 2 + 1].y1);
    swap(maxn[o * 2 + 1].d0, maxn[o * 2 + 1].d1);
    rev[o] = 0;
  }
}
void build(int l, int r, int o) {
  if (l == r) {
    if (a[l].second == 0)
      maxn[o] = Data(a[l].first, a[l].first, -1, -1, 0, -1);
    else
      maxn[o] = Data(-1, -1, a[l].first, a[l].first, -1, 0);
  } else {
    int m = ((l + r) >> 1);
    build(l, m, o * 2);
    build(m + 1, r, o * 2 + 1);
    maxn[o] = merge(maxn[o * 2], maxn[o * 2 + 1]);
  }
}
void update(int l, int r, int o, int lx, int rx) {
  if (l >= lx && r <= rx) {
    rev[o] ^= 1;
    swap(maxn[o].x0, maxn[o].x1);
    swap(maxn[o].y0, maxn[o].y1);
    swap(maxn[o].d0, maxn[o].d1);
  } else {
    pushdown(o);
    int m = ((l + r) >> 1);
    if (m >= lx) update(l, m, o * 2, lx, rx);
    if (m < rx) update(m + 1, r, o * 2 + 1, lx, rx);
    maxn[o] = merge(maxn[o * 2], maxn[o * 2 + 1]);
  }
}
int query() { return max(maxn[1].d0, maxn[1].d1); }
}  // namespace SGT
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    e[2 * i - 1] = Edge(y, z, head[x]);
    head[x] = 2 * i - 1;
    e[2 * i] = Edge(x, z, head[y]);
    head[y] = 2 * i;
  }
  Tree::build(n);
  SGT::build(1, n, 1);
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d", &x);
    SGT::update(1, n, 1, Tree::dfn1[bel[x]], Tree::ed1[bel[x]]);
    printf("%d\n", SGT::query());
  }
  return 0;
}