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;
#pragma GCC optimize("O3")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int get_random() {
  static uniform_int_distribution<int> dist(0, 1e9 + 6);
  return dist(rng);
}
template <class T>
void make_unique(T& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
int geti() {
  int x;
  cin >> x;
  return x;
}
long long getll() {
  long long x;
  cin >> x;
  return x;
}
double getd() {
  double x;
  cin >> x;
  return x;
}
const int MAXN = 5e5 + 100;
void solve(int tt) {}
template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S),
          F (*composition)(F, F), F (*id)()>
struct segment_tree {
  int _l, _r;
  vector<S> a;
  vector<S> tree;
  vector<F> lazy;
  void push(int l, int r, int node) {
    if (lazy[node] != id()) {
      int mid = (l + r) / 2;
      int rnode = node + (mid - l + 1) * 2;
      tree[node + 1] = mapping(lazy[node], tree[node + 1]);
      lazy[node + 1] = composition(lazy[node], lazy[node + 1]);
      tree[rnode] = mapping(lazy[node], tree[rnode]);
      lazy[rnode] = composition(lazy[node], lazy[rnode]);
      lazy[node] = id();
    }
  }
  void build(int l, int r, int node) {
    if (l == r)
      tree[node] = a[l];
    else {
      int mid = l + (r - l) / 2;
      int rnode = node + (mid - l + 1) * 2;
      build(l, mid, node + 1);
      build(mid + 1, r, rnode);
      tree[node] = op(tree[node + 1], tree[rnode]);
    }
  }
  void update(F v, int left, int right, int l, int r, int node) {
    if (left > right) return;
    if (right < l || r < left) return;
    if (left <= l && r <= right) {
      tree[node] = mapping(v, tree[node]);
      lazy[node] = composition(v, lazy[node]);
    } else {
      push(l, r, node);
      int mid = l + (r - l) / 2;
      int rnode = node + (mid - l + 1) * 2;
      update(v, left, right, l, mid, node + 1);
      update(v, left, right, mid + 1, r, rnode);
      tree[node] = op(tree[node + 1], tree[rnode]);
    }
  }
  S query(int left, int right, int l, int r, int node) {
    if (left > right) return e();
    if (right < l || r < left) return e();
    if (left <= l && r <= right) return tree[node];
    push(l, r, node);
    int mid = l + (r - l) / 2;
    int rnode = node + (mid - l + 1) * 2;
    S r1 = query(left, right, l, mid, node + 1);
    S r2 = query(left, right, mid + 1, r, rnode);
    return op(r1, r2);
  }
  vector<S> get_tree() {
    vector<S> res;
    for (int i = _l; i <= _r; i++) {
      res.push_back(query(i, i));
    }
    return res;
  }
  void init(int l, int r) {
    _l = l;
    _r = r;
    a.assign(r + 1, e());
    tree.assign(2 * r + 1, e());
    lazy.assign(2 * r + 1, id());
  }
  S& operator[](int idx) { return a[idx]; }
  void build() { build(_l, _r, 0); }
  void update(F v, int left, int right) { update(v, left, right, _l, _r, 0); }
  S query(int left, int right) { return query(left, right, _l, _r, 0); }
};
long long zero() { return 0; }
long long int_max() { return INT_MAX; }
long long int_min() { return INT_MIN; }
long long get_min(long long x, long long y) { return min(x, y); }
long long get_max(long long x, long long y) { return max(x, y); }
long long add(long long x, long long y) { return x + y; }
long long mul(long long x, long long y) { return x * y; }
pair<int, int> f1(pair<int, int> x, pair<int, int> y) {
  return pair<int, int>({min(x.first, y.first), max(x.second, y.second)});
};
pair<int, int> f2() { return pair<int, int>({1e9, -1e9}); };
pair<int, int> f3(int x, pair<int, int> y) {
  return pair<int, int>(
      {min(y.second * x, y.first * x), max(y.second * x, y.first * x)});
};
int f4(int x, int y) { return x * y; };
int f5() { return 1; };
segment_tree<pair<int, int>, f1, f2, int, f3, f4, f5> segt;
vector<int> graph1[MAXN];
vector<int> graph2[MAXN];
int in[MAXN], out[MAXN], level[MAXN], t = 1;
int cnt[MAXN];
int n;
void dfs(int i, int p, int c, int l) {
  ;
  ;
  in[i] = t++;
  cnt[in[i]] = c;
  segt[in[i]] = (c % 2 ? pair<int, int>({-l, -l}) : pair<int, int>({l, l}));
  level[i] = l;
  for (auto j : graph1[i]) {
    if (j == p) continue;
    dfs(j, i, c, l + 1);
  }
  for (auto j : graph2[i]) {
    if (j == p) continue;
    dfs(j, i, c + 1, l + 1);
  }
  out[i] = t;
}
pair<int, int> find_root() {
  queue<int> q({1});
  vector<int> vis(n + 1);
  vis[1] = 1;
  int res1 = 1;
  while (q.size()) {
    int len = q.size();
    while (len--) {
      auto f = q.front();
      q.pop();
      for (auto p : graph1[f]) {
        if (!vis[p]) {
          q.push(p);
          res1 = p;
          vis[p] = 1;
        }
      }
      for (auto p : graph2[f]) {
        if (!vis[p]) {
          q.push(p);
          res1 = p;
          vis[p] = 1;
        }
      }
    }
  }
  int res2 = res1;
  q.push(res1);
  vis = vector<int>(n + 1);
  vis[res1] = 1;
  while (q.size()) {
    int len = q.size();
    while (len--) {
      auto f = q.front();
      q.pop();
      for (auto p : graph1[f]) {
        if (!vis[p]) {
          q.push(p);
          res2 = p;
          vis[p] = 1;
        }
      }
      for (auto p : graph2[f]) {
        if (!vis[p]) {
          q.push(p);
          res2 = p;
          vis[p] = 1;
        }
      }
    }
  }
  return {res1, res2};
}
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  int u, v, tp;
  vector<int> us, vs;
  for (auto i = (0); i < (n - 1); i++) {
    cin >> u >> v >> tp;
    if (tp == 0) {
      graph1[u].push_back(v);
      graph1[v].push_back(u);
    } else {
      graph2[u].push_back(v);
      graph2[v].push_back(u);
    }
    us.push_back(u);
    vs.push_back(v);
  }
  pair<int, int> roots = find_root();
  segt.init(1, n);
  t = 1;
  dfs(roots.first, -1, 0, 0);
  segt.build();
  int m;
  cin >> m;
  vector<int> idxs;
  for (auto i = (0); i < (m); i++) {
    int idx;
    cin >> idx;
    idx--;
    idxs.push_back(idx);
  }
  vector<int> res1(m), res2(m);
  for (auto i = (0); i < (m); i++) {
    int idx = idxs[i];
    u = us[idx], v = vs[idx];
    if (level[u] > level[v]) swap(u, v);
    ;
    ;
    segt.update(-1, in[v], out[v] - 1);
    res1[i] = segt.query(1, n).second;
  }
  segt.init(1, n);
  t = 1;
  dfs(roots.second, -1, 0, 0);
  segt.build();
  ;
  ;
  ;
  ;
  for (auto i = (0); i < (m); i++) {
    int idx = idxs[i];
    u = us[idx], v = vs[idx];
    if (level[u] > level[v]) swap(u, v);
    ;
    ;
    segt.update(-1, in[v], out[v] - 1);
    res2[i] = segt.query(1, n).second;
  }
  for (auto i = (0); i < (m); i++) {
    cout << max(res1[i], res2[i]) << endl;
  }
  return 0;
}