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 maxn = 5e5 + 5;
vector<long long> a[maxn];
vector<long long> b[maxn];
long long corn[maxn];
long long corn1[maxn];
bool used[maxn];
long long tin[maxn];
long long tout[maxn];
long long z[maxn];
long long timer = 0;
long long t[4 * maxn][2][2];
long long is[4 * maxn];
void dfs(long long x) {
  used[x] = true;
  for (auto v : a[x]) {
    if (!used[v]) {
      corn[v] = corn[x] + 1;
      dfs(v);
    }
  }
  used[x] = false;
}
void dfs1(long long x) {
  used[x] = true;
  for (auto v : a[x]) {
    if (!used[v]) {
      corn1[v] = corn1[x] + 1;
      dfs1(v);
    }
  }
  used[x] = false;
}
void dfs2(long long x) {
  tin[x] = timer;
  z[timer] = x;
  ++timer;
  used[x] = true;
  for (auto v : a[x]) {
    if (!used[v]) {
      dfs2(v);
    }
  }
  tout[x] = timer;
  used[x] = false;
}
void build(long long node, long long tl, long long tr) {
  if ((tr - tl) == 1) {
    long long x = z[tl];
    t[node][0][0] = corn[x];
    t[node][0][1] = corn1[x];
    t[node][1][0] = (-1e18);
    t[node][1][1] = (-1e18);
    is[node] = 0;
    return;
  }
  long long tm = (tl + tr) / 2;
  build(2 * node + 1, tl, tm);
  build(2 * node + 2, tm, tr);
  is[node] = 0;
  t[node][1][0] = (-1e18);
  t[node][1][1] = (-1e18);
  t[node][0][0] = max(t[2 * node + 1][0][0], t[2 * node + 2][0][0]);
  t[node][0][1] = max(t[2 * node + 1][0][1], t[2 * node + 2][0][1]);
}
vector<vector<long long> > merg(vector<vector<long long> > u,
                                vector<vector<long long> > v) {
  for (long long i = 0; i < 2; ++i) {
    for (long long j = 0; j < 2; ++j) {
      u[i][j] = max(u[i][j], v[i][j]);
    }
  }
  return u;
}
void push(long long node) {
  if (is[node]) {
    is[2 * node + 1] ^= 1;
    is[2 * node + 2] ^= 1;
    is[node] = 0;
    swap(t[2 * node + 1][0][0], t[2 * node + 1][1][0]);
    swap(t[2 * node + 1][0][1], t[2 * node + 1][1][1]);
    swap(t[2 * node + 2][0][0], t[2 * node + 2][1][0]);
    swap(t[2 * node + 2][0][1], t[2 * node + 2][1][1]);
    for (long long i = 0; i < 2; ++i) {
      for (long long j = 0; j < 2; ++j) {
        t[node][i][j] = max(t[2 * node + 1][i][j], t[2 * node + 2][i][j]);
      }
    }
  }
}
void to(long long node, long long tl, long long tr, long long l, long long r) {
  if (tl >= l && tr <= r) {
    is[node] ^= 1;
    swap(t[node][0][0], t[node][1][0]);
    swap(t[node][0][1], t[node][1][1]);
    return;
  }
  if (tl >= r || tr <= l) {
    return;
  }
  push(node);
  long long tm = (tl + tr) / 2;
  to(2 * node + 1, tl, tm, l, r);
  to(2 * node + 2, tm, tr, l, r);
  for (long long i = 0; i < 2; ++i) {
    for (long long j = 0; j < 2; ++j) {
      t[node][i][j] = max(t[2 * node + 1][i][j], t[2 * node + 2][i][j]);
    }
  }
}
vector<vector<long long> > get(long long node, long long tl, long long tr,
                               long long l, long long r) {
  if (tr <= l || tl >= r) {
    return {{(long long)-1e18, (long long)-1e18},
            {(long long)-1e18, (long long)-1e18}};
  }
  if (tl >= l && tr <= r) {
    return {{t[node][0][0], t[node][0][1]}, {t[node][1][0], t[node][1][1]}};
  }
  push(node);
  long long tm = (tl + tr) / 2;
  return merg(get(2 * node + 1, tl, tm, l, r), get(2 * node + 2, tm, tr, l, r));
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<pair<long long, long long> > v;
  pair<long long, long long> u[n - 1];
  for (long long i = 0; i < (n - 1); ++i) {
    long long x, y;
    cin >> x >> y;
    x--;
    y--;
    long long w;
    cin >> w;
    u[i] = {x, y};
    if (w == 1) v.push_back({x, y});
    b[x].push_back(w);
    b[y].push_back(w);
    a[x].push_back(y);
    a[y].push_back(x);
  }
  corn[0] = 0;
  dfs(0);
  long long d1;
  long long ma = (-1);
  for (long long i = 0; i < n; ++i) {
    if (corn[i] >= ma) {
      ma = corn[i];
      d1 = i;
    }
  }
  corn[d1] = 0;
  dfs(d1);
  long long d2;
  ma = (-1);
  for (long long i = 0; i < n; ++i) {
    if (corn[i] >= ma) {
      ma = corn[i];
      d2 = i;
    }
  }
  corn1[d2] = 0;
  dfs1(d2);
  dfs2(d1);
  build(0, 0, n);
  long long d2c = 0;
  for (auto h : v) {
    if (corn[h.first] <= corn[h.second]) {
      swap(h.first, h.second);
    }
    to(0, 0, n, tin[h.first], tout[h.first]);
    if (tin[h.first] <= tin[d2] && tout[h.first] >= tout[d2]) {
      d2c ^= 1;
    }
  }
  vector<vector<long long> > v1 = get(0, 0, n, 0, n);
  long long m;
  cin >> m;
  while (m--) {
    long long x;
    cin >> x;
    x--;
    pair<long long, long long> h = u[x];
    if (corn[h.first] <= corn[h.second]) {
      swap(h.first, h.second);
    }
    to(0, 0, n, tin[h.first], tout[h.first]);
    if (tin[h.first] <= tin[d2] && tout[h.first] >= tout[d2]) {
      d2c ^= 1;
    }
    vector<vector<long long> > v = get(0, 0, n, 0, n);
    cout << max(v[0][0], v[d2c][1]) << '\n';
  }
  return 0;
}