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 node {
  int x, v;
  node(int nx = 0, int nv = 0) {
    x = nx;
    v = nv;
  }
};
int n, u, v, t, gl, gr, dp[500005], st[20][1000005], lg[1000005],
    tr[2000005][2][2], rev[2000005], ans[2000005][2];
int dep[500005], cnt = 0, tot = 0, dfn[500005], seq[500005], ed[500005],
                 e[500005][2], m, dm[500005];
vector<node> s[500005];
void dfs(int p, int f) {
  node v;
  cnt++;
  tot++;
  dfn[p] = ed[p] = cnt;
  dm[p] = tot;
  st[0][tot] = dep[p];
  seq[cnt] = p;
  for (int i = 0; i < s[p].size(); i++) {
    v = s[p][i];
    if (v.x == f) continue;
    dp[v.x] = dp[p] ^ v.v;
    dep[v.x] = dep[p] + 1;
    dfs(v.x, p);
    tot++;
    st[0][tot] = dep[p];
  }
  ed[p] = cnt;
}
int ask(int x, int y) {
  int len = y - x + 1;
  return min(st[lg[len]][x], st[lg[len]][y - (1 << lg[len]) + 1]);
}
int dist(int x, int y) {
  return dep[x] + dep[y] - 2 * ask(min(dm[x], dm[y]), max(dm[x], dm[y]));
}
void upd(int p) {
  int u, v, x, y, tmp;
  u = p << 1;
  v = u + 1;
  for (int ty = 0; ty < 2; ty++) {
    if (ans[u][ty] > ans[v][ty]) {
      ans[p][ty] = ans[u][ty];
      tr[p][ty][0] = tr[u][ty][0];
      tr[p][ty][1] = tr[u][ty][1];
    } else {
      ans[p][ty] = ans[v][ty];
      tr[p][ty][0] = tr[v][ty][0];
      tr[p][ty][1] = tr[v][ty][1];
    }
    if (ans[u][ty] == -1 || ans[v][ty] == -1) continue;
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        x = tr[u][ty][j];
        y = tr[v][ty][k];
        tmp = dist(x, y);
        if (tmp > ans[p][ty]) {
          ans[p][ty] = tmp;
          tr[p][ty][0] = x;
          tr[p][ty][1] = y;
        }
      }
    }
  }
}
void act(int p) {
  rev[p] ^= 1;
  swap(ans[p][0], ans[p][1]);
  swap(tr[p][0][0], tr[p][1][0]);
  swap(tr[p][0][1], tr[p][1][1]);
}
void push(int p) {
  if (!rev[p]) return;
  act(2 * p);
  act(2 * p + 1);
  rev[p] = 0;
}
void build(int p, int l, int r) {
  ans[p][0] = ans[p][1] = -1;
  if (l == r) {
    ans[p][dp[seq[l]]] = 0;
    tr[p][dp[seq[l]]][0] = tr[p][dp[seq[l]]][1] = seq[l];
    return;
  }
  int mid = (l + r) / 2;
  build(2 * p, l, mid);
  build(2 * p + 1, mid + 1, r);
  upd(p);
}
void modify(int p, int l, int r) {
  if (l > gr || r < gl) return;
  if (l >= gl && r <= gr) {
    act(p);
    return;
  }
  int mid = (l + r) / 2;
  push(p);
  modify(2 * p, l, mid);
  modify(2 * p + 1, mid + 1, r);
  upd(p);
}
int main() {
  lg[1] = 0;
  for (int i = 2; i <= 1000000; i++) lg[i] = lg[i / 2] + 1;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &u, &v, &t);
    s[u].push_back(node(v, t));
    s[v].push_back(node(u, t));
    e[i][0] = u;
    e[i][1] = v;
  }
  dfs(1, 0);
  for (int i = 1; i < 20; i++) {
    for (int j = 1; j <= tot; j++) {
      if (j + (1 << (i - 1)) > tot)
        st[i][j] = st[i - 1][j];
      else
        st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
    }
  }
  build(1, 1, n);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &u);
    u = max(dfn[e[u][0]], dfn[e[u][1]]);
    gl = u;
    gr = ed[seq[u]];
    modify(1, 1, n);
    printf("%d\n", max(ans[1][0], ans[1][1]));
  }
  return 0;
}