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;
vector<pair<int, int> > AdjList[500005];
int id[500005];
int A[500005];
int B[500005];
int T[500005];
int ans[500005];
int n, m;
int dist1[500005];
int dist2[500005];
int indx[500005];
int right1[500005];
int inv[500005];
bool visited[500005];
int cnt;
void dfs(int u, int d1, int d2) {
  if (visited[u]) {
    return;
  }
  visited[u] = true;
  dist1[u] = d1;
  dist2[u] = d2;
  indx[u] = cnt;
  inv[cnt] = u;
  cnt++;
  for (pair<int, int> v : AdjList[u]) {
    if (!visited[v.first]) {
      dfs(v.first, d1 + 1, d2 ^ v.second);
    }
  }
  right1[u] = cnt - 1;
}
int findFurthestVertex(int root) {
  for (int i = 1; i <= n; i++) {
    visited[i] = 0;
  }
  cnt = 1;
  dfs(root, 0, 0);
  int maxDist = -1;
  int bestVertex = -1;
  for (int i = 1; i <= n; i++) {
    if (dist1[i] > maxDist) {
      maxDist = dist1[i];
      bestVertex = i;
    }
  }
  return bestVertex;
}
int max0[(1 << 20) + 5];
int max1[(1 << 20) + 5];
int lazyterbalik[(1 << 20) + 5];
void init(int i = 1, int s = 1, int e = n) {
  if (s == e) {
    lazyterbalik[i] = 0;
    if (dist2[inv[s]] == 0) {
      max0[i] = dist1[inv[s]];
      max1[i] = -1;
    } else {
      max1[i] = dist1[inv[s]];
      max0[i] = -1;
    }
    return;
  } else {
    int l = (i << 1);
    int r = (i << 1) | 1;
    int m = (s + e) >> 1;
    init(l, s, m);
    init(r, m + 1, e);
    max0[i] = max(max0[l], max0[r]);
    max1[i] = max(max1[l], max1[r]);
    lazyterbalik[i] = 0;
  }
}
void propagate(int i, int s, int e) {
  if (lazyterbalik[i]) {
    if (s == e) {
      swap(max0[i], max1[i]);
      lazyterbalik[i] = 0;
      return;
    } else {
      int l = (i << 1);
      int r = (i << 1) | 1;
      lazyterbalik[l] ^= 1;
      lazyterbalik[r] ^= 1;
      swap(max0[i], max1[i]);
      lazyterbalik[i] = 0;
      return;
    }
  }
}
void update(int x, int y, int i = 1, int s = 1, int e = n) {
  propagate(i, s, e);
  if (x <= s && e <= y) {
    lazyterbalik[i] ^= 1;
    return;
  } else {
    int l = (i << 1);
    int r = (i << 1) | 1;
    int m = (s + e) >> 1;
    if (x > m) {
      update(x, y, r, m + 1, e);
    } else if (y <= m) {
      update(x, y, l, s, m);
    } else {
      update(x, y, l, s, m);
      update(x, y, r, m + 1, e);
    }
    propagate(l, s, m);
    propagate(r, m + 1, e);
    max0[i] = max(max0[l], max0[r]);
    max1[i] = max(max1[l], max1[r]);
  }
}
int query() {
  propagate(1, 1, n);
  return max0[1];
}
void findAns(int root) {
  findFurthestVertex(root);
  init();
  for (int i = 1; i <= m; i++) {
    int u = A[id[i]];
    int v = B[id[i]];
    if (dist1[u] < dist1[v]) {
      swap(u, v);
    }
    update(indx[u], right1[u]);
    ans[i] = max(ans[i], query());
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &A[i], &B[i], &T[i]);
    AdjList[A[i]].push_back(pair<int, int>(B[i], T[i]));
    AdjList[B[i]].push_back(pair<int, int>(A[i], T[i]));
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &id[i]);
  }
  memset(ans, -1, sizeof(ans));
  int root = findFurthestVertex(1);
  findAns(root);
  int root2 = findFurthestVertex(root);
  findAns(root2);
  for (int i = 1; i <= m; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}