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.
10
5 7 0
2 10 1
1 5 0
6 8 0
4 9 1
2 5 1
10 8 0
2 3 1
4 2 1
10
9
9
9
5
2
5
7
2
3
2
 5
2 4 0
5 2 0
1 3 1
1 2 1
5
3
3
4
1
1
 20
9 11 0
1 5 0
1 14 0
5 17 0
14 3 0
16 6 0
9 2 0
6 4 0
11 10 0
12 7 0
8 18 0
13 1 0
18 11 0
8 6 0
5 10 0
5 15 0
2 20 0
19 17 0
7 17 0
20
9
11
11
14
14
5
5
10
10
4
4
18
17
18
17
17
8
8
15
17
 2
2 1 1
10
1
1
1
1
1
1
1
1
1
1

5
5
5
5
5
4
5
4
5
5
 2
3
2
3
2
 7
9
7
9
7
9
7
9
7
9
7
8
8
8
7
8
9
8
9
9
 1
0
1
0
1
0
1
0
1
0

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
#ifdef HITONANODE_LOCAL
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl
#else
#define dbg(x) {}
#endif


using Edges = vector<vector<pint>>;
// rから各ノードへの距離
vector<int> tree_distance(int N, int r, Edges &e)
{
    vector<int> res(N); // res[r] = 0;
    queue<pint> q;
    q.push(pint(r, -1)); // (now, prev)
    while(!q.empty())
    {
        pint pa = q.front(); q.pop();
        if (pa.second >= 0) res[pa.first] = res[pa.second] + 1;
        for (auto [nx, d] : e[pa.first]) if (nx != pa.second) q.push(pint(nx, pa.first));
    }
    return res;
}
// 木の直径 rは実在するノードの番号であればよい
pint tree_diameter(int N, int r, Edges &e)
{
    vector<int> d = tree_distance(N, r, e);
    int s = max_element(d.begin(), d.end()) - d.begin();
    d = tree_distance(N, s, e);
    int t = max_element(d.begin(), d.end()) - d.begin();
    return make_pair(s, t);
}

using VAL = pair<int, int>;

template <uint32_t len>
struct RandomizedBinarySearchTree {
    // Do your RuBeSTy! ⌒°( ・ω・)°⌒
    /*
    struct rand_int_ // non-deterministic
    {
        mt19937 mt;
        rand_int_() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
        lint operator()(lint x) { return this->operator()(0, x); } // [0, x)
        lint operator()(lint l, lint r) {
            uniform_int_distribution<lint> d(l, r - 1);
            return d(mt);
        }
        lint operator()() { return this->operator()(1e8); }
    } _rand;
    /*/
    inline uint32_t _rand() // XorShift
    {
        static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123;
        uint32_t t = x ^ (x << 11);
        x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    }
    // */

    // 各ノードに持たせるデータ型と代数構造
    struct Node
    {
        Node *l, *r;
        uint32_t sz; // 自身を頂点とする部分木のサイズ
        VAL val; // 自身がrootの部分木を記述, dval==Idvalのときのみ単独で意味を持つ
        Node(const VAL &v)
        : l(nullptr), r(nullptr), sz(1), val(v) {}
        Node() {}
    };
    inline Node *_revise_val(Node *t) noexcept // （tの子に関する外的操作後に呼んで）szとvalを適切に直す
    {
        if (t)
        {
            t->sz = size(t->l) + size(t->r) + 1;
            t->val.second = t->val.first;
            if (t->l) chmax(t->val.second, t->l->val.second);
            if (t->r) chmax(t->val.second, t->r->val.second);
        };
        return t;
    }
    vector<Node> data;
    uint32_t d_ptr;

    RandomizedBinarySearchTree()
    : d_ptr(0) { data.resize(len); }

    Node *new_tree() { return nullptr; } // 新たな木を作成
    static inline uint32_t size(const Node *t) { return t ? t->sz : 0; }
    inline int mem_used() { return (int)d_ptr; }
    inline bool empty(Node *t) { return !t; }
    inline Node *_make_node(const VAL &val) { if (d_ptr >= len) exit(1); return &(data[d_ptr++] = Node(val)); }
    virtual void _duplicate_node(Node *&) {}


    // lとrをrootとする木同士を結合して，新たなrootを返す
    Node *merge(Node *l, Node *r) noexcept
    {
        if (l == nullptr || r == nullptr) return l ? l : r;
        if (_rand() % (l->sz + r->sz) < l->sz)
        {
            l->r = merge(l->r, r);
            return _revise_val(l);
        }
        else
        {
            r->l = merge(l, r->l);
            return _revise_val(r);
        }
    }

