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>
template <class T, class U>
inline bool smin(T &x, const U &y) {
  return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
  return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
struct Info {
  int c, len, d[2][2], r[2];
  Info() : c(0), len(0) {
    d[0][0] = 0, d[0][1] = -1e9;
    d[1][0] = 0, d[1][1] = -1e9;
    r[0] = r[1] = -1e9;
  }
  void reverse() {
    std::swap(d[0][0], d[1][0]);
    std::swap(d[0][1], d[1][1]);
  }
  Info &compress(Info a, Info b) {
    c = a.c ^ b.c;
    len = a.len + b.len;
    d[0][0] = std::max(a.d[0][0], a.len + b.d[0][a.c]);
    d[0][1] = std::max(a.d[0][1], a.len + b.d[0][!a.c]);
    d[1][0] = std::max(b.d[1][0], b.len + a.d[1][b.c]);
    d[1][1] = std::max(b.d[1][1], b.len + a.d[1][!b.c]);
    r[0] = std::max(
        {a.r[0], b.r[0], a.d[1][0] + b.d[0][0], a.d[1][1] + b.d[0][1]});
    r[1] = std::max(
        {a.r[1], b.r[1], a.d[1][0] + b.d[0][1], a.d[1][1] + b.d[0][0]});
    return *this;
  }
  Info &rakeToPath(Info a, Info b) {
    c = b.c;
    len = b.len;
    d[0][0] = std::max(a.d[0][0], b.d[0][0]);
    d[0][1] = std::max(a.d[0][1], b.d[0][1]);
    d[1][0] = std::max(b.d[1][0], b.len + a.d[0][b.c]);
    d[1][1] = std::max(b.d[1][1], b.len + a.d[0][!b.c]);
    r[0] = std::max(
        {a.r[0], b.r[0], a.d[0][0] + b.d[0][0], a.d[0][1] + b.d[0][1]});
    r[1] = std::max(
        {a.r[1], b.r[1], a.d[0][0] + b.d[0][1], a.d[0][1] + b.d[0][0]});
    return *this;
  }
  Info &rake(Info a, Info b) {
    d[0][0] = std::max(a.d[0][0], b.d[0][0]);
    d[0][1] = std::max(a.d[0][1], b.d[0][1]);
    r[0] = std::max(
        {a.r[0], b.r[0], a.d[0][0] + b.d[0][0], a.d[0][1] + b.d[0][1]});
    r[1] = std::max(
        {a.r[1], b.r[1], a.d[0][0] + b.d[0][1], a.d[0][1] + b.d[0][0]});
    return *this;
  }
};
struct SplayTree {
  static SplayTree *null;
  SplayTree *ch[3], *fa;
  bool rev;
  Info info;
  SplayTree() {
    static bool _init = true;
    if (_init) {
      _init = false;
      null = new SplayTree;
      null->ch[0] = null->ch[1] = null->ch[2] = null->fa = null;
    }
    ch[0] = ch[1] = ch[2] = fa = null;
  }
  bool notRoot() const { return fa->ch[0] == this || fa->ch[1] == this; }
  void reverse() {
    rev ^= 1;
    std::swap(ch[0], ch[1]);
    info.reverse();
  }
  bool dir() const { return fa->ch[1] == this; }
  SplayTree *sch(int d, SplayTree *c) { return ch[d] = c, c->fa = this; }
  void rotate() {
    SplayTree *p = fa;
    p->pushdown(), pushdown();
    bool d = dir();
    fa = p->fa;
    if (fa != null) {
      fa->ch[fa->ch[2] == p ? 2 : p->dir()] = this;
    }
    p->sch(d, ch[!d])->pushup();
    sch(!d, p)->pushup();
  }
  SplayTree *splay(SplayTree *p = null) {
    for (pushdown(); fa != p && notRoot(); rotate()) {
      if (fa->fa != p && fa->notRoot()) {
        fa->fa->pushdown();
        (fa->dir() == dir() ? fa : this)->rotate();
      }
    }
    return this;
  }
  SplayTree *splayExt(int d) {
    SplayTree *o = this;
    while (o->pushdown(), o->ch[d] != null) o = o->ch[d];
    return o->splay(fa);
  }
  virtual void pushup() {}
  virtual void pushdown() {}
};
SplayTree *SplayTree::null;
struct CompressTree : SplayTree {
  void pushup() {
    info.compress(ch[0]->info, Info().rakeToPath(ch[2]->info, ch[1]->info));
  }
  void pushdown() {
    if (rev) {
      rev = false;
      ch[0]->reverse(), ch[1]->reverse();
    }
  }
};
struct RakeTree : SplayTree {
  void pushup() {
    info.rake(ch[0]->info, Info().rake(ch[2]->info, ch[1]->info));
  }
};
void splice(SplayTree *o) {
  o->splay();
  o = o->fa->splay();
  SplayTree *c = o->ch[2];
  c->pushdown();
  if (o->ch[1] != SplayTree::null) {
    std::swap(o->ch[1]->fa, c->ch[2]->fa);
    std::swap(o->ch[1], c->ch[2]);
  } else {
    o->sch(1, c->ch[2]);
    if (c->ch[0] != SplayTree::null) {
      c->ch[0]->splayExt(1);
      c->ch[0]->sch(1, c->ch[1]);
      o->ch[2] = c->ch[0];
    } else {
      c->ch[1]->pushdown();
      o->ch[2] = c->ch[1];
    }
    c = o->ch[2], c->fa = o;
  }
  c->pushup(), o->pushup();
  o->ch[1]->rotate();
}
void access(SplayTree *o) {
  o->splay();
  if (o->ch[1] != SplayTree::null) {
    SplayTree *c = new RakeTree;
    c->sch(0, o->ch[2]);
    c->sch(2, o->ch[1]);
    c->pushup();
    o->ch[1] = SplayTree::null;
    o->sch(2, c);
    o->pushup();
  }
  while (o->fa != SplayTree::null) splice(o->fa);
}
void evert(SplayTree *o) { access(o), o->reverse(), o->pushdown(); }
void link(SplayTree *x, SplayTree *y, int c) {
  access(x);
  evert(y);
  y->pushdown();
  SplayTree *e = new CompressTree;
  e->info.len = 1, e->info.c = c;
  y->sch(0, e)->pushup();
  x->sch(1, y)->pushup();
}
constexpr int N(3e5 + 5);
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, m;
  std::cin >> n;
  SplayTree *t = new CompressTree[n + 1];
  assert(SplayTree::null);
  std::vector<int> u(n - 1), v(n - 1);
  for (int i = 0, c; i < n - 1; i++) {
    std::cin >> u[i] >> v[i] >> c;
    link(t + u[i], t + v[i], c);
  }
  std::cin >> m;
  while (m--) {
    int i;
    std::cin >> i;
    i--;
    evert(t + u[i]), access(t + v[i]);
    t[v[i]].pushdown();
    t[u[i]].pushdown();
    t[u[i]].ch[1]->info.c ^= 1;
    t[u[i]].pushup();
    t[v[i]].pushup();
    std::cout << t[v[i]].info.r[0] << "\n";
  }
  return 0;
}