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;
inline int read() {
  int sum = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch <= '9' && ch >= '0') sum = sum * 10 + ch - '0', ch = getchar();
  return sum;
}
int n;
int head[500010], last[500010 << 1], p, v[500010 << 1], val[500010 << 1];
void add(int a, int b, int c) {
  v[++p] = b;
  last[p] = head[a];
  head[a] = p;
  val[p] = c;
}
int Val[500010];
int Size[2][500010];
int dep[2][500010];
int dfn[2][500010], cnt;
int fan[2][500010];
struct seg_tree {
  int flag;
  int mmax[2][500010 << 2];
  int rev[500010 << 2];
  void gai(int now) {
    rev[now] ^= 1;
    swap(mmax[0][now], mmax[1][now]);
  }
  void pushdown(int now) {
    if (!rev[now]) return;
    gai(now << 1);
    gai(now << 1 | 1);
    rev[now] = 0;
  }
  void build(int now, int l, int r) {
    if (l == r)
      return void(mmax[Val[fan[flag][l]]][now] = dep[flag][fan[flag][l]]);
    int mid = l + r >> 1;
    build(now << 1, l, mid);
    build(now << 1 | 1, mid + 1, r);
    up(now);
  }
  void up(int now) {
    mmax[0][now] = max(mmax[0][now << 1], mmax[0][now << 1 | 1]);
    mmax[1][now] = max(mmax[1][now << 1], mmax[1][now << 1 | 1]);
  }
  void modify(int now, int l, int r, int ll, int rr) {
    if (ll <= l && r <= rr) return void(gai(now));
    pushdown(now);
    int mid = l + r >> 1;
    if (ll <= mid) modify(now << 1, l, mid, ll, rr);
    if (rr >= mid + 1) modify(now << 1 | 1, mid + 1, r, ll, rr);
    up(now);
  }
} S[2];
void dfs(int a, int b, int ty) {
  Size[ty][a] = 1;
  dep[ty][a] = dep[ty][b] + 1;
  fan[ty][dfn[ty][a] = ++cnt] = a;
  for (int i = head[a]; i; i = last[i])
    if (v[i] != b)
      Val[v[i]] = Val[a] ^ val[i], dfs(v[i], a, ty),
      Size[ty][a] += Size[ty][v[i]];
}
int rt1, rt2;
pair<int, int> st[500010];
int sum[500010];
int lowbit(int w) { return w & (-w); }
void upd(int a, int b) {
  for (int i = a; i <= n; i += lowbit(i)) sum[i] ^= b;
}
int cha(int a) {
  int u = 0;
  for (int i = a; i; i -= lowbit(i)) u ^= sum[i];
  return u;
}
int main() {
  n = read();
  for (int i = 1; i < n; i++) {
    int l = read(), r = read(), c = read();
    add(l, r, c);
    add(r, l, c);
    st[i] = make_pair(l, r);
  }
  dfs(1, 0, 0);
  cnt = 0;
  int mmax = 0;
  for (int i = 1; i <= n; i++) mmax = max(mmax, dep[0][i]);
  for (int i = 1; i <= n; i++)
    if (dep[0][i] == mmax) rt1 = i;
  Val[rt1] = 0;
  dfs(rt1, 0, 0);
  cnt = 0;
  mmax = 0;
  for (int i = 1; i <= n; i++) mmax = max(mmax, dep[0][i]);
  for (int i = 1; i <= n; i++)
    if (dep[0][i] == mmax) rt2 = i;
  dfs(rt2, 0, 1);
  S[0].flag = 0, S[1].flag = 1;
  S[0].build(1, 1, n);
  S[1].build(1, 1, n);
  int q = read();
  for (int i = 1; i <= q; i++) {
    int id = read();
    int l = st[id].first, r = st[id].second;
    if (dfn[0][l] > dfn[0][r]) swap(l, r);
    upd(dfn[0][r], 1);
    upd(dfn[0][r] + Size[0][r], 1);
    S[0].modify(1, 1, n, dfn[0][r], dfn[0][r] + Size[0][r] - 1);
    if (dfn[1][l] < dfn[1][r]) swap(l, r);
    S[1].modify(1, 1, n, dfn[1][l], dfn[1][l] + Size[1][l] - 1);
    if (dfn[1][rt1] >= dfn[1][l] && dfn[1][rt1] <= dfn[1][l] + Size[1][l] - 1)
      S[1].modify(1, 1, n, 1, n);
    int col1 = Val[rt1] ^ cha(dfn[0][rt1]);
    int col2 = Val[rt2] ^ cha(dfn[0][rt2]);
    if (col1 == col2)
      cout << dep[0][rt2] - 1 << '\n';
    else {
      int ans = max(S[0].mmax[col1][1], S[1].mmax[col2][1]) - 1;
      cout << ans << '\n';
    }
  }
  return 0;
}