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 T>
inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 60;
const int mods = 1e9 + 7;
const int inv2 = (mods + 1) >> 1;
const int MAXN = 500005;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                *oT = obuf + SIZE - 1, c;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char c) {
  *oS++ = c;
  if (oS == oT) flush();
}
inline void getc(char &c) {
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       !isalpha(c) && c != EOF;
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
}
inline void reads(char *st) {
  char c;
  int n = 0;
  getc(st[++n]);
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       isalpha(c);
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    st[++n] = c;
  st[n + 1] = '\0';
}
template <class I>
inline void read(I &x) {
  for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 3) + (x << 1) + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (x < 0) putc('-'), x = -x;
  if (!x) putc('0');
  while (x) que[++num] = x % 10 + 48, x /= 10;
  while (num) putc(que[num--]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_Flusher_;
}  // namespace FastIO
using FastIO ::print;
using FastIO ::putc;
using FastIO ::read;
using FastIO ::reads;
pair<int, int> E[MAXN];
vector<pair<int, int> > e[MAXN];
int id[MAXN], dfnA[MAXN], dfnB[MAXN], dep[MAXN], depA[MAXN], depB[MAXN],
    fnsA[MAXN], fnsB[MAXN], col[MAXN], MX, ID, DFN = 0, A, B;
void dfsA(int x, int father) {
  id[dfnA[x] = ++DFN] = x;
  if (dep[x] > MX) MX = dep[x], ID = x;
  for (auto v : e[x]) {
    if (v.first == father) continue;
    col[v.first] = col[x] ^ v.second, dep[v.first] = dep[x] + 1,
    dfsA(v.first, x);
  }
  fnsA[x] = DFN;
}
void dfsB(int x, int father) {
  id[dfnB[x] = ++DFN] = x;
  if (dep[x] > MX) MX = dep[x], ID = x;
  for (auto v : e[x]) {
    if (v.first == father) continue;
    col[v.first] = col[x] ^ v.second, dep[v.first] = dep[x] + 1,
    dfsB(v.first, x);
  }
  fnsB[x] = DFN;
}
struct Segment_Tree {
  int tag[MAXN << 2], s[MAXN << 2][2];
  void down(int x) {
    if (tag[x]) {
      tag[x << 1] ^= 1, swap(s[x << 1][0], s[x << 1][1]);
      tag[x << 1 | 1] ^= 1, swap(s[x << 1 | 1][0], s[x << 1 | 1][1]);
      tag[x] = 0;
    }
  }
  void up(int x) {
    s[x][0] = max(s[x << 1][0], s[x << 1 | 1][0]);
    s[x][1] = max(s[x << 1][1], s[x << 1 | 1][1]);
  }
  void build(int x, int l, int r) {
    if (l == r) {
      s[x][col[id[l]]] = dep[id[l]];
      s[x][col[id[l]] ^ 1] = -INF;
      return;
    }
    int mid = (l + r) >> 1;
    build(x << 1, l, mid);
    build(x << 1 | 1, mid + 1, r);
    up(x);
  }
  void update(int x, int l, int r, int L, int R) {
    if (l >= L && r <= R) {
      tag[x] ^= 1;
      swap(s[x][0], s[x][1]);
      return;
    }
    down(x);
    int mid = (l + r) >> 1;
    if (R <= mid)
      update(x << 1, l, mid, L, R);
    else if (L > mid)
      update(x << 1 | 1, mid + 1, r, L, R);
    else
      update(x << 1, l, mid, L, mid),
          update(x << 1 | 1, mid + 1, r, mid + 1, R);
    up(x);
  }
  void print(int x, int l, int r) {
    if (l == r) {
      cout << id[l] << ":" << s[x][0] << endl;
      return;
    }
    int mid = (l + r) >> 1;
    down(x);
    print(x << 1, l, mid);
    print(x << 1 | 1, mid + 1, r);
  }
} TA, TB;
signed main() {
  int n;
  read(n);
  for (int i = 1, u, v, c; i < n; ++i) {
    read(u), read(v), read(c);
    e[u].push_back(make_pair(v, c));
    e[v].push_back(make_pair(u, c));
    E[i] = make_pair(u, v);
  }
  DFN = MX = ID = col[1] = dep[1] = 0, dfsA(1, 0), A = ID;
  DFN = MX = ID = col[A] = dep[A] = 0, dfsA(A, 0), B = ID;
  TA.build(1, 1, n);
  for (int i = 1; i <= n; ++i) depA[i] = dep[i];
  DFN = MX = ID = col[B] = dep[B] = 0, dfsB(B, 0);
  TB.build(1, 1, n);
  for (int i = 1; i <= n; ++i) depB[i] = dep[i];
  int Case;
  read(Case);
  while (Case--) {
    int x;
    read(x);
    int u = E[x].first, v = E[x].second;
    if (depA[u] < depA[v]) swap(u, v);
    TA.update(1, 1, n, dfnA[u], fnsA[u]);
    if (depB[u] < depB[v]) swap(u, v);
    TB.update(1, 1, n, dfnB[u], fnsB[u]);
    print(max(TA.s[1][0], TB.s[1][0])), putc('\n');
  }
  return 0;
}