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;
int inf;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T, class T2>
int chkmin(T &a, T2 b) {
  return a > b ? a = b, 1 : 0;
}
template <class T, class T2>
int chkmax(T &a, T2 b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T, class T2>
T mmin(T a, T2 b) {
  return a < b ? a : b;
}
template <class T, class T2>
T mmax(T a, T2 b) {
  return a > b ? a : b;
}
template <class T>
T aabs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
int dcmp(T a, T b) {
  return a > b;
}
template <int *a>
int cmp_a(int first, int second) {
  return a[first] < a[second];
}
template <class T>
bool sort2(T &a, T &b) {
  return a > b ? swap(a, b), 1 : 0;
}
struct __INIT__ {
  __INIT__() {
    fill((unsigned char *)&inf, (unsigned char *)&inf + sizeof(inf), 0x3f);
  }
} __INIT___;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char first) {
  *oS++ = first;
  if (oS == oT) flush();
}
template <typename A>
inline bool read(A &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(char &first) {
  while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                               (iS == iT ? EOF : *iS++))
                            : *iS++)) == ' ' ||
         first == '\n' || first == '\r')
    ;
  return first != EOF;
}
inline bool read(char *first) {
  while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                                (iS == iT ? EOF : *iS++))
                             : *iS++)) == '\n' ||
         *first == ' ' || *first == '\r')
    ;
  if (*first == EOF) return 0;
  while (!(*first == '\n' || *first == ' ' || *first == '\r' || *first == EOF))
    *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                           : *iS++);
  *first = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
  return read(first) && read(second...);
}
template <typename A>
inline bool write(A first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char first) {
  putc(first);
  return 0;
}
inline bool write(const char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
inline bool write(char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
  return write(first) || write(second...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
int n;
int u[500005], v[500005], uv[500005];
bitset<500005> c;
vector<int> to[500005];
bitset<500005> w, ww;
int dep[500005];
int mn[21][1000005], t;
int st[1000005];
int dfn[500005], low[500005], d;
int id[500005];
void dfs(int first, int f) {
  mn[0][st[first] = ++t] = dep[first];
  ww[dfn[first] = ++d] = w[first];
  id[d] = first;
  for (auto i : to[first])
    if (i != f) {
      if (v[i] == first) swap(u[i], v[i]);
      w[uv[i] ^ first] = w[first] ^ c[i];
      dep[uv[i] ^ first] = dep[first] + 1;
      mn[0][++t] = dep[first];
      dfs(uv[i] ^ first, i);
    }
  low[first] = d;
}
int lg[1000005];
void init() {
  for (int i = 2; i <= t; ++i) lg[i] = lg[i >> 1] + 1;
  for (int i = 1; (1 << i) <= t; ++i)
    for (int j = 1; j + (1 << i) - 1 <= t; ++j)
      mn[i][j] = mmin(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]);
}
int query(int l, int r) {
  if (l > r) swap(l, r);
  int h = lg[r - l + 1];
  return mmin(mn[h][l], mn[h][r - (1 << h) + 1]);
}
int dist(int first, int second) {
  return dep[first] + dep[second] - (query(st[first], st[second]) << 1);
}
struct xmx {
  int s, u, v;
  xmx() {}
  xmx(int s, int u, int v) : s(s), u(u), v(v) {}
  xmx(int u, int v) : u(u), v(v) { s = dist(u, v); }
  xmx(int u) : s(0), u(u), v(u) {}
  void merge(int first) {
    if (!~first) return;
    if (!~u) {
      u = v = first;
      s = 0;
      return;
    }
    int xu = u, xv = v;
    if (chkmax(s, dist(first, xu))) {
      u = xu;
      v = first;
    }
    if (chkmax(s, dist(first, xv))) {
      u = first;
      v = xv;
    }
  }
  void merge(xmx b) {
    if (b.u) merge(b.u);
    if (b.v != b.u) merge(b.v);
  }
};
xmx operator+(const xmx &a, const xmx &b) {
  xmx c = a;
  c.merge(b);
  return c;
}
struct smt {
  int ls, rs, rev;
  xmx mx0, mx1;
  smt *l, *r;
  smt() {}
  smt(int, int);
  void putrev() {
    rev ^= 1;
    swap(mx0, mx1);
  }
  void reverse(int la, int ra) {
    if (la <= ls && rs <= ra) {
      putrev();
      return;
    }
    if (rev) {
      l->putrev();
      r->putrev();
      rev = 0;
    }
    if (la <= l->rs) l->reverse(la, ra);
    if (r->ls <= ra) r->reverse(la, ra);
    mx0 = l->mx0 + r->mx0;
    mx1 = l->mx1 + r->mx1;
  }
};
smt *rt, node[1000005], *at = node;
smt *newsmt(int la, int ra) {
  smt *rt = ++at;
  *rt = smt(la, ra);
  return rt;
}
smt::smt(int la, int ra) {
  ls = la;
  rs = ra;
  rev = 0;
  if (ls == rs) {
    if (ww[ls])
      mx1 = id[ls], mx0 = -1;
    else
      mx0 = id[ls], mx1 = -1;
    l = r = 0;
    return;
  }
  int mid = (ls + rs) >> 1;
  l = newsmt(ls, mid);
  r = newsmt(mid + 1, rs);
  mx0 = l->mx0 + r->mx0;
  mx1 = l->mx1 + r->mx1;
}
signed main() {
  int q;
  read(n);
  int w = 0;
  for (int i = 1; i < n; ++i) {
    read(u[i], v[i], w);
    c[i] = w;
    uv[i] = u[i] ^ v[i];
    to[u[i]].emplace_back(i);
    to[v[i]].emplace_back(i);
  }
  dfs(1, 0);
  init();
  read(q);
  rt = newsmt(1, n);
  for (; q; --q) {
    int first;
    read(first);
    rt->reverse(dfn[v[first]], low[v[first]]);
    write(mmax(rt->mx0.s, rt->mx1.s), '\n');
  }
  return 0;
}