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;
template <typename T1, typename T2>
inline void chkmin(T1 &x, T2 y) {
  if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, T2 y) {
  if (x < y) x = y;
}
const string FILENAME = "input";
const int MAXN = 500228;
int n, q;
vector<pair<int, int> > g[MAXN];
int c[MAXN];
int dep[MAXN];
int last = 0;
int ft[MAXN];
int sums[MAXN];
unordered_map<int, int> in[MAXN];
vector<int> out[MAXN];
int timer;
vector<int> who[MAXN];
vector<int> score[MAXN][2];
struct rmq {
  vector<pair<int, int> > d;
  vector<int> mod;
  int ss = 1;
  void init(int n) {
    while (ss < n) {
      ss <<= 1;
    }
    d.resize(2 * ss);
    mod.resize(2 * ss);
    for (auto &x : d) {
      x = make_pair(0, 0);
    }
    for (auto &x : mod) {
      x = 0;
    }
  }
  void relax() {
    for (int v = ss - 1; v >= 0; v--) {
      d[v].first = max(d[v * 2].first, d[v * 2 + 1].first);
      d[v].second = max(d[v * 2].second, d[v * 2 + 1].second);
    }
  }
  void push(int v) {
    if (mod[v] != 0) {
      swap(d[v].first, d[v].second);
      if (v < ss) {
        mod[v * 2] ^= mod[v];
        mod[v * 2 + 1] ^= mod[v];
      }
      mod[v] = 0;
    }
  }
  void add(int v, int vl, int vr, int l, int r, int x) {
    if (vl > r || vr < l) {
      push(v);
      return;
    }
    if (l <= vl && vr <= r) {
      mod[v] ^= x;
      push(v);
      return;
    }
    push(v);
    add(v * 2, vl, (vl + vr) / 2, l, r, x);
    add(v * 2 + 1, (vl + vr) / 2 + 1, vr, l, r, x);
    d[v].first = max(d[v * 2].first, d[v * 2 + 1].first);
    d[v].second = max(d[v * 2].second, d[v * 2 + 1].second);
  }
  int curadd = 0;
  int getmax(int v, int vl, int vr, int l, int r, int dd) {
    if (vl > r || vr < l) {
      return 0LL;
    }
    if (l <= vl && vr <= r) {
      int need = dd ^ mod[v] ^ curadd;
      return (need == 0 ? d[v].first : d[v].second);
    }
    curadd ^= mod[v];
    auto res = max(getmax(v * 2, vl, (vl + vr) / 2, l, r, dd),
                   getmax(v * 2 + 1, (vl + vr) / 2 + 1, vr, l, r, dd));
    curadd ^= mod[v];
    return res;
  }
} kek[MAXN];
multiset<int> keks[MAXN][2];
vector<int> deps[MAXN];
int cs[MAXN];
int root;
int deep[MAXN];
void dfs(int u, int pr = -1) {
  timer++;
  in[root][u] = timer;
  int pos = timer - 1;
  for (auto h : g[u]) {
    if (h.first == pr) {
      continue;
    }
    if (deep[h.first]) {
      continue;
    }
    if (u == root) {
      who[root][timer] = timer;
    } else {
      who[root][timer] = who[root][pos];
    }
    deps[root][timer] = deps[root][pos] + 1;
    cs[timer] = cs[pos] ^ c[h.second];
    int gg = timer;
    dfs(h.first, u);
    if (u == root) {
      for (int d = 0; d < 2; d++) {
        if (score[root][d][gg]) {
          keks[root][d].insert(score[root][d][gg]);
        }
      }
    }
  }
  out[root][pos] = timer;
  chkmax(score[root][cs[pos]][who[root][pos]], deps[root][pos]);
  if (cs[pos] == 0) {
    kek[root].d[kek[root].ss + pos].first = deps[root][pos];
    kek[root].d[kek[root].ss + pos].second = 0;
  } else {
    kek[root].d[kek[root].ss + pos].second = deps[root][pos];
    kek[root].d[kek[root].ss + pos].first = 0;
  }
}
int ft1[MAXN];
void updateDistsForRoot(int root, int id, int diff) {
  int x = ft[id];
  int y = ft1[id];
  if (in[root].find(x) == in[root].end() ||
      in[root].find(y) == in[root].end()) {
    return;
  }
  int fts = in[root][x] - 1;
  int fts1 = in[root][y] - 1;
  if (deps[root][fts] < deps[root][fts1]) {
    swap(x, y);
    swap(fts, fts1);
  }
  kek[root].add(1, 1, kek[root].ss, fts + 1, out[root][fts], diff);
  int ff = who[root][fts];
  for (int tt = 0; tt < 2; tt++) {
    auto &value = score[root][tt][ff];
    if (value != 0) {
      keks[root][tt].erase(keks[root][tt].find(value));
    }
    value = kek[root].getmax(1, 1, kek[root].ss, ff + 1, out[root][ff], tt);
    if (value != 0) {
      keks[root][tt].insert(value);
    }
  }
}
multiset<int> kekr;
int findAnsForRoot(int root) {
  int res = 0;
  for (int t = 0; t < 2; t++) {
    if (keks[root][t].empty()) {
      continue;
    }
    auto x = keks[root][t].rbegin();
    int ans = 0;
    int cp = 0;
    for (int it = 0; it < 2; it++) {
      if (x == keks[root][t].rend()) {
        break;
      }
      ans += *x;
      cp += t;
      x++;
    }
    if (cp % 2 == 0) {
      chkmax(res, ans);
    }
  }
  return res;
}
void change(int root, int id, int diff) {
  kekr.erase(kekr.find(findAnsForRoot(root)));
  updateDistsForRoot(root, id, diff);
  kekr.insert(findAnsForRoot(root));
}
int sz[MAXN];
int cng;
int PRcentroid[MAXN];
vector<int> v[MAXN];
vector<int> sts;
int d[MAXN];
void dfs2(int u, int pr = -1) {
  sz[u] = 1;
  sts.push_back(u);
  for (auto h : g[u]) {
    if (!deep[h.first] && h.first != pr) {
      d[h.first] = d[u] + 1;
      dfs2(h.first, u);
      sz[u] += sz[h.first];
    }
  }
}
int curcentre;
vector<int> st[MAXN];
void dfs1(int u, int pr = -1) {
  st[curcentre].push_back(u);
  for (auto h : g[u]) {
    if (!deep[h.first] && h.first != pr) {
      dfs1(h.first, u);
    }
  }
}
inline int find_centroid() {
  int ans = -1;
  for (auto u : sts) {
    if (sz[u] >= cng / 2) {
      if (ans == -1 || d[u] > d[ans]) {
        ans = u;
      }
    }
  }
  return ans;
}
void centroid_decomposition(int s) {
  curcentre = s;
  cng = n;
  dfs1(s);
  root = s;
  kek[root].init(cng);
  timer = 0;
  deps[root].resize(cng);
  out[root].resize(cng);
  who[root].resize(cng);
  score[root][1].resize(cng);
  score[root][0].resize(cng);
  cs[root] = 0;
  dfs(root);
  kek[root].relax();
  kekr.insert(findAnsForRoot(root));
}
int roots, root1;
void trys(int u, int id, int diff) {
  change(roots, id, diff);
  change(root1, id, diff);
}
int dist[MAXN];
void dfsk(int u, int pr = -1) {
  for (auto h : g[u]) {
    if (h.first != pr) {
      dist[h.first] = dist[u] + 1;
      dfsk(h.first, u);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b >> c[i];
    a--, b--;
    g[a].push_back({b, i});
    g[b].push_back({a, i});
    if (a > b) {
      swap(a, b);
    }
    ft[i] = b;
    ft1[i] = a;
  }
  dfsk(0);
  int cur = 0;
  for (int i = 0; i < n; i++) {
    if (dist[i] > dist[cur]) {
      cur = i;
    }
  }
  roots = cur;
  dist[roots] = 0;
  dfsk(roots);
  cur = 0;
  for (int i = 0; i < n; i++) {
    if (dist[i] > dist[cur]) {
      cur = i;
    }
  }
  root1 = cur;
  centroid_decomposition(roots);
  centroid_decomposition(root1);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int d;
    cin >> d;
    d--;
    c[d] ^= 1;
    trys(ft[d], d, 1);
    if (kekr.empty()) {
      cout << 0 << '\n';
    } else {
      cout << *kekr.rbegin() << '\n';
    }
  }
  return 0;
}