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 int N = 500000;
int s[N], t[N], type[N];
int q, qind[N];
void dfs(int n, vector<vector<int>>& adj, int root, void preVisit(int, int) = 0,
         void postVisit(int, int) = 0, void traverseTreeEdge(int, int, int) = 0,
         void traverseBackEdge(int, int) = 0) {
  vector<int> parent(n, -2);
  vector<int> stackNode(n);
  vector<int> stackEdgeIndex(n);
  int stackSize = 1;
  stackNode[0] = root;
  stackEdgeIndex[0] = 0;
  parent[root] = -1;
  while (stackSize > 0) {
    int node = stackNode[stackSize - 1];
    int edgeIndex = stackEdgeIndex[stackSize - 1];
    if (edgeIndex == 0 && preVisit) {
      preVisit(node, parent[node]);
    }
    if (edgeIndex == adj[node].size()) {
      stackSize--;
      if (postVisit) {
        postVisit(node, parent[node]);
      }
    } else {
      int e = adj[node][edgeIndex];
      int other = s[e] == node ? t[e] : s[e];
      stackEdgeIndex[stackSize - 1]++;
      if (parent[other] == -2) {
        parent[other] = node;
        if (traverseTreeEdge) traverseTreeEdge(node, other, e);
        stackNode[stackSize] = other;
        stackEdgeIndex[stackSize] = 0;
        stackSize++;
      } else if (other != parent[node]) {
        if (traverseBackEdge) traverseBackEdge(node, other);
      }
    }
  }
}
int depth[N];
bool good[N];
int preorder[N], postorder[N];
int subtreeRoot[N];
int curorder;
void preVisit(int v, int p) {
  preorder[v] = curorder++;
  if (p == -1) {
    depth[v] = 0;
    good[v] = true;
  } else {
    depth[v] = depth[p] + 1;
  }
}
void postVisit(int v, int p) { postorder[v] = curorder - 1; }
void traverseTreeEdge(int p, int v, int e) {
  if (type[e])
    good[v] = !good[p];
  else
    good[v] = good[p];
  subtreeRoot[e] = v;
}
int getFurthest(int n, vector<vector<int>>& adj, int root) {
  dfs(n, adj, root, preVisit);
  int ans = 0;
  for (int i = 1; i < n; i++)
    if (depth[i] > depth[ans]) ans = i;
  return ans;
}
int ans[N];
struct Node {
  Node* left = 0;
  Node* right = 0;
  int leftValue;
  int rightValue;
  bool inverted = false;
  int goodDepth = 0, badDepth = 0;
};
class SegmentTree {
 public:
  SegmentTree(int n) { root = buildTree(0, n - 1); }
  ~SegmentTree() { dispose(root); }
  void dispose(Node* node) {
    if (!node) return;
    dispose(node->left);
    dispose(node->right);
    delete node;
  }
  Node* buildTree(int l, int r) {
    Node* node = new Node();
    node->leftValue = l;
    node->rightValue = r;
    if (l < r) {
      int mid = (l + r) / 2;
      node->left = buildTree(l, mid);
      node->right = buildTree(mid + 1, r);
    }
    return node;
  }
  void setDepth(int ind, int depth, bool good) {
    setDepth(root, ind, depth, good);
  }
  void setDepth(Node* node, int ind, int depth, bool good) {
    if (node->rightValue == ind && node->leftValue == ind) {
      if (good)
        node->goodDepth = depth;
      else
        node->badDepth = depth;
      return;
    }
    if (ind <= node->left->rightValue)
      setDepth(node->left, ind, depth, good);
    else
      setDepth(node->right, ind, depth, good);
    node->goodDepth = max(node->left->goodDepth, node->right->goodDepth);
    node->badDepth = max(node->left->badDepth, node->right->badDepth);
  }
  void addSegment(int left, int right) { addSegment(root, left, right); }
  void pushLazy(Node* node) {
    if (node->inverted) {
      swap(node->goodDepth, node->badDepth);
      if (node->left) node->left->inverted = !node->left->inverted;
      if (node->right) node->right->inverted = !node->right->inverted;
      node->inverted = false;
    }
  }
  void addSegment(Node* node, int left, int right) {
    if (node->rightValue < left || node->leftValue > right) return;
    if (left <= node->leftValue && right >= node->rightValue) {
      node->inverted = !node->inverted;
      return;
    }
    pushLazy(node);
    addSegment(node->left, left, right);
    addSegment(node->right, left, right);
    node->goodDepth = node->badDepth = 0;
    if (node->left) {
      pushLazy(node->left);
      node->goodDepth = node->left->goodDepth;
      node->badDepth = node->left->badDepth;
    }
    if (node->right) {
      pushLazy(node->right);
      node->goodDepth = max(node->goodDepth, node->right->goodDepth);
      node->badDepth = max(node->badDepth, node->right->badDepth);
    }
  }
  int query(int left, int right) { return query(root, left, right); }
  int query(Node* node, int left, int right) {
    if (!node || node->rightValue < left || node->leftValue > right) return 0;
    pushLazy(node);
    if (left <= node->leftValue && right >= node->rightValue) {
      return node->goodDepth;
    }
    return max(query(node->left, left, right), query(node->right, left, right));
  }
  Node* root;
};
void solve(int n, vector<vector<int>>& adj, int root) {
  curorder = 0;
  dfs(n, adj, root, preVisit, postVisit, traverseTreeEdge);
  SegmentTree tree(n);
  for (int i = 0; i < n; i++) tree.setDepth(preorder[i], depth[i], good[i]);
  for (int i = 0; i < q; i++) {
    int v = subtreeRoot[qind[i]];
    tree.addSegment(preorder[v], postorder[v]);
    ans[i] = max(ans[i], tree.query(0, n - 1));
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> adj(n);
  for (int i = 0; i < n - 1; i++) {
    cin >> s[i] >> t[i] >> type[i];
    s[i]--;
    t[i]--;
    adj[s[i]].push_back(i);
    adj[t[i]].push_back(i);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> qind[i];
    qind[i]--;
  }
  int v1 = getFurthest(n, adj, 0);
  int v2 = getFurthest(n, adj, v1);
  memset(ans, 0, sizeof(ans));
  solve(n, adj, v1);
  solve(n, adj, v2);
  for (int i = 0; i < q; i++) cout << ans[i] << "\n";
  return 0;
}