    // [0, k)の木と[k, root->size())の木に分けて各root
    // （部分木の要素数が0ならnullptr）を返す
    pair<Node*, Node*> split(Node *&root, int k) noexcept // rootの子孫からあとk個欲しい
    {
        if (root == nullptr) return make_pair(nullptr, nullptr);
        if (k == 0) return make_pair(nullptr, root);
        if (k <= (int)size(root->l)) // leftからk個拾える
        {
            auto p = split(root->l, k);
            root->l = p.second;
            return make_pair(p.first, _revise_val(root));
        }
        else
        {
            auto p = split(root->r, k - size(root->l) - 1);
            root->r = p.first;
            return make_pair(_revise_val(root), p.second);
        }
    }

    // 0-indexedでarray[pos]の手前に新たな要素newvalを挿入する
    void insert(Node *&root, int pos, const VAL &newval) noexcept
    {
        auto p = split(root, pos);
        root = merge(p.first, merge(_make_node(newval), p.second));
    }

    // 0-indexedでarray[pos]を削除する（先頭からpos+1個目の要素）
    void erase(Node *&root, int pos) noexcept
    {
        auto p = split(root, pos);
        auto p2 = split(p.second, 1);
        root = merge(p.first, p2.second);
    }

    // array[pos].valを取得する
    Node range_get(Node *&root, int l, int r) noexcept
    {
        auto p = split(root, l);
        auto p2 = split(p.second, r - l);
        Node res = *p2.first;
        root = merge(p.first, merge(p2.first, p2.second));
        return res;
    }
    Node get(Node *&root, int pos)
    {
        return range_get(root, pos, pos + 1);
    }

    // データを壊して新規にinitの内容を詰める
    void assign(Node *&root, const vector<VAL> &init)
    {
        d_ptr = 0;
        int N = init.size();
        root = N ? _assign_range(0, N, init) : new_tree();
    }
    Node *_assign_range(int l, int r, const vector<VAL> &init)
    {
        if (r - l == 1) {
            auto t = _make_node(init[l]);
            return _revise_val(t);
        }
        return merge(_assign_range(l, (l + r) / 2, init), _assign_range((l + r) / 2, r, init));
    }
};

vector<pint> edges;
vector<vector<pint>> to;
vector<int> query;
vector<int> solve(int N, int U)
{
    vector<int> par(N, -1);
    vector<int> dinit(N, 0);
    vector<int> depth(N, 0);

    vector<int> vin(N);
    vector<int> vout(N);
    int cur = 0;
    vector<int> cur2i(N);

    auto dfs = [&](auto&& dfs, int now, int prv, int dnow, int D) -> void {
        par[now] = prv;
        dinit[now] = dnow;
        vin[now] = cur;
        cur2i[cur] = now;
        cur++;
        depth[now] = D;
        for (auto [nxt, d] : to[now]) {
            if (nxt != prv) {
                dfs(dfs, nxt, now, dnow ^ d, D + 1);
            }
        }
        vout[now] = cur;
    };

    dfs(dfs, U, -1, 0, 0);

    using RBST = RandomizedBinarySearchTree<500010 * 2>;
    RBST rbst({});
    auto t0 = rbst.new_tree();
    auto t1 = rbst.new_tree();
    REP(t, N) {
        int i = cur2i[t];
        rbst.insert(t0, t, dinit[i] == 0 ? make_pair(depth[i], depth[i]) : make_pair(-1, -1));
        rbst.insert(t1, t, dinit[i] == 1 ? make_pair(depth[i], depth[i]) : make_pair(-1, -1));
    }

    vector<int> ret;
    int nq = 0;
    for (auto e : query) {
        nq++;
        e--;
        auto [a, b] = edges[e];
        int c = a;
        if (par[b] == a) c = b;
        const int L = vin[c], R = vout[c];
        auto [L0, C0t] = rbst.split(t0, L);
        auto [C0, R0] = rbst.split(C0t, R - L);

        auto [L1, C1t] = rbst.split(t1, L);
        auto [C1, R1] = rbst.split(C1t, R - L);

        t0 = rbst.merge(L0, rbst.merge(C1, R0));
        t1 = rbst.merge(L1, rbst.merge(C0, R1));
        ret.emplace_back(t0->val.second);
    }
    return ret;
}

int main()
{
    int N;
    cin >> N;
    edges.resize(N - 1);
    to.resize(N);
    vector<int> c(N - 1);
    REP(i, N - 1)
    {
        int u, v;
        cin >> u >> v >> c[i];
        u--, v--;
        edges[i] = make_pair(u, v);
        to[u].emplace_back(v, c[i]), to[v].emplace_back(u, c[i]);
    }

    const auto [U, V] = tree_diameter(N, 0, to);
    int Q;
    cin >> Q;
    query.resize(Q);
    cin >> query;
    vector<int> solve1 = solve(N, U);
    // vector<int> solve2 = solve(N, V);
    // REP(q, Q) cout << max(solve1[q], solve2[q]) << '\n';
    REP(q, Q) cout << solve1[q] << '\n';
}
