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 int N = 5e5 + 7;
struct SegTree {
  pair<int, int> tr[4 * N];
  bool lz[4 * N];
  pair<int, int> combine(pair<int, int> left, pair<int, int> right) {
    return make_pair(max(left.first, right.first),
                     max(left.second, right.second));
  }
  void propagate(int u, int st, int en) {
    if (!lz[u]) return;
    swap(tr[u].first, tr[u].second);
    if (st != en) {
      lz[2 * u] ^= 1;
      lz[2 * u + 1] ^= 1;
    }
    lz[u] = 0;
  }
  void build(int u, int st, int en, int *aa, int *tp) {
    if (st == en) {
      if (tp[st])
        tr[u] = {0, aa[st]};
      else
        tr[u] = {aa[st], 0};
      lz[u] = 0;
    } else {
      int mid = (st + en) / 2;
      build(2 * u, st, mid, aa, tp);
      build(2 * u + 1, mid + 1, en, aa, tp);
      tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
      lz[u] = 0;
    }
  }
  void update(int u, int st, int en, int l, int r) {
    propagate(u, st, en);
    if (r < st || en < l)
      return;
    else if (l <= st && en <= r) {
      lz[u] ^= 1;
      propagate(u, st, en);
    } else {
      int mid = (st + en) / 2;
      update(2 * u, st, mid, l, r);
      update(2 * u + 1, mid + 1, en, l, r);
      tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
    }
  }
  int query(int u, int st, int en, int l, int r) {
    propagate(u, st, en);
    if (r < st || en < l)
      return 0;
    else if (l <= st && en <= r)
      return tr[u].first;
    else {
      int mid = (st + en) / 2;
      return max(query(2 * u, st, mid, l, r),
                 query(2 * u + 1, mid + 1, en, l, r));
    }
  }
};
struct Edge {
  int u, v, w;
  int other(int x) { return u ^ x ^ v; }
};
vector<Edge> edges;
vector<int> adj[N];
bool done[N];
int farthest(int s) {
  memset(done, 0, sizeof done);
  queue<int> q;
  q.push(s);
  done[s] = 1;
  int ans = s;
  while (q.size()) {
    int u = q.front();
    ans = u;
    q.pop();
    for (int e : adj[u]) {
      int v = edges[e].other(u);
      if (!done[v]) {
        q.push(v);
        done[v] = 1;
      }
    }
  }
  return ans;
}
bool col[2][N];
int dis[2][N];
int st[2][N], en[2][N], par[2][N];
int aa[2][N], cc[2][N];
SegTree tree[2];
void dfs(int id, int u, int pe, int &iid) {
  st[id][u] = ++iid;
  aa[id][iid] = dis[id][u];
  cc[id][iid] = col[id][u];
  for (int e : adj[u]) {
    if (e == pe) continue;
    int v = edges[e].other(u);
    bool w = edges[e].w;
    dis[id][v] = 1 + dis[id][u];
    col[id][v] = col[id][u] ^ w;
    par[id][v] = u;
    dfs(id, v, e, iid);
  }
  en[id][u] = iid;
}
int n;
void make(int u, int id) {
  int iid = 0;
  dfs(id, u, -1, iid);
  assert(iid == n);
  tree[id].build(1, 1, n, aa[id], cc[id]);
}
void flip(int id, int e) {
  int u = edges[e].u;
  int v = edges[e].v;
  if (par[id][u] != v) swap(u, v);
  assert(par[id][u] == v);
  tree[id].update(1, 1, n, st[id][u], en[id][u]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back(i);
    adj[v].push_back(i);
    edges.push_back({u, v, w});
  }
  int A = farthest(1);
  int B = farthest(A);
  make(A, 0);
  make(B, 1);
  int m;
  cin >> m;
  while (m--) {
    int id;
    cin >> id;
    id--;
    flip(0, id);
    flip(1, id);
    cout << max(tree[0].query(1, 1, n, 1, n), tree[1].query(1, 1, n, 1, n))
         << "\n";
  }
}