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;
const int maxn = 1e6;
int n, x[555555], y[555555], t[555555], cnt, beg[555555], en[555555],
    dep[555555], crt, vl[555555], b[555555], e[555555], id, Log[1111111],
    ww[555555], q;
struct treenode {
  int lazy, x0, y0, x1, y1, mx0, mx1;
  treenode() {
    x0 = y0 = x1 = y1 = -1;
    mx0 = mx1 = -1e9;
    lazy = 0;
  }
} tree[2222222];
vector<pair<int, int> > g[555555];
int arr[1111111], st[22][1111111];
void dfs(int i, int fa) {
  dep[i] = dep[fa] + 1;
  beg[i] = ++cnt;
  for (int j = 0; j < g[i].size(); j++) {
    int to = g[i][j].first, val = g[i][j].second;
    if (to == fa) continue;
    vl[to] = vl[i] ^ val;
    dfs(to, i);
  }
  en[i] = cnt;
}
void rfs(int i, int fa) {
  ++crt;
  b[i] = crt;
  arr[crt] = dep[i];
  for (int j = 0; j < g[i].size(); j++) {
    int to = g[i][j].first;
    if (to == fa) continue;
    rfs(to, i);
    ++crt;
    arr[crt] = dep[i];
  }
  e[i] = crt;
}
int get(int l, int r) {
  int x = Log[r - l + 1];
  return min(st[x][l], st[x][r - (1 << x) + 1]);
}
int dist(int x, int y) {
  if (x == -1 || y == -1) return -1e9;
  if (b[x] > b[y]) swap(x, y);
  return dep[x] + dep[y] - (get(b[x], e[y]) << 1);
}
void pushup(int node) {
  int mx = -2e9, cr;
  if (tree[node << 1].mx0 > mx) {
    mx = tree[node << 1].mx0;
    tree[node].x0 = tree[node << 1].x0;
    tree[node].y0 = tree[node << 1].y0;
  }
  if (tree[(node << 1) | 1].mx0 > mx) {
    mx = tree[(node << 1) | 1].mx0;
    tree[node].x0 = tree[(node << 1) | 1].x0;
    tree[node].y0 = tree[(node << 1) | 1].y0;
  }
  cr = dist(tree[node << 1].x0, tree[(node << 1) | 1].x0);
  if (cr > mx) {
    mx = cr;
    tree[node].x0 = tree[node << 1].x0;
    tree[node].y0 = tree[(node << 1) | 1].x0;
  }
  cr = dist(tree[node << 1].x0, tree[(node << 1) | 1].y0);
  if (cr > mx) {
    mx = cr;
    tree[node].x0 = tree[node << 1].x0;
    tree[node].y0 = tree[(node << 1) | 1].y0;
  }
  cr = dist(tree[node << 1].y0, tree[(node << 1) | 1].x0);
  if (cr > mx) {
    mx = cr;
    tree[node].x0 = tree[node << 1].y0;
    tree[node].y0 = tree[(node << 1) | 1].x0;
  }
  cr = dist(tree[node << 1].y0, tree[(node << 1) | 1].y0);
  if (cr > mx) {
    mx = cr;
    tree[node].x0 = tree[node << 1].y0;
    tree[node].y0 = tree[(node << 1) | 1].y0;
  }
  tree[node].mx0 = mx;
  mx = -2e9;
  if (tree[node << 1].mx1 > mx) {
    mx = tree[node << 1].mx1;
    tree[node].x1 = tree[node << 1].x1;
    tree[node].y1 = tree[node << 1].y1;
  }
  if (tree[(node << 1) | 1].mx1 > mx) {
    mx = tree[(node << 1) | 1].mx1;
    tree[node].x1 = tree[(node << 1) | 1].x1;
    tree[node].y1 = tree[(node << 1) | 1].y1;
  }
  cr = dist(tree[node << 1].x1, tree[(node << 1) | 1].x1);
  if (cr > mx) {
    mx = cr;
    tree[node].x1 = tree[node << 1].x1;
    tree[node].y1 = tree[(node << 1) | 1].x1;
  }
  cr = dist(tree[node << 1].x1, tree[(node << 1) | 1].y1);
  if (cr > mx) {
    mx = cr;
    tree[node].x1 = tree[node << 1].x1;
    tree[node].y1 = tree[(node << 1) | 1].y1;
  }
  cr = dist(tree[node << 1].y1, tree[(node << 1) | 1].x1);
  if (cr > mx) {
    mx = cr;
    tree[node].x1 = tree[node << 1].y1;
    tree[node].y1 = tree[(node << 1) | 1].x1;
  }
  cr = dist(tree[node << 1].y1, tree[(node << 1) | 1].y1);
  if (cr > mx) {
    mx = cr;
    tree[node].x1 = tree[node << 1].y1;
    tree[node].y1 = tree[(node << 1) | 1].y1;
  }
  tree[node].mx1 = mx;
}
void push(int node) {
  tree[node].lazy ^= 1;
  swap(tree[node].x0, tree[node].x1);
  swap(tree[node].y0, tree[node].y1);
  swap(tree[node].mx0, tree[node].mx1);
}
void pushdown(int node) {
  if (tree[node].lazy) {
    push(node << 1);
    push((node << 1) | 1);
    tree[node].lazy = 0;
  }
}
void build(int l, int r, int node) {
  if (l == r) {
    if (vl[ww[l]] == 1) {
      tree[node].x0 = tree[node].y0 = ww[l];
      tree[node].x1 = tree[node].y1 = -1;
      tree[node].mx0 = 0;
      tree[node].mx1 = -1e9;
    } else {
      tree[node].x1 = tree[node].y1 = ww[l];
      tree[node].x0 = tree[node].y0 = -1;
      tree[node].mx0 = -1e9;
      tree[node].mx1 = 0;
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, node << 1);
  build(mid + 1, r, (node << 1) | 1);
  pushup(node);
}
void update(int beg, int en, int l, int r, int node) {
  if (l > en || r < beg) return;
  if (l >= beg && r <= en) {
    push(node);
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(node);
  update(beg, en, l, mid, node << 1);
  update(beg, en, mid + 1, r, (node << 1) | 1);
  pushup(node);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &x[i], &y[i], &t[i]);
    g[x[i]].push_back(make_pair(y[i], t[i]));
    g[y[i]].push_back(make_pair(x[i], t[i]));
  }
  dfs(1, 0);
  rfs(1, 0);
  for (int i = 2; i <= maxn; i++) Log[i] = Log[i >> 1] + 1;
  for (int i = 1; i <= crt; i++) st[0][i] = arr[i];
  for (int i = 1; i <= 20; i++) {
    for (int j = 1; j <= crt; j++) {
      st[i][j] = st[i - 1][j];
      if (j + (1 << (i - 1)) <= crt)
        st[i][j] = min(st[i][j], st[i - 1][j + (1 << (i - 1))]);
    }
  }
  for (int i = 1; i <= n; i++) ww[beg[i]] = i;
  build(1, n, 1);
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &id);
    if (dep[x[id]] < dep[y[id]]) swap(x[id], y[id]);
    update(beg[x[id]], en[x[id]], 1, n, 1);
    printf("%d\n", max(tree[1].mx0, tree[1].mx1));
  }
  return 0;
}