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>
inline void err() { std::cerr << "\033[0m\n"; }
template <class T, class... U>
inline void err(const T &x, const U &...a) {
  std::cerr << x << ' ';
  err(a...);
}
template <class T>
inline void readInt(T &w) {
  char c, p = 0;
  while (!isdigit(c = getchar())) p = c == '-';
  for (w = c & 15; isdigit(c = getchar());) w = w * 10 + (c & 15);
  if (p) w = -w;
}
template <class T, class... U>
inline void readInt(T &w, U &...a) {
  readInt(w), readInt(a...);
}
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 Matrix = std::array<std::array<int, 4>, 4>;
Matrix operator*(const Matrix &a, const Matrix &b) {
  Matrix r;
  r[0][0] = std::max({a[0][0] + b[0][0], a[0][1] + b[1][0], a[0][2] + b[2][0],
                      a[0][3] + b[3][0]});
  r[0][1] = std::max({a[0][0] + b[0][1], a[0][1] + b[1][1], a[0][2] + b[2][1],
                      a[0][3] + b[3][1]});
  r[0][2] = std::max({a[0][0] + b[0][2], a[0][1] + b[1][2], a[0][2] + b[2][2],
                      a[0][3] + b[3][2]});
  r[0][3] = std::max({a[0][0] + b[0][3], a[0][1] + b[1][3], a[0][2] + b[2][3],
                      a[0][3] + b[3][3]});
  r[1][0] = std::max({a[1][0] + b[0][0], a[1][1] + b[1][0], a[1][2] + b[2][0],
                      a[1][3] + b[3][0]});
  r[1][1] = std::max({a[1][0] + b[0][1], a[1][1] + b[1][1], a[1][2] + b[2][1],
                      a[1][3] + b[3][1]});
  r[1][2] = std::max({a[1][0] + b[0][2], a[1][1] + b[1][2], a[1][2] + b[2][2],
                      a[1][3] + b[3][2]});
  r[1][3] = std::max({a[1][0] + b[0][3], a[1][1] + b[1][3], a[1][2] + b[2][3],
                      a[1][3] + b[3][3]});
  r[2][0] = std::max({a[2][0] + b[0][0], a[2][1] + b[1][0], a[2][2] + b[2][0],
                      a[2][3] + b[3][0]});
  r[2][1] = std::max({a[2][0] + b[0][1], a[2][1] + b[1][1], a[2][2] + b[2][1],
                      a[2][3] + b[3][1]});
  r[2][2] = std::max({a[2][0] + b[0][2], a[2][1] + b[1][2], a[2][2] + b[2][2],
                      a[2][3] + b[3][2]});
  r[2][3] = std::max({a[2][0] + b[0][3], a[2][1] + b[1][3], a[2][2] + b[2][3],
                      a[2][3] + b[3][3]});
  r[3][0] = std::max({a[3][0] + b[0][0], a[3][1] + b[1][0], a[3][2] + b[2][0],
                      a[3][3] + b[3][0]});
  r[3][1] = std::max({a[3][0] + b[0][1], a[3][1] + b[1][1], a[3][2] + b[2][1],
                      a[3][3] + b[3][1]});
  r[3][2] = std::max({a[3][0] + b[0][2], a[3][1] + b[1][2], a[3][2] + b[2][2],
                      a[3][3] + b[3][2]});
  r[3][3] = std::max({a[3][0] + b[0][3], a[3][1] + b[1][3], a[3][2] + b[2][3],
                      a[3][3] + b[3][3]});
  return r;
}
constexpr int N(5e5 + 5), INF(1e9);
int n, m, val[N], fa[N], son[N], siz[N];
std::vector<int> g[N];
void dfs1(int x) {
  siz[x] = 1;
  for (int y : g[x]) {
    if (y == fa[x]) continue;
    fa[y] = x;
    dfs1(y);
    siz[x] += siz[y];
    if (siz[y] > siz[son[x]]) son[x] = y;
  }
}
struct Node {
  Node *ls, *rs, *fa;
  Matrix val, sum;
  inline void pushup() {
    sum = ls ? ls->sum * val : val;
    if (rs) sum = sum * rs->sum;
  }
} t[N];
std::multiset<int> vir[N][3];
void updateVal(int x) {
  auto &v = t[x].val;
  auto p0 = vir[x][0].rbegin(), p1 = vir[x][1].rbegin();
  int k = val[son[x]], m0 = *p0, m1 = *p1;
  v[0][k] = 1, v[0][!k] = v[0][2] = -INF, v[0][3] = m0;
  v[1][!k] = 1, v[1][k] = v[1][2] = -INF, v[1][3] = m1;
  v[2][k] = m0 + 1, v[2][!k] = m1 + 1, v[2][2] = 0;
  v[2][3] = vir[x][0].size() > 1 ? std::max(m0 + *++p0, m1 + *++p1) : 0;
  smax(v[2][3], *vir[x][2].rbegin());
  v[3][0] = v[3][1] = v[3][2] = -INF, v[3][3] = 0;
}
int f[N][3];
void dfs2(int x) {
  f[x][0] = f[x][2] = 0, f[x][1] = -INF;
  vir[x][0].insert(0), vir[x][1].insert(-INF), vir[x][2].insert(0);
  for (int y : g[x]) {
    if (y == fa[x]) continue;
    dfs2(y);
    smax(f[x][2], f[y][2]);
    smax(f[x][2], f[x][0] + f[y][val[y]] + 1);
    smax(f[x][2], f[x][1] + f[y][!val[y]] + 1);
    smax(f[x][0], f[y][val[y]] + 1);
    smax(f[x][1], f[y][!val[y]] + 1);
    if (y == son[x]) continue;
    vir[x][0].insert(f[y][val[y]] + 1);
    vir[x][1].insert(f[y][!val[y]] + 1);
    vir[x][2].insert(f[y][2]);
  }
  updateVal(x);
}
int top[N], s[N], sum[N];
Node *build(int l, int r) {
  if (l == r) return t[s[l]].pushup(), t + s[l];
  int m = l, tot = sum[r] + sum[l - 1];
  while (m < r && sum[m] << 1 < tot) m++;
  Node *o = t + s[m];
  if (l < m) o->ls = build(l, m - 1), o->ls->fa = o;
  if (m < r) o->rs = build(m + 1, r), o->rs->fa = o;
  o->pushup();
  return o;
}
Node *root;
void dfs3(int x, int tp) {
  top[x] = tp;
  if (x == tp) {
    int m = 0;
    for (int i = x; i; i = son[i]) {
      s[++m] = i;
      sum[m] = sum[m - 1] + siz[i] - siz[son[i]];
    }
    if (x > 1)
      build(1, m)->fa = t + fa[x];
    else
      root = build(1, m);
  }
  if (!son[x]) return;
  dfs3(son[x], tp);
  for (int y : g[x]) {
    if (y == fa[x] || y == son[x]) continue;
    dfs3(y, y);
  }
}
inline void erase(std::multiset<int> &s, int x) {
  if (x < 0)
    assert(*s.begin() < 0), s.erase(s.begin());
  else
    s.erase(s.find(x));
}
int maxc;
void work(int x, int y) {
  int c = 0;
  assert(fa[y] == x);
  Node *o = t + x;
  int &v = val[y];
  if (son[x] == y) {
    v ^= 1;
  } else {
    Node *p = t + y;
    while (p->fa != o) p = p->fa;
    erase(vir[x][0], p->sum[v][3] + 1);
    erase(vir[x][1], p->sum[!v][3] + 1);
    v ^= 1;
    vir[x][0].insert(p->sum[v][3] + 1);
    vir[x][1].insert(p->sum[!v][3] + 1);
  }
  for (updateVal(x); o->fa; o = o->fa) {
    if (o->fa->ls != o && o->fa->rs != o) {
      x = o->fa - t, y = val[top[o - t]];
      assert(x == fa[top[o - t]]);
      erase(vir[x][0], o->sum[y][3] + 1);
      erase(vir[x][1], o->sum[!y][3] + 1);
      erase(vir[x][2], o->sum[2][3]);
      o->pushup();
      vir[x][0].insert(o->sum[y][3] + 1);
      vir[x][1].insert(o->sum[!y][3] + 1);
      vir[x][2].insert(o->sum[2][3]);
      updateVal(x);
    } else {
      o->pushup();
    }
    c++;
  }
  o->pushup();
  smax(maxc, c);
  assert(c <= 50);
}
struct Edge {
  int x, y, z;
} e[N];
int main() {
  readInt(n);
  double T = (double)clock() / CLOCKS_PER_SEC;
  for (int i = 1; i < n; i++) {
    auto &[x, y, z] = e[i];
    readInt(x, y, z);
    g[x].push_back(y), g[y].push_back(x);
  }
  dfs1(1);
  for (int i = 1; i < n; i++) {
    auto &[x, y, z] = e[i];
    if (fa[y] != x) std::swap(x, y);
    assert(fa[y] == x);
    val[y] = z;
  }
  dfs2(1);
  dfs3(1, 1);
  readInt(m);
  while (m--) {
    int i;
    readInt(i);
    work(e[i].x, e[i].y);
    printf("%d\n", root->sum[2][3]);
  }
  T = (double)clock() / CLOCKS_PER_SEC - T;
  ;
  return 0;
}