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;
void read(int &x) {
  static char c;
  int f = 0;
  while (!isdigit(c = getchar()))
    if (c == '-') f = 1;
  x = c ^ 48;
  while (isdigit(c = getchar())) x = (x * 10) + (c ^ 48);
  if (f) x = -x;
}
void read(long long &x) {
  static char c;
  int f = 0;
  while (!isdigit(c = getchar()))
    if (c == '-') f = 1;
  x = c ^ 48;
  while (isdigit(c = getchar())) x = (x * 10) + (c ^ 48);
}
void read(unsigned int &x) {
  static char c;
  while (!isdigit(c = getchar()))
    ;
  x = c ^ 48;
  while (isdigit(c = getchar())) x = (x * 10) + (c ^ 48);
}
void read(unsigned long long &x) {
  static char c;
  while (!isdigit(c = getchar()))
    ;
  x = c ^ 48;
  while (isdigit(c = getchar())) x = (x * 10) + (c ^ 48);
}
void chkmax(int &x, int y) {
  if (y > x) x = y;
}
void chkmin(int &x, int y) {
  if (y < x) x = y;
}
void chkmax(long long &x, long long y) {
  if (y > x) x = y;
}
void chkmin(long long &x, long long y) {
  if (y < x) x = y;
}
void chkmax(unsigned int &x, unsigned int y) {
  if (y > x) x = y;
}
void chkmin(unsigned int &x, unsigned int y) {
  if (y < x) x = y;
}
void chkmax(unsigned long long &x, unsigned long long y) {
  if (y > x) x = y;
}
void chkmin(unsigned long long &x, unsigned long long y) {
  if (y < x) x = y;
}
const int N = 5e5 + 10;
int n, q;
struct Link {
  int u, v, w;
} L[N];
struct Edge {
  int v, nxt, w;
} ed[N << 1];
int head[N], cnt;
void adde(int u, int v, int w) {
  ed[++cnt] = (Edge){v, head[u], w}, head[u] = cnt;
}
void add(int u, int v, int w) { adde(u, v, w), adde(v, u, w); }
int siz[N], dep[N], col[N];
int Dfn[N], seg[N], Dtot;
int Eul[N << 1], Beg[N], Etot;
void Build(int now, int fa) {
  siz[now] = 1, dep[now] = dep[fa] + 1;
  Dfn[now] = ++Dtot, seg[Dtot] = now, Beg[now] = ++Etot, Eul[Etot] = dep[now];
  for (int i = head[now]; i; i = ed[i].nxt) {
    int v = ed[i].v;
    if (v == fa) continue;
    col[v] = col[now] ^ ed[i].w;
    Build(v, now), siz[now] += siz[v];
    Eul[++Etot] = dep[now];
  }
}
int _log2[N << 1], ST[22][N << 1];
void Init() {
  const int _n = 2 * n - 1;
  for (int i = 2; i <= _n; ++i) _log2[i] = _log2[i >> 1] + 1;
  for (int i = 1; i <= _n; ++i) ST[0][i] = Eul[i];
  for (int B = 1; B <= _log2[_n]; ++B)
    for (int i = 1; i + (1 << B) - 1 <= _n; ++i)
      ST[B][i] = min(ST[B - 1][i], ST[B - 1][i + (1 << (B - 1))]);
}
int Getmin(int l, int r) {
  int k = _log2[r - l + 1];
  return min(ST[k][l], ST[k][r - (1 << k) + 1]);
}
int Dist(int u, int v) {
  if (Beg[u] > Beg[v]) swap(u, v);
  return dep[u] + dep[v] - 2 * Getmin(Beg[u], Beg[v]);
}
struct Node {
  pair<int, int> pr[2];
  int dis[2];
  int tag;
} t[N << 2];
void Pushdown(int now) {
  const int ls = now << 1, rs = now << 1 | 1;
  if (t[now].tag)
    swap(t[ls].pr[0], t[ls].pr[1]), swap(t[ls].dis[0], t[ls].dis[1]),
        swap(t[rs].pr[0], t[rs].pr[1]), swap(t[rs].dis[0], t[rs].dis[1]),
        t[ls].tag ^= 1, t[rs].tag ^= 1, t[now].tag = 0;
}
void Update(int now) {
  const int ls = now << 1, rs = now << 1 | 1;
  auto Check = [](int now, int C, int u, int v) -> void {
    if (!(u && v)) return;
    int dis = Dist(u, v);
    if (dis >= t[now].dis[C])
      t[now].pr[C] = make_pair(u, v), t[now].dis[C] = dis;
  };
  for (int C : {0, 1}) {
    t[now].pr[C] = make_pair(0, 0), t[now].dis[C] = 0;
    Check(now, C, t[ls].pr[C].first, t[ls].pr[C].second);
    Check(now, C, t[rs].pr[C].first, t[rs].pr[C].second);
    Check(now, C, t[ls].pr[C].first, t[rs].pr[C].first);
    Check(now, C, t[ls].pr[C].first, t[rs].pr[C].second);
    Check(now, C, t[ls].pr[C].second, t[rs].pr[C].first);
    Check(now, C, t[ls].pr[C].second, t[rs].pr[C].second);
  }
}
void Build(int now, int l, int r) {
  if (l == r) return t[now].pr[col[seg[l]]] = make_pair(seg[l], seg[l]), void();
  const int ls = now << 1, rs = now << 1 | 1, mid = (l + r) >> 1;
  Build(ls, l, mid), Build(rs, mid + 1, r), Update(now);
}
void Modify(int now, int l, int r, int x, int y) {
  if (l >= x && r <= y) {
    swap(t[now].pr[0], t[now].pr[1]);
    swap(t[now].dis[0], t[now].dis[1]);
    t[now].tag ^= 1;
    return;
  }
  Pushdown(now);
  const int ls = now << 1, rs = now << 1 | 1, mid = (l + r) >> 1;
  if (mid >= x) Modify(ls, l, mid, x, y);
  if (mid < y) Modify(rs, mid + 1, r, x, y);
  Update(now);
}
void Work() {
  read(n);
  for (int i = 1; i < n; ++i)
    read(L[i].u), read(L[i].v), read(L[i].w), add(L[i].u, L[i].v, L[i].w);
  Build(1, 0), Init(), Build(1, 1, n);
  for (int i = 1; i < n; ++i)
    if (Dfn[L[i].u] < Dfn[L[i].v]) swap(L[i].u, L[i].v);
  read(q);
  while (q--) {
    int id;
    read(id);
    Modify(1, 1, n, Dfn[L[id].u], Dfn[L[id].u] + siz[L[id].u] - 1);
    printf("%d\n", max(t[1].dis[0], t[1].dis[1]));
  }
}
int main() { Work(); }