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;
struct Info {
  int u, type;
};
const int INF = 1e9;
struct segment_tree {
  vector<int> _max1;
  vector<int> _max2;
  vector<int> promise;
  vector<bool> used;
  vector<int> tin;
  vector<int> tout;
  int timer;
  segment_tree(vector<vector<Info> > &g, int _u) {
    timer = 0;
    int n = (1 << 21);
    _max1.resize(n, 0);
    _max2.resize(n, 0);
    used.resize(n, false);
    tin.resize(1 << 20);
    tout.resize(1 << 20);
    dfs1(_u, g, 0, 0);
    promise.resize(n, -INF);
    build();
  }
  void build() {
    int now = (1 << 19) - 2;
    while (now >= 0) {
      promise[now] = 0;
      _max1[now] = max(_max1[now * 2 + 1], _max1[now * 2 + 2]);
      _max2[now] = max(_max2[now * 2 + 1], _max2[now * 2 + 2]);
      now--;
    }
  }
  void dfs1(int v, vector<vector<Info> > &g, int parity, int height) {
    tin[v] = timer++;
    if (parity == 0)
      _max1[(1 << 19) - 1 + tin[v]] = height;
    else
      _max2[(1 << 19) - 1 + tin[v]] = height;
    used[v] = true;
    for (auto [_u, _type] : g[v]) {
      if (!used[_u]) {
        dfs1(_u, g, parity ^ _type, height + 1);
      }
    }
    tout[v] = timer;
  }
  void push(int v) {
    if (promise[v] == 1) {
      promise[v] = 0;
      swap(_max1[2 * v + 1], _max2[2 * v + 1]);
      swap(_max1[2 * v + 2], _max2[2 * v + 2]);
      promise[2 * v + 1] ^= 1;
      promise[2 * v + 2] ^= 1;
    }
  }
  void updute(int v, int l, int r, int lq, int rq) {
    if (lq >= r || l >= rq) {
      return;
    }
    if (l >= lq && r <= rq) {
      promise[v] ^= 1;
      swap(_max1[v], _max2[v]);
      return;
    }
    push(v);
    int m = (l + r) / 2;
    updute(v * 2 + 1, l, m, lq, rq);
    updute(v * 2 + 2, m, r, lq, rq);
    _max1[v] = max(_max1[2 * v + 1], _max1[2 * v + 2]);
    _max2[v] = max(_max2[2 * v + 1], _max2[2 * v + 2]);
  }
  int Answer(int q) {
    updute(0, 0, (1 << 19), tin[q], tout[q]);
    return _max1[0];
  }
  void write(int v, int l, int r) {
    push(v);
    if (l + 1 == r) {
      cout << _max1[v] << ' ' << _max2[v] << '\n';
      return;
    }
    int m = (l + r) / 2;
    write(v * 2 + 1, l, m);
    write(v * 2 + 2, m, r);
  }
};
void dfs(int v, vector<bool> &used, vector<vector<Info> > &g, vector<int> &d) {
  used[v] = true;
  for (auto [_u, _type] : g[v]) {
    if (!used[_u]) {
      d[_u] = d[v] + 1;
      dfs(_u, used, g, d);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<vector<Info> > g(n);
  vector<pair<int, int> > numbers;
  for (int i = 0; i < n - 1; ++i) {
    int _u, _v, _type;
    cin >> _v >> _u >> _type;
    numbers.push_back({_v - 1, _u - 1});
    g[_v - 1].push_back(Info{_u - 1, _type});
    g[_u - 1].push_back(Info{_v - 1, _type});
  }
  vector<bool> used(n, false);
  vector<int> d(n, 0);
  dfs(0, used, g, d);
  int _u = 0;
  for (int i = 0; i < n; ++i) {
    if (d[i] > d[_u]) {
      _u = i;
    }
  }
  used.assign(n, false);
  d.assign(n, 0);
  dfs(_u, used, g, d);
  int _v = 0;
  for (int i = 0; i < n; ++i) {
    if (d[i] > d[_v]) {
      _v = i;
    }
  }
  segment_tree st1(g, _u);
  segment_tree st2(g, _v);
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int q, q1, q2;
    cin >> q;
    if (st1.tin[numbers[q - 1].first] > st1.tin[numbers[q - 1].second])
      q1 = numbers[q - 1].first;
    else
      q1 = numbers[q - 1].second;
    if (st2.tin[numbers[q - 1].first] > st2.tin[numbers[q - 1].second])
      q2 = numbers[q - 1].first;
    else
      q2 = numbers[q - 1].second;
    cout << max(st1.Answer(q1), st2.Answer(q2)) << '\n';
  }
}