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 double eps = 1e-8;
const long long INF64 = 1e18;
const double INF = 2e9 + 63;
const long long mod = 1e9 + 7;
const int maxn = 3e3 + 79;
const long long sigma = 27;
const double pi = acos(-1);
struct VertVal {
  long long lowest0;
  long long lowest1;
};
template <typename T, typename PushT>
struct RMQ {
  vector<T> mas;
  T neutral;
  int size;
  std::function<T(const T&, const T&)> op;
  vector<PushT> masPush;
  PushT neutralPush;
  std::function<pair<PushT, PushT>(const PushT&)> opPush;
  std::function<void(PushT&, T&)> opApply;
  std::function<PushT(const PushT&, const PushT&)> opPendingCompose;
  RMQ(int size, std::function<T(const T&, const T&)> op, T neutral,
      std::function<pair<PushT, PushT>(const PushT&)> opPush, PushT neutralPush,
      std::function<void(PushT&, T&)> opApply,
      std::function<PushT(const PushT&, const PushT&)> opPendingCompose) {
    this->neutral = neutral;
    this->size = size;
    this->op = op;
    mas.assign(4 * size, neutral);
    this->opPush = opPush;
    this->opApply = opApply;
    this->opPendingCompose = opPendingCompose;
    this->neutralPush = neutralPush;
    masPush.assign(4 * size, neutralPush);
  }
  void setVal(int pos, T val) { setValImpl(pos, val, 0, size - 1, 1); }
  void setValImpl(int pos, T val, int l, int r, int v) {
    if (l == r && r == pos) {
      mas[v] = val;
      return;
    }
    if (l > pos || r < pos) return;
    int m = (l + r) / 2;
    setValImpl(pos, val, l, m, 2 * v);
    setValImpl(pos, val, m + 1, r, 2 * v + 1);
    mas[v] = op(mas[2 * v], mas[2 * v + 1]);
  }
  void modify(int l, int r, PushT d) { modifyImpl(l, r, d, 0, size - 1, 1); }
  void push(int v, bool isLeaf) {
    if (!isLeaf) {
      auto p = opPush(masPush[v]);
      masPush[2 * v] = opPendingCompose(masPush[2 * v], p.first);
      masPush[2 * v + 1] = opPendingCompose(masPush[2 * v + 1], p.second);
    }
    opApply(masPush[v], mas[v]);
  }
  void modifyImpl(int rq_l, int rq_r, long long d, int l, int r, int v) {
    push(v, l == r);
    if (rq_l > r || l > rq_r) return;
    if (rq_l <= l && r <= rq_r) {
      masPush[v] = opPendingCompose(masPush[v], d);
      push(v, l == r);
      return;
    }
    int m = (l + r) / 2;
    modifyImpl(rq_l, rq_r, d, l, m, 2 * v);
    modifyImpl(rq_l, rq_r, d, m + 1, r, 2 * v + 1);
    mas[v] = op(mas[2 * v], mas[2 * v + 1]);
  }
  T query(int l, int r) { return queryImpl(l, r, 0, size - 1, 1); }
  T queryImpl(int rq_l, int rq_r, int l, int r, int v) {
    push(v, l == r);
    if (rq_l > r || l > rq_r) return neutral;
    if (rq_l <= l && r <= rq_r) {
      return mas[v];
    }
    int m = (l + r) / 2;
    T ans1 = queryImpl(rq_l, rq_r, l, m, 2 * v);
    T ans2 = queryImpl(rq_l, rq_r, m + 1, r, 2 * v + 1);
    return op(ans1, ans2);
  }
};
struct Edge {
  int to;
  int getTo() const { return to; }
  long long getW() const { return 1; }
};
struct WEdge {
  int to;
  long long w;
  int getTo() const { return to; }
  long long getW() const { return w; }
};
struct None {};
template <typename EdgeType, typename VertType = None>
struct Graph {
  vector<VertType> v;
  vector<vector<EdgeType>> g;
  Graph(int n) : g(n), v(n){};
  void addEdge(int from, int to, EdgeType info) {
    info.to = to;
    g[from].push_back(info);
    info.to = from;
    g[to].push_back(info);
  }
  void addOEdge(int from, int to, EdgeType info) {
    info.to = to;
    g[from].push_back(info);
  }
};
struct PFInfo {
  int par;
  long long dist;
  friend bool operator<(const PFInfo& a, const PFInfo& b) {
    return a.dist < b.dist;
  }
};
template <typename EdgeType, typename VertType>
vector<PFInfo> bfs(const Graph<EdgeType, VertType>& g, int start) {
  vector<PFInfo> res(g.g.size(), {-1, INF64});
  queue<int> s;
  res[start].par = -2;
  res[start].dist = 0;
  s.push(start);
  while (!s.empty()) {
    int v = s.front();
    s.pop();
    for (auto& it : g.g[v]) {
      int to = it.getTo();
      if (res[to].dist == INF64) {
        res[to].par = v;
        res[to].dist = res[v].dist + it.getW();
        s.push(to);
      }
    }
  }
  return res;
}
vector<int> restorePath(const vector<PFInfo>& pfInfo, int to) {
  vector<int> res;
  while (true) {
    res.push_back(to);
    if (pfInfo[to].par == -1) {
      return {};
    }
    if (pfInfo[to].par == -2) {
      return res;
    }
    to = pfInfo[to].par;
  }
}
struct MyEdge : Edge {
  int roadType;
  MyEdge(int roadType) : roadType(roadType){};
};
int cnt = 0;
void dfs(Graph<MyEdge>& g, vector<pair<int, int>>& segs, vector<int>& oddness,
         vector<int>& depths, int v = 0, int curCnt = 0, int depth = 0,
         int p = -1) {
  oddness[cnt] = curCnt;
  depths[cnt] = depth;
  int start = cnt++;
  for (auto& it : g.g[v]) {
    int to = it.getTo();
    if (to == p) continue;
    dfs(g, segs, oddness, depths, to, curCnt ^ it.roadType, depth + 1, v);
  }
  segs[v] = {start, cnt - 1};
}
void solve() {
  int n;
  cin >> n;
  Graph<MyEdge> g(n);
  vector<pair<int, int>> edges;
  for (int i = 0; i < n - 1; i++) {
    int u, v, t;
    cin >> u >> v >> t;
    u--;
    v--;
    g.addEdge(u, v, {t});
    edges.push_back({u, v});
  }
  auto dists = bfs(g, 0);
  int end1 = max_element(dists.begin(), dists.end()) - dists.begin();
  auto dists1 = bfs(g, end1);
  int end2 = max_element(dists1.begin(), dists1.end()) - dists1.begin();
  auto dists2 = bfs(g, end2);
  vector<pair<int, int>> seqFrom1(n), seqFrom2(n);
  vector<int> oddnessFrom1(n), oddnessFrom2(n);
  vector<int> depths1(n), depths2(n);
  cnt = 0;
  dfs(g, seqFrom1, oddnessFrom1, depths1, end1);
  assert(cnt == n);
  RMQ<VertVal, int> rmq1(
      n,
      [](const VertVal& a, const VertVal& b) {
        return VertVal{max(a.lowest0, b.lowest0), max(a.lowest1, b.lowest1)};
      },
      VertVal{-1, -1},
      [](const int& par) { return pair<bool, bool>(par, par); }, false,
      [](int& a, VertVal& b) {
        if (a) swap(b.lowest0, b.lowest1);
        a = false;
      },
      [](const int& a, const int& b) { return a ^ b; });
  for (int i = 0; i < n; i++) {
    if (oddnessFrom1[i]) {
      rmq1.setVal(i, {-1, depths1[i]});
    } else {
      rmq1.setVal(i, {depths1[i], -1});
    }
  }
  cnt = 0;
  dfs(g, seqFrom2, oddnessFrom2, depths2, end2);
  assert(cnt == n);
  RMQ<VertVal, int> rmq2(
      n,
      [](const VertVal& a, const VertVal& b) {
        return VertVal{max(a.lowest0, b.lowest0), max(a.lowest1, b.lowest1)};
      },
      VertVal{-1, -1},
      [](const int& par) { return pair<bool, bool>(par, par); }, false,
      [](int& a, VertVal& b) {
        if (a) swap(b.lowest0, b.lowest1);
        a = false;
      },
      [](const int& a, const int& b) { return a ^ b; });
  auto tmp = rmq1.query(0, 1);
  for (int i = 0; i < n; i++) {
    if (oddnessFrom2[i]) {
      rmq2.setVal(i, {-1, depths2[i]});
    } else {
      rmq2.setVal(i, {depths2[i], -1});
    }
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int id;
    cin >> id;
    id--;
    auto p = edges[id];
    int u = p.first;
    int v = p.second;
    int root = -1;
    if (dists1[u].par == v) {
      root = u;
    } else if (dists1[v].par == u) {
      root = v;
    } else
      assert(false);
    auto seg = seqFrom1[root];
    rmq1.modify(seg.first, seg.second, true);
    root = -1;
    if (dists2[u].par == v) {
      root = u;
    } else if (dists2[v].par == u) {
      root = v;
    } else
      assert(false);
    seg = seqFrom2[root];
    rmq2.modify(seg.first, seg.second, true);
    auto res1 = rmq1.query(0, n - 1);
    auto res2 = rmq2.query(0, n - 1);
    long long res = max(res1.lowest0, res2.lowest0);
    cout << res << "\n";
  }
}
int main(int argc, char** argv) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(2) << fixed;
  int t;
  solve();
  return 0;
}