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;
namespace io {
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE + 1], *is = ibuf, *it = ibuf;
char obuf[BUFSIZE + 1], *os = obuf, *ot = obuf + BUFSIZE;
inline char read_char() {
  if (is == it) {
    it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
    if (is == it) *it++ = EOF;
  }
  return *is++;
}
template <class T>
inline void read_int(T &x) {
  T f = 1;
  char c = read_char();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = read_char();
  }
  x = 0;
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = read_char();
  }
  x *= f;
}
inline void read_str(char *s) {
  char c = read_char();
  while (c <= ' ') {
    c = read_char();
  }
  while (c > ' ') {
    *s++ = c;
    c = read_char();
  }
  *s = 0;
}
inline void flush() {
  fwrite(obuf, 1, os - obuf, stdout);
  os = obuf;
}
inline void print_char(char c) {
  *os++ = c;
  if (os == ot) {
    flush();
  }
}
template <class T>
inline void print_int(T x, char c = 0) {
  static char q[40];
  if (!x) {
    print_char('0');
  } else {
    if (x < 0) {
      print_char('-');
      x = -x;
    }
    int top = 0;
    while (x) {
      q[top++] = x % 10 + '0';
      x /= 10;
    }
    while (top--) {
      print_char(q[top]);
    }
  }
  if (c) print_char(c);
}
inline void print_str(char *s, char c = 0) {
  while (*s) {
    print_char(*s++);
  }
  if (c) print_char(c);
}
struct flusher_t {
  inline ~flusher_t() { flush(); }
} flusher;
}  // namespace io
using io::print_char;
using io::print_int;
using io::print_str;
using io::read_char;
using io::read_int;
using io::read_str;
const int N = 500005;
int n, Q, tot, dfn[N], dl[N], pp[N], dr[N], dfc, st[20][N * 2], lg2[N * 2],
    dep[N], dist[N];
vector<pair<int, int> > G[N];
inline int LCA(int l, int r) {
  l = dfn[l], r = dfn[r];
  if (l > r) swap(l, r);
  int i = lg2[r - l + 1];
  if (dep[st[i][l]] < dep[st[i][r - (1 << i) + 1]])
    return st[i][l];
  else
    return st[i][r - (1 << i) + 1];
}
inline int getdist(int u, int v) {
  return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
}
struct Node {
  int u, v, d;
};
inline Node Merge(const Node &a, const Node &b) {
  if (a.d == -1) return b;
  if (b.d == -1) return a;
  Node ret;
  if (a.d > b.d) {
    ret = a;
  } else {
    ret = b;
  }
  int d1 = getdist(a.u, b.u), d2 = getdist(a.u, b.v), d3 = getdist(a.v, b.u),
      d4 = getdist(a.v, b.v);
  int md = max(max(d1, d2), max(d3, d4));
  if (md <= ret.d) return ret;
  ret.d = md;
  if (d1 == md) {
    ret.u = a.u;
    ret.v = b.u;
  } else if (d2 == md) {
    ret.u = a.u;
    ret.v = b.v;
  } else if (d3 == md) {
    ret.u = a.v;
    ret.v = b.u;
  } else {
    ret.u = a.v;
    ret.v = b.v;
  }
  return ret;
}
Node tr[N * 4][2];
int tag[N * 4];
void build(int i, int l, int r) {
  tag[i] = 0;
  if (l == r) {
    int u = pp[l];
    tr[i][dist[u]].u = tr[i][dist[u]].v = u;
    tr[i][dist[u]].d = 0;
    tr[i][dist[u] ^ 1].u = tr[i][dist[u] ^ 1].v = 0;
    tr[i][dist[u] ^ 1].d = -1;
    return;
  }
  int mid = (l + r) >> 1;
  build(i << 1, l, mid);
  build(i << 1 | 1, mid + 1, r);
  tr[i][0] = Merge(tr[i << 1][0], tr[i << 1 | 1][0]);
  tr[i][1] = Merge(tr[i << 1][1], tr[i << 1 | 1][1]);
}
inline void pushdown(int i) {
  if (tag[i]) {
    swap(tr[i << 1][0], tr[i << 1][1]);
    swap(tr[i << 1 | 1][0], tr[i << 1 | 1][1]);
    tag[i << 1] ^= 1;
    tag[i << 1 | 1] ^= 1;
    tag[i] = 0;
  }
}
void modify(int i, int l, int r, int lf, int rg) {
  if (lf <= l && r <= rg) {
    swap(tr[i][0], tr[i][1]);
    tag[i] ^= 1;
    return;
  }
  pushdown(i);
  int mid = (l + r) >> 1;
  if (lf <= mid) modify(i << 1, l, mid, lf, rg);
  if (rg > mid) modify(i << 1 | 1, mid + 1, r, lf, rg);
  tr[i][0] = Merge(tr[i << 1][0], tr[i << 1 | 1][0]);
  tr[i][1] = Merge(tr[i << 1][1], tr[i << 1 | 1][1]);
}
void dfs(int u, int lst, int depth, int D) {
  dl[u] = ++dfc;
  pp[dfc] = u;
  st[0][dfn[u] = ++tot] = u;
  dep[u] = depth;
  dist[u] = D;
  for (auto &e : G[u]) {
    int v = e.first;
    if (v == lst) continue;
    dfs(v, u, depth + 1, D ^ e.second);
    st[0][++tot] = u;
  }
  dr[u] = dfc;
}
int eu[N], ev[N];
int main() {
  read_int(n);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    read_int(u);
    eu[i] = u;
    read_int(v);
    ev[i] = v;
    read_int(w);
    G[u].push_back(make_pair(v, w));
    G[v].push_back(make_pair(u, w));
  }
  tot = 0;
  dfs(1, 0, 0, 0);
  lg2[0] = -1;
  for (int i = 1; i <= tot; i++) lg2[i] = lg2[i - 1] + ((i & (i - 1)) ? 0 : 1);
  for (int i = 1; i < 20; i++) {
    for (int j = 1; j <= tot - (1 << i) + 1; j++) {
      if (dep[st[i - 1][j]] < dep[st[i - 1][j + (1 << (i - 1))]])
        st[i][j] = st[i - 1][j];
      else
        st[i][j] = st[i - 1][j + (1 << (i - 1))];
    }
  }
  build(1, 1, n);
  read_int(Q);
  for (int i = 1; i < n; i++) {
    if (dl[eu[i]] > dl[ev[i]]) swap(eu[i], ev[i]);
  }
  while (Q--) {
    int eid;
    read_int(eid);
    modify(1, 1, n, dl[ev[eid]], dr[ev[eid]]);
    print_int(max(tr[1][0].d, tr[1][1].d), '\n');
  }
  return 0;
}