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>
struct IO_Tp {
  const static int _I_Buffer_Size = 20 << 20;
  char _I_Buffer[_I_Buffer_Size], *_I_pos = _I_Buffer;
  const static int _O_Buffer_Size = 20 << 20;
  char _O_Buffer[_O_Buffer_Size], *_O_pos = _O_Buffer;
  IO_Tp() { fread(_I_Buffer, 1, _I_Buffer_Size, stdin); }
  ~IO_Tp() { fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout); }
  IO_Tp &operator>>(int &res) {
    res = 0;
    while (!isdigit(*_I_pos)) ++_I_pos;
    do res = res * 10 + (*_I_pos++ & 15);
    while (isdigit(*_I_pos));
    return *this;
  }
  char getop() {
    while (!isdigit(*_I_pos)) ++_I_pos;
    return *_I_pos++ & 15;
  }
  IO_Tp &operator<<(int n) {
    static char _buf[10];
    char *_pos = _buf;
    do *_pos++ = '0' + n % 10;
    while (n /= 10);
    while (_pos != _buf) *_O_pos++ = *--_pos;
    return *this;
  }
  IO_Tp &operator<<(char ch) {
    *_O_pos++ = ch;
    return *this;
  }
} IO;
struct agg_Tp {
  int length, dist[2][2], res[2];
  int px;
  agg_Tp() {
    px = 0;
    length = dist[0][0] = dist[1][0] = 0;
    dist[0][1] = dist[1][1] = res[0] = res[1] = -(1 << 30);
  }
  void reverse() {
    std::swap(dist[0][0], dist[1][0]), std::swap(dist[0][1], dist[1][1]);
  }
  agg_Tp &compress(const agg_Tp &a, const agg_Tp &b) {
    length = a.length + b.length;
    px = a.px ^ b.px;
    dist[0][0] = std::max(a.dist[0][0], a.length + b.dist[0][a.px]);
    dist[0][1] = std::max(a.dist[0][1], a.length + b.dist[0][!a.px]);
    dist[1][0] = std::max(b.dist[1][0], b.length + a.dist[1][b.px]);
    dist[1][1] = std::max(b.dist[1][1], b.length + a.dist[1][!b.px]);
    res[0] = std::max(
        std::max(a.res[0], b.res[0]),
        std::max(a.dist[1][0] + b.dist[0][0], a.dist[1][1] + b.dist[0][1]));
    res[1] = std::max(
        std::max(a.res[1], b.res[1]),
        std::max(a.dist[1][0] + b.dist[0][1], a.dist[1][1] + b.dist[0][0]));
    return *this;
  }
  agg_Tp &rake_to_path(const agg_Tp &a, const agg_Tp &b) {
    length = b.length;
    px = b.px;
    dist[0][0] = std::max(a.dist[0][0], b.dist[0][0]);
    dist[0][1] = std::max(a.dist[0][1], b.dist[0][1]);
    dist[1][0] = std::max(b.dist[1][0], b.length + a.dist[0][b.px]);
    dist[1][1] = std::max(b.dist[1][1], b.length + a.dist[0][!b.px]);
    res[0] = std::max(
        std::max(a.res[0], b.res[0]),
        std::max(a.dist[0][0] + b.dist[0][0], a.dist[0][1] + b.dist[0][1]));
    res[1] = std::max(
        std::max(a.res[1], b.res[1]),
        std::max(a.dist[0][0] + b.dist[0][1], a.dist[0][1] + b.dist[0][0]));
    return *this;
  }
  agg_Tp &rake(const agg_Tp &a, const agg_Tp &b) {
    dist[0][0] = std::max(a.dist[0][0], b.dist[0][0]);
    dist[0][1] = std::max(a.dist[0][1], b.dist[0][1]);
    res[0] = std::max(
        std::max(a.res[0], b.res[0]),
        std::max(a.dist[0][0] + b.dist[0][0], a.dist[0][1] + b.dist[0][1]));
    res[1] = std::max(
        std::max(a.res[1], b.res[1]),
        std::max(a.dist[0][0] + b.dist[0][1], a.dist[0][1] + b.dist[0][0]));
    return *this;
  }
};
const bool compress = false, rake = true;
struct Splay_Tree {
  static Splay_Tree *Null;
  void *operator new(size_t size);
  void *operator new[](size_t size);
  void operator delete(void *ptr);
  Splay_Tree *ch[3], *parent;
  bool rev;
  agg_Tp agg;
  Splay_Tree() : rev(false) {
    static bool _init = false;
    if (!_init) {
      _init = true;
      Null = new Splay_Tree;
      Null->ch[0] = Null->ch[1] = Null->ch[2] = Null->parent = Null;
    }
    ch[0] = ch[1] = ch[2] = parent = Null;
  }
  bool is_root() const {
    return parent->ch[0] != this && parent->ch[1] != this;
  }
  template <const bool type>
  void pushup();
  template <const bool type>
  void pushdown();
  void reverse() {
    rev ^= 1, std::swap(ch[0], ch[1]);
    agg.reverse();
  }
  bool dir() const { return parent->ch[1] == this; }
  Splay_Tree *sch(const int d, Splay_Tree *c) {
    return ch[d] = c, c->parent = this;
  }
  template <const bool type>
  void rotate() {
    Splay_Tree *p = parent;
    p->pushdown<type>(), pushdown<type>();
    bool d = dir();
    if (parent = p->parent, p->parent != Splay_Tree::Null) {
      if (p->parent->ch[2] == p)
        p->parent->ch[2] = this;
      else
        p->parent->ch[p->dir()] = this;
    }
    p->sch(d, ch[!d])->pushup<type>(), sch(!d, p)->pushup<type>();
  }
  template <const bool type>
  Splay_Tree *splay(Splay_Tree *p = Null) {
    for (pushdown<type>(); parent != p && !is_root(); rotate<type>())
      if (parent->parent != p && !parent->is_root())
        parent->parent->pushdown<type>(),
            (parent->dir() == dir() ? parent : this)->rotate<type>();
    return this;
  }
  template <const bool type>
  Splay_Tree *splay_m(const bool d) {
    Splay_Tree *o = this;
    while (o->pushdown<type>(), o->ch[d] != Splay_Tree::Null) o = o->ch[d];
    return o->splay<type>(parent);
  }
};
Splay_Tree *Splay_Tree::Null;
const int Max_N = 500005;
const int _M_size = Max_N * 3;
char _M[_M_size * sizeof(Splay_Tree)], *_M_cur = _M + sizeof(_M);
void *_M_rc[_M_size], **_M_rc_cur = _M_rc;
inline void *Splay_Tree::operator new(size_t size) {
  return _M_rc_cur != _M_rc ? *--_M_rc_cur : _M_cur -= size;
}
inline void *Splay_Tree::operator new[](size_t size) { return _M_cur -= size; }
inline void Splay_Tree::operator delete(void *ptr) { *_M_rc_cur++ = ptr; }
template <>
inline void Splay_Tree::pushup<compress>() {
  agg.compress(ch[0]->agg, agg_Tp().rake_to_path(ch[2]->agg, ch[1]->agg));
}
template <>
inline void Splay_Tree::pushup<rake>() {
  agg.rake(ch[0]->agg, agg_Tp().rake(ch[2]->agg, ch[1]->agg));
}
template <>
inline void Splay_Tree::pushdown<compress>() {
  if (rev) ch[0]->reverse(), ch[1]->reverse(), rev = false;
}
template <>
inline void Splay_Tree::pushdown<rake>() {}
void splay_splice(Splay_Tree *o) {
  o->splay<rake>();
  o = o->parent->splay<compress>();
  Splay_Tree *c = o->ch[2];
  c->pushdown<rake>();
  if (o->ch[1] != Splay_Tree::Null)
    std::swap(o->ch[1]->parent, c->ch[2]->parent),
        std::swap(o->ch[1], c->ch[2]);
  else {
    o->sch(1, c->ch[2]);
    if (c->ch[0] != Splay_Tree::Null)
      c->ch[0]->splay_m<rake>(1), c->ch[0]->sch(1, c->ch[1]),
          o->ch[2] = c->ch[0];
    else
      c->ch[1]->pushdown<rake>(), o->ch[2] = c->ch[1];
    delete c, c = o->ch[2], c->parent = o;
  }
  c->pushup<rake>(), o->pushup<compress>(), o->ch[1]->rotate<compress>();
}
void access(Splay_Tree *o) {
  o->splay<compress>();
  if (o->ch[1] != Splay_Tree::Null) {
    Splay_Tree *c = new Splay_Tree;
    c->sch(0, o->ch[2]), c->sch(2, o->ch[1]), c->pushup<rake>();
    o->ch[1] = Splay_Tree::Null, o->sch(2, c), o->pushup<compress>();
  }
  while (o->parent != Splay_Tree::Null) splay_splice(o->parent);
}
inline void evert(Splay_Tree *o) {
  access(o), o->reverse(), o->pushdown<compress>();
}
inline void link(Splay_Tree *o1, Splay_Tree *o2, const int c) {
  access(o1), evert(o2), o2->pushdown<compress>();
  Splay_Tree *e = new Splay_Tree;
  e->agg.length = 1, e->agg.px = c;
  o2->sch(0, e)->pushup<compress>(), o1->sch(1, o2)->pushup<compress>();
}
int N, M;
int u[Max_N], v[Max_N];
Splay_Tree *node0;
int main(int argc, char **argv) {
  IO >> N;
  node0 = new Splay_Tree[N + 1];
  for (int i = 1; i != N; ++i) {
    int c;
    IO >> u[i] >> v[i] >> c;
    link((node0 + u[i]), (node0 + v[i]), c);
  }
  IO >> M;
  while (M--) {
    int e;
    IO >> e;
    evert((node0 + u[e])), access((node0 + v[e]));
    (node0 + v[e])->pushdown<compress>(),
        (node0 + v[e])->ch[0]->pushdown<compress>();
    (node0 + v[e])->ch[0]->ch[1]->agg.px ^= 1;
    (node0 + v[e])->ch[0]->pushup<compress>(),
        (node0 + v[e])->pushup<compress>();
    IO << (node0 + v[e])->agg.res[0] << '\n';
  }
  return 0;
}