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;
const long long INF = 1e9;
const long long INFL = 1e18;
const long long N = 5e5 + 10;
const long long hMod = 239017;
struct node {
  int one, zero;
};
vector<vector<pair<int, int> > > g;
node tree[N * 4], tree2[N * 4];
vector<int> a, name, deep, size_g;
vector<int> a2, name2, deep2, size_g2;
bool met[N * 4];
bool met2[N * 4];
int finish, start, mx = 0, name_kol = 0;
void find_mx(int v, int pr, int k) {
  if (k > mx) {
    finish = v;
    mx = k;
  }
  for (auto next : g[v])
    if (next.first != pr) find_mx(next.first, v, k + 1);
}
void find_diametr() {
  find_mx(1, -1, 0);
  start = finish;
  mx = 0;
  find_mx(start, -1, 0);
}
void dfs(int v, int pr, int k, int sum) {
  name[v] = ++name_kol;
  a[name[v]] = sum % 2;
  deep[name[v]] = k;
  for (auto next : g[v])
    if (next.first != pr) {
      dfs(next.first, v, k + 1, sum + next.second);
      size_g[name[v]] += size_g[name[next.first]];
    }
  size_g[name[v]]++;
}
void dfs2(int v, int pr, int k, int sum) {
  name2[v] = ++name_kol;
  a2[name2[v]] = sum % 2;
  deep2[name2[v]] = k;
  for (auto next : g[v])
    if (next.first != pr) {
      dfs2(next.first, v, k + 1, sum + next.second);
      size_g2[name2[v]] += size_g2[name2[next.first]];
    }
  size_g2[name2[v]]++;
}
void build(int v, int tl, int tr) {
  if (tl == tr) {
    if (a[tl] == 0) {
      tree[v].zero = deep[tl];
      tree[v].one = -1;
    } else {
      tree[v].one = deep[tl];
      tree[v].zero = -1;
    }
  } else {
    int mid = (tl + tr) / 2;
    build(v * 2, tl, mid);
    build(v * 2 + 1, mid + 1, tr);
    tree[v].one = max(tree[v * 2].one, tree[v * 2 + 1].one);
    tree[v].zero = max(tree[v * 2].zero, tree[v * 2 + 1].zero);
  }
}
void push(int v, int tl, int tr) {
  if (met[v] == 1 && tl != tr) {
    swap(tree[v * 2].zero, tree[v * 2].one);
    swap(tree[v * 2 + 1].zero, tree[v * 2 + 1].one);
    met[v * 2] ^= 1;
    met[v * 2 + 1] ^= 1;
  }
  met[v] = 0;
}
void update(int v, int tl, int tr, int l, int r) {
  push(v, tl, tr);
  if (tl == l && tr == r) {
    met[v] = 1;
    swap(tree[v].one, tree[v].zero);
    push(v, tl, tr);
    return;
  }
  int mid = (tl + tr) / 2;
  if (r <= mid)
    update(v * 2, tl, mid, l, r);
  else if (l > mid)
    update(v * 2 + 1, mid + 1, tr, l, r);
  else {
    update(v * 2, tl, mid, l, mid);
    update(v * 2 + 1, mid + 1, tr, mid + 1, r);
  }
  tree[v].one = max(tree[v * 2].one, tree[v * 2 + 1].one);
  tree[v].zero = max(tree[v * 2].zero, tree[v * 2 + 1].zero);
}
void build2(int v, int tl, int tr) {
  if (tl == tr) {
    if (a2[tl] == 0) {
      tree2[v].zero = deep2[tl];
      tree2[v].one = -1;
    } else {
      tree2[v].one = deep2[tl];
      tree2[v].zero = -1;
    }
  } else {
    int mid = (tl + tr) / 2;
    build2(v * 2, tl, mid);
    build2(v * 2 + 1, mid + 1, tr);
    tree2[v].one = max(tree2[v * 2].one, tree2[v * 2 + 1].one);
    tree2[v].zero = max(tree2[v * 2].zero, tree2[v * 2 + 1].zero);
  }
}
void push2(int v, int tl, int tr) {
  if (met2[v] == 1 && tl != tr) {
    swap(tree2[v * 2].zero, tree2[v * 2].one);
    swap(tree2[v * 2 + 1].zero, tree2[v * 2 + 1].one);
    met2[v * 2] ^= 1;
    met2[v * 2 + 1] ^= 1;
  }
  met2[v] = 0;
}
void update2(int v, int tl, int tr, int l, int r) {
  push2(v, tl, tr);
  if (tl == l && tr == r) {
    met2[v] = 1;
    swap(tree2[v].one, tree2[v].zero);
    push2(v, tl, tr);
    return;
  }
  int mid = (tl + tr) / 2;
  if (r <= mid)
    update2(v * 2, tl, mid, l, r);
  else if (l > mid)
    update2(v * 2 + 1, mid + 1, tr, l, r);
  else {
    update2(v * 2, tl, mid, l, mid);
    update2(v * 2 + 1, mid + 1, tr, mid + 1, r);
  }
  tree2[v].one = max(tree2[v * 2].one, tree2[v * 2 + 1].one);
  tree2[v].zero = max(tree2[v * 2].zero, tree2[v * 2 + 1].zero);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  g.resize(n + 1);
  vector<pair<int, int> > road, road2;
  for (int i = 1; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    g[x].push_back({y, z});
    g[y].push_back({x, z});
    road.push_back({x, y});
  }
  find_diametr();
  size_g.resize(n + 1);
  name.resize(n + 1);
  deep.resize(n + 1);
  a.resize(n + 1);
  size_g2.resize(n + 1);
  name2.resize(n + 1);
  deep2.resize(n + 1);
  a2.resize(n + 1);
  dfs(finish, -1, 0, 0);
  name_kol = 0;
  dfs2(start, -1, 0, 0);
  for (int i = 0; i < n - 1; i++) {
    road2.push_back({name2[road[i].first], name2[road[i].second]});
  }
  for (int i = 0; i < n - 1; i++) {
    road[i].first = name[road[i].first];
    road[i].second = name[road[i].second];
  }
  build(1, 1, n);
  build2(1, 1, n);
  int q;
  cin >> q;
  while (q--) {
    int nom;
    cin >> nom;
    nom--;
    int x = road[nom].first;
    int y = road[nom].second;
    if (deep[x] > deep[y]) swap(x, y);
    update(1, 1, n, y, y + size_g[y] - 1);
    x = road2[nom].first;
    y = road2[nom].second;
    if (deep2[x] > deep2[y]) swap(x, y);
    update2(1, 1, n, y, y + size_g2[y] - 1);
    int ans = max(tree[1].zero, tree2[1].zero);
    cout << ans << "\n";
  }
  return 0;
}