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 eu[500005], ev[500005], ew[500005];
int n, q, tot, st[500005], ed[500005];
int val[2][500005], dist[2][500005];
int dep[500005];
vector<pair<int, int> > G[500005];
void add_edge(int u, int v, int w) {
  G[u].push_back(pair<int, int>(v, w));
  G[v].push_back(pair<int, int>(u, w));
}
void dfs(int v, int p, int d, bool flag = false) {
  if (flag) st[v] = ++tot;
  dep[v] = d;
  for (auto e : G[v]) {
    int to = e.first;
    if (to == p) continue;
    dfs(to, v, d + 1, flag);
  }
  if (flag) ed[v] = tot;
}
void dfs2(int v, int p, int d, int sgn, int id) {
  val[id][st[v]] = sgn;
  dist[id][st[v]] = d;
  for (auto e : G[v]) {
    int to = e.first, x = e.second;
    if (to == p) continue;
    dfs2(to, v, d + 1, sgn ^ x, id);
  }
}
struct segtree {
  int lazy[4 * 500005];
  int maxi[2][4 * 500005];
  void pushup(int k) {
    for (int i = 0; i < 2; i++)
      maxi[i][k] = max(maxi[i][k * 2], maxi[i][k * 2 + 1]);
  }
  void flip(int k) {
    swap(maxi[0][k], maxi[1][k]);
    lazy[k] ^= 1;
  }
  void pushdown(int k) {
    if (!lazy[k]) return;
    for (int i = k * 2; i <= k * 2 + 1; i++) flip(i);
    lazy[k] = 0;
  }
  void build(int k, int l, int r, int id) {
    lazy[k] = 0;
    if (l == r) {
      if (val[id][l] == 0) {
        maxi[0][k] = dist[id][l];
        maxi[1][k] = -1000000000;
      } else {
        maxi[0][k] = -1000000000;
        maxi[1][k] = dist[id][l];
      }
      return;
    }
    int mid = (l + r) / 2;
    build(k * 2, l, mid, id);
    build(k * 2 + 1, mid + 1, r, id);
    pushup(k);
  }
  void update(int k, int l, int r, int x, int y) {
    if (l > y || x > r) return;
    if (l >= x && r <= y) {
      flip(k);
      return;
    }
    pushdown(k);
    int mid = (l + r) / 2;
    update(k * 2, l, mid, x, y);
    update(k * 2 + 1, mid + 1, r, x, y);
    pushup(k);
  }
  int query() { return maxi[0][1]; }
} seg[2];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    scanf("%d%d%d", &eu[i], &ev[i], &ew[i]);
    add_edge(eu[i], ev[i], ew[i]);
  }
  dfs(1, 0, 0, true);
  int maxd = -1000000000, u = -1, v = -1;
  for (int i = 1; i <= n; i++) {
    if (dep[i] > maxd) {
      maxd = dep[i];
      u = i;
    }
  }
  dfs(u, 0, 0);
  maxd = -1000000000;
  for (int i = 1; i <= n; i++) {
    if (dep[i] > maxd) {
      maxd = dep[i];
      v = i;
    }
  }
  dfs(1, 0, 0);
  dfs2(u, 0, 0, 0, 0);
  dfs2(v, 0, 0, 0, 1);
  seg[0].build(1, 1, n, 0);
  seg[1].build(1, 1, n, 1);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int id;
    scanf("%d", &id);
    id--;
    int uu = eu[id], vv = ev[id];
    if (dep[uu] > dep[vv]) swap(uu, vv);
    int l = st[vv], r = ed[vv];
    if (st[u] >= l && st[u] <= r) {
      seg[0].update(1, 1, n, 1, l - 1);
      seg[0].update(1, 1, n, r + 1, n);
    } else {
      seg[0].update(1, 1, n, l, r);
    }
    if (st[v] >= l && st[v] <= r) {
      seg[1].update(1, 1, n, 1, l - 1);
      seg[1].update(1, 1, n, r + 1, n);
    } else {
      seg[1].update(1, 1, n, l, r);
    }
    printf("%d\n", max(seg[0].query(), seg[1].query()));
  }
  return 0;
}