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;
using ll = long long;
const int inf = 1e9 + 10;
const ll inf_ll = 1e18 + 10;
struct segtree {
  struct node {
    int x0, x1;
  };
  node combine(node x, node y) { return {max(x.x0, y.x0), max(x.x1, y.x1)}; }
  constexpr static node identity = {-inf, -inf};
  vector<int> a, b;
  vector<node> tree;
  vector<bool> u;
  int n;
  segtree(vector<node>& v) {
    n = 1 << int(ceil(log2(v.size())));
    tree.resize(2 * n);
    a.resize(2 * n);
    b.resize(2 * n);
    u.assign(2 * n, 0);
    for (int i = 2 * n - 1; i > 0; i--) {
      if (i >= n) {
        a[i] = b[i] = i - n;
        if (i - n < v.size()) {
          tree[i] = v[i - n];
        } else {
          tree[i] = identity;
        }
      } else {
        a[i] = a[2 * i];
        b[i] = b[2 * i + 1];
        tree[i] = combine(tree[2 * i], tree[2 * i + 1]);
      }
    }
  }
  void push(int i) {
    if (!u[i]) return;
    swap(tree[i].x0, tree[i].x1);
    if (i < n) {
      u[2 * i] = !u[2 * i];
      u[2 * i + 1] = !u[2 * i + 1];
    }
    u[i] = 0;
  }
  void update(int l, int r, int i = 1) {
    if (l <= a[i] && r >= b[i]) {
      u[i] = !u[i];
    }
    push(i);
    if (l > b[i] || r < a[i] || l <= a[i] && r >= b[i]) return;
    update(l, r, 2 * i);
    update(l, r, 2 * i + 1);
    tree[i] = combine(tree[2 * i], tree[2 * i + 1]);
  }
  node query(int l, int r, int i = 1) {
    push(i);
    if (l <= a[i] && r >= b[i]) return tree[i];
    if (l > b[i] || r < a[i]) return identity;
    return combine(query(l, r, 2 * i), query(l, r, 2 * i + 1));
  }
};
int n;
vector<vector<array<int, 2>>> adj;
int bfs(int i) {
  vector<bool> v(n);
  queue<int> q({i});
  v[i] = 1;
  int x;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (auto& [t, j] : adj[x])
      if (!v[j]) v[j] = 1, q.push(j);
  }
  return x;
}
vector<int> d, col;
vector<segtree::node> v;
int tt = 0;
vector<int> vin, vout;
void dfs(int i, int k) {
  vin[i] = tt++;
  if (col[i])
    v.push_back({-inf, d[i]});
  else
    v.push_back({d[i], -inf});
  for (auto& [t, j] : adj[i])
    if (j != k) {
      d[j] = d[i] + 1;
      col[j] = col[i] ^ t;
      dfs(j, i);
    }
  vout[i] = tt - 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  adj.resize(n);
  vector<array<int, 2>> e;
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    int t;
    cin >> t;
    adj[x].push_back({t, y}), adj[y].push_back({t, x});
    e.push_back({x, y});
  }
  int r1 = bfs(0);
  int r2 = bfs(r1);
  0;
  d.assign(n, 0);
  col.assign(n, 0);
  vin.assign(n, 0);
  vout.assign(n, 0);
  v.clear();
  dfs(r1, r1);
  vector<int> vin1(vin), vout1(vout);
  segtree s1(v);
  tt = 0;
  d.assign(n, 0);
  col.assign(n, 0);
  vin.assign(n, 0);
  vout.assign(n, 0);
  v.clear();
  dfs(r2, r2);
  vector<int> vin2(vin), vout2(vout);
  segtree s2(v);
  int m;
  cin >> m;
  while (m--) {
    int i;
    cin >> i;
    i--;
    auto [x, y] = e[i];
    0;
    if (vin1[x] >= vin1[y])
      s1.update(vin1[x], vout1[x]);
    else
      s1.update(vin1[y], vout1[y]);
    if (vin2[x] >= vin2[y]) {
      0;
      s2.update(vin2[x], vout2[x]);
    } else {
      0;
      s2.update(vin2[y], vout2[y]);
    }
    0;
    0;
    cout << max(s1.query(0, n - 1).x0, s2.query(0, n - 1).x0) << "\n";
  }
}