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;
long long read() {
  long long x;
  scanf("%lld", &x);
  return x;
}
const int N = 5.1e5;
vector<int> es[N];
int dis[N];
int dfn[N];
int dfe[N];
void dfs(int x, int fa) {
  static int dft;
  if (fa == -1) dft = 0;
  dfn[x] = ++dft;
  dis[x] = fa == -1 ? 0 : dis[fa] + 1;
  for (int i = 0; i < (int)(es[x].size()); i++) {
    int v = es[x][i];
    if (v == fa) continue;
    dfs(v, x);
  }
  dfe[x] = dft;
}
struct SegTree {
  int dis[4 * N][2];
  bool flag[4 * N];
  void build(const int *a, int ll, int rr, int id) {
    if (ll == rr) {
      dis[id][0] = a[ll];
      dis[id][1] = INT_MIN;
      flag[id] = false;
      return;
    }
    int mid = (ll + rr) >> 1;
    build(a, ll, mid, id * 2);
    build(a, mid + 1, rr, id * 2 + 1);
    for (int i = 0; i < (int)(2); i++)
      dis[id][i] =
          max(dis[id * 2][i ^ flag[id]], dis[id * 2 + 1][i ^ flag[id]]);
  }
  void flip(int l, int r, int ll, int rr, int id) {
    if (l <= ll && r >= rr) {
      flag[id] ^= 1;
      swap(dis[id][0], dis[id][1]);
      return;
    }
    if (r < ll || l > rr) return;
    int mid = (ll + rr) >> 1;
    flip(l, r, ll, mid, id * 2);
    flip(l, r, mid + 1, rr, id * 2 + 1);
    for (int i = 0; i < (int)(2); i++)
      dis[id][i] =
          max(dis[id * 2][i ^ flag[id]], dis[id * 2 + 1][i ^ flag[id]]);
  }
};
int n;
int u[N], v[N], t[N];
struct Solver {
  int dis[N];
  int dfn[N];
  int dfe[N];
  SegTree tr;
  Solver() {
    memcpy(dis, ::dis, sizeof(dis));
    memcpy(dfn, ::dfn, sizeof(dfn));
    memcpy(dfe, ::dfe, sizeof(dfe));
    static int tmp[N];
    for (int i = (int)(1); i <= (int)(n); i++) tmp[dfn[i]] = ::dis[i];
    tr.build(tmp, 1, n, 1);
  }
  void edit(int u, int v) {
    int c = dis[u] > dis[v] ? u : v;
    tr.flip(dfn[c], dfe[c], 1, n, 1);
  }
  int query() { return tr.dis[1][tr.flag[1]]; }
};
int main() {
  n = read();
  for (int i = (int)(1); i <= (int)(n - 1); i++) {
    u[i] = read();
    v[i] = read();
    t[i] = read();
    es[u[i]].push_back(v[i]);
    es[v[i]].push_back(u[i]);
  }
  dfs(1, -1);
  int a = max_element(dis + 1, dis + n + 1) - dis;
  dfs(a, -1);
  int b = max_element(dis + 1, dis + n + 1) - dis;
  static Solver s1;
  dfs(b, -1);
  static Solver s2;
  for (int i = (int)(1); i <= (int)(n - 1); i++)
    if (t[i]) {
      s1.edit(u[i], v[i]);
      s2.edit(u[i], v[i]);
    };
  int m = read();
  for (int _ = 0; _ < (int)(m); _++) {
    int id = read();
    s1.edit(u[id], v[id]);
    s2.edit(u[id], v[id]);
    int ans = max(s1.query(), s2.query());
    printf("%d\n", ans);
  }
  return 0;
}