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;
const long long MOD = 1e9 + 7;
template <class T>
void chmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
  if (a < b) a = b;
}
struct Edge {
  int to, id;
  long long dist;
  Edge(int to, long long dist = 1, int id = 0) : to(to), dist(dist), id(id) {}
};
struct HeavyLightDecomposition {
  vector<vector<Edge>> g;
  vector<int> in, out, head, par, dep, sz;
  int times;
  int root;
  HeavyLightDecomposition(int V, vector<vector<Edge>> &G, int root = 0)
      : g(G), in(V), out(V), head(V), par(V), dep(V), sz(V), root(root) {
    times = 0;
    sz_dfs(root, -1);
    hld_dfs(root, -1);
  }
  void sz_dfs(int now, int p) {
    par[now] = p;
    sz[now] = 1;
    if (p == -1)
      dep[now] = 0;
    else
      dep[now] = dep[p] + 1;
    for (auto &e : g[now]) {
      if (e.to == p) continue;
      sz_dfs(e.to, now);
      sz[now] += sz[e.to];
      if (sz[e.to] > sz[g[now][0].to]) swap(e, g[now][0]);
    }
  }
  void hld_dfs(int now, int p) {
    in[now] = times++;
    for (auto e : g[now]) {
      if (e.to == p) continue;
      head[e.to] = (e.to == g[now][0].to ? head[now] : e.to);
      hld_dfs(e.to, now);
    }
    out[now] = times;
  }
  int lca(int u, int v) {
    for (;; v = par[head[v]]) {
      if (in[u] > in[v]) swap(u, v);
      if (head[u] == head[v]) return u;
    }
  }
  int distance(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
  template <class T, class G>
  void update(int v, const T &x, const G &g) {
    g(in[v], out[v], x);
  }
  template <class F>
  void query(int u, int v, const F &f, bool isedge) {
    for (;; v = par[head[v]]) {
      if (in[u] > in[v]) swap(u, v);
      if (head[u] == head[v]) break;
      f(in[head[v]], in[v] + 1);
    }
    if (isedge && u == v) return;
    f(in[u] + isedge, in[v] + 1);
  }
};
template <typename T, typename S>
struct LazySegmentTree {
  int n;
  vector<T> data;
  vector<S> lazy;
  T te;
  S se;
  inline void merge_functions(S &lazy, S &val) { lazy ^= val; }
  inline void operate(T &data, S &val, int len) {
    swap(data.first, data.second);
  }
  inline T merge_values(T &x, T &y) {
    return make_pair(max(x.first, y.first), max(x.second, y.second));
  }
  LazySegmentTree() {}
  LazySegmentTree(int sz, T te, S se) : te(te), se(se) {
    n = 1;
    while (n < sz) n *= 2;
    data.resize(2 * n - 1, te);
    lazy.resize(2 * n - 1, se);
  }
  void build(const vector<T> &A) {
    for (int k = 0; k < int(A.size()); k++) data[k + n - 1] = A[k];
    for (int k = n - 2; k >= 0; k--)
      data[k] = merge_values(data[2 * k + 1], data[2 * k + 2]);
  }
  void eval(int k, int l, int r) {
    if (lazy[k] == se) return;
    operate(data[k], lazy[k], r - l);
    if (r - l > 1) {
      merge_functions(lazy[2 * k + 1], lazy[k]);
      merge_functions(lazy[2 * k + 2], lazy[k]);
    }
    lazy[k] = se;
  }
  void update(int a, int b, S val, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      merge_functions(lazy[k], val);
      eval(k, l, r);
    } else {
      update(a, b, val, 2 * k + 1, l, (l + r) / 2);
      update(a, b, val, 2 * k + 2, (l + r) / 2, r);
      data[k] = merge_values(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return te;
    if (a <= l && r <= b) return data[k];
    T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return merge_values(vl, vr);
  }
  T get(int p) { return query(p, p + 1); }
};
int findFar(int st, vector<vector<Edge>> &g) {
  int N = g.size();
  vector<int> dist(N, MOD);
  dist[st] = 0;
  queue<int> Q;
  Q.push(st);
  while (!Q.empty()) {
    int now = Q.front();
    Q.pop();
    for (auto e : g[now]) {
      if (dist[e.to] == MOD) {
        dist[e.to] = dist[now] + 1;
        Q.push(e.to);
      }
    }
  }
  int mat = 0;
  for (int i = 0; i < (N); ++i)
    if (dist[mat] < dist[i]) mat = i;
  return mat;
}
pair<int, int> getDiameter(vector<vector<Edge>> &g) {
  int st = findFar(0, g);
  st = findFar(st, g);
  int ed = findFar(st, g);
  return make_pair(st, ed);
}
void dfs(int now, int par, int col, vector<vector<Edge>> &g,
         vector<int> &dist) {
  dist[now] = col;
  for (auto e : g[now]) {
    if (e.to == par) continue;
    dfs(e.to, now, col ^ e.dist, g, dist);
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<vector<Edge>> g(N);
  struct E {
    int from, to, col;
    E() {}
    E(int from, int to, int col) : from(from), to(to), col(col) {}
  };
  vector<E> edges;
  for (int i = 0; i < (N - 1); ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a].emplace_back(Edge{b, c});
    g[b].emplace_back(Edge{a, c});
    edges.emplace_back(E{a, b, c});
  }
  auto f = getDiameter(g);
  int st = f.first;
  int ed = f.second;
  const pair<int, int> unit = make_pair(0, 0);
  HeavyLightDecomposition hld1(N, g, st), hld2(N, g, ed);
  LazySegmentTree<pair<int, int>, int> seg1(N, unit, 0);
  LazySegmentTree<pair<int, int>, int> seg2(N, unit, 0);
  {
    vector<pair<int, int>> init(N);
    vector<int> dist(N);
    dfs(st, -1, 0, g, dist);
    for (int i = 0; i < (N); ++i) {
      if (dist[i] == 0)
        init[hld1.in[i]] = make_pair(hld1.dep[i], 0);
      else
        init[hld1.in[i]] = make_pair(0, hld1.dep[i]);
    }
    seg1.build(init);
  }
  {
    vector<pair<int, int>> init(N);
    vector<int> dist(N);
    dfs(ed, -1, 0, g, dist);
    for (int i = 0; i < (N); ++i) {
      if (dist[i] == 0)
        init[hld2.in[i]] = make_pair(hld2.dep[i], 0);
      else
        init[hld2.in[i]] = make_pair(0, hld2.dep[i]);
    }
    seg2.build(init);
  }
  int Q;
  cin >> Q;
  for (int q = 0; q < (Q); ++q) {
    int A;
    cin >> A;
    A--;
    {
      int now = 0;
      if (hld1.dep[edges[A].from] < hld1.dep[edges[A].to])
        now = edges[A].to;
      else
        now = edges[A].from;
      hld1.update(now, 1, [&](int l, int r, int x) { seg1.update(l, r, x); });
    }
    {
      int now = 0;
      if (hld2.dep[edges[A].from] < hld2.dep[edges[A].to])
        now = edges[A].to;
      else
        now = edges[A].from;
      hld2.update(now, 1, [&](int l, int r, int x) { seg2.update(l, r, x); });
    }
    auto res = max(seg1.query(0, N).first, seg2.query(0, N).first);
    cout << res << "\n";
  }
  return 0;
}