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

//package round679;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Set;

public class D2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int n = ni();
		int[] from = new int[n - 1];
		int[] to = new int[n - 1];
		int[] ws = new int[n-1];
		for (int i = 0; i < n - 1; i++) {
			from[i] = ni() - 1;
			to[i] = ni() - 1;
			ws[i] = ni();
		}
		int[][] g = packU(n, from, to);
		int[] cpar = buildCentroidTree(g);
		vimap = new int[21][n];
		vind = new int[21][n];
		vrights = new int[21][n];
		for(int i = 0;i < 21;i++)Arrays.fill(vimap[i], -1);
		for(int i = 0;i < 21;i++)Arrays.fill(vind[i], -1);
		for(int i = 0;i < 21;i++)Arrays.fill(vrights[i], -1);
		sts = new SegmentTreeRXQ[n][];
		Context cx = dfsTopCT(cpar, g);

		for(int i = 0;i < n-1;i++){
			if(ws[i] == 1){
				flip(i, from, to, cpar, cx);
			}
		}

		MinHeap[][] hs = new MinHeap[2][n]; // 01 sep
		MinHeap all = new MinHeap(n);
		for(int i = 0;i < n;i++){
			hs[0][i] = new MinHeap(cx.ctch[i].length+1);
			hs[1][i] = new MinHeap(cx.ctch[i].length);
			hs[0][i].add(cx.ctch[i].length, 0);
		}
		for(int i = 0;i < n;i++){
			for(int j = 0;j < cx.ctch[i].length;j++){
				hs[0][i].add(j, -sts[i][j].max0[1]);
				hs[1][i].add(j, -sts[i][j].max1[1]);
			}
			all.add(i, Math.min(sum2(hs[0][i]), sum2(hs[1][i])));
		}
//		tr(cpar);

		for(int Q = ni();Q > 0;Q--){
			int e = ni()-1;
			int[] touched = flip(e, from, to, cpar, cx);
			for(int sep : touched){
				int de = cx.cdep[sep];
				int ind = vind[de][to[e] == sep ? from[e] : to[e]];
//				tr(sep, -sts[sep][ind].max0[1], -sts[sep][ind].max1[1]);
				hs[0][sep].update(ind, -sts[sep][ind].max0[1]);
				hs[1][sep].update(ind, -sts[sep][ind].max1[1]);
				all.update(sep, Math.min(sum2(hs[0][sep]), sum2(hs[1][sep])));
			}
			out.println(-all.min());
		}
	}

	int[] flip(int i, int[] from, int[] to, int[] cpar, Context cx)
	{
		int f = from[i], t = to[i];
		int lca = lcanaive(f, t, cpar);
		int[] touched = new int[21];
		int p = 0;
		for(int j = cx.cdep[lca];j >= 0;j--, lca = cpar[lca]){
//			tr(lca, j);
			touched[p++] = lca;
			if(t == lca){
				assert vind[j][f] != -1;
				sts[lca][vind[j][f]].flip(vimap[j][f], vrights[j][f]);
			}else if(f == lca){
				assert vind[j][t] != -1;
				sts[lca][vind[j][t]].flip(vimap[j][t], vrights[j][t]);
			}else {
				assert vind[j][f] == vind[j][t];
				int can = vimap[j][f] < vimap[j][t] ? t : f;
				sts[lca][vind[j][t]].flip(vimap[j][can], vrights[j][can]);
			}
		}
		return Arrays.copyOf(touched, p);
	}

	int sum2(MinHeap h)
	{
		if(h.size() < 2)return Integer.MAX_VALUE / 2;
		int min = h.a[1] + h.a[2];
		if(h.a.length >= 4){
			min = Math.min(min, h.a[1] + h.a[3]);
		}
		return min;
	}

	public static class MinHeap {
		public int[] a;
		public int[] map;
		public int[] imap;
		public int n;
		public int pos;
		public static int INF = Integer.MAX_VALUE;

		public MinHeap(int m)
		{
			n = m+2;
			a = new int[n];
			map = new int[n];
			imap = new int[n];
			Arrays.fill(a, INF);
			Arrays.fill(map, -1);
			Arrays.fill(imap, -1);
			pos = 1;
		}

		public int add(int ind, int x)
		{
			int ret = imap[ind];
			if(imap[ind] < 0){
				a[pos] = x; map[pos] = ind; imap[ind] = pos;
				pos++;
				up(pos-1);
			}
			return ret != -1 ? a[ret] : x;
		}

		public int update(int ind, int x)
		{
			int ret = imap[ind];
			if(imap[ind] < 0){
				a[pos] = x; map[pos] = ind; imap[ind] = pos;
				pos++;
				up(pos-1);
			}else{
				int o = a[ret];
				a[ret] = x;
				up(ret);
				down(ret);
				//			if(a[ret] > o){
				//				up(ret);
				//			}else{
				//				down(ret);
				//			}
			}
			return x;
		}

		public int remove(int ind)
		{
			if(pos == 1)return INF;
			if(imap[ind] == -1)return INF;

			pos--;
			int rem = imap[ind];
			int ret = a[rem];
			map[rem] = map[pos];
			imap[map[pos]] = rem;
			imap[ind] = -1;
			a[rem] = a[pos];
			a[pos] = INF;
			map[pos] = -1;

			up(rem);
			down(rem);
			return ret;
		}

		public int min() { return a[1]; }
		public int argmin() { return map[1]; }
		public int size() {	return pos-1; }

		private void up(int cur)
		{
			for(int c = cur, p = c>>>1;p >= 1 && a[p] > a[c];c>>>=1, p>>>=1){
				int d = a[p]; a[p] = a[c]; a[c] = d;
				int e = imap[map[p]]; imap[map[p]] = imap[map[c]]; imap[map[c]] = e;
				e = map[p]; map[p] = map[c]; map[c] = e;
			}
		}

		private void down(int cur)
		{
			for(int c = cur;2*c < pos;){
				int b = a[2*c] < a[2*c+1] ? 2*c : 2*c+1;
				if(a[b] < a[c]){
					int d = a[c]; a[c] = a[b]; a[b] = d;
					int e = imap[map[c]]; imap[map[c]] = imap[map[b]]; imap[map[b]] = e;
					e = map[c]; map[c] = map[b]; map[b] = e;
					c = b;
				}else{
					break;
				}
			}
		}
	}


	public static int lcanaive(int x, int y, int[] par)
	{
		Set<Integer> ancs = new HashSet<>();
		for(int z = y;z != -1;z = par[z])ancs.add(z);
		for(int z = x;z != -1;z = par[z])if(ancs.contains(z))return z;
		return -1;
	}



	public class SegmentTreeRXQ {
		public int M, H, N;
		public boolean[] plus;
		public int[] max0;
		public int[] max1;

		public SegmentTreeRXQ(int n)
		{
			N = n;
			H = Integer.highestOneBit(Math.max(n,1))<<1;
			M = H<<1;
			plus = new boolean[H];
			max0 = new int[M];
			max1 = new int[M];
			Arrays.fill(max0, Integer.MIN_VALUE/3);
			Arrays.fill(max1, Integer.MIN_VALUE/3);
		}

		public SegmentTreeRXQ(int[] a)
		{
			this(a.length);
			for(int i = 0;i < a.length;i++){
				max0[H+i] = a[i];
				max1[H+i] = Integer.MIN_VALUE/3;
			}
			for(int i = H-1;i >= 1;i--)propagate(i);
		}

		private void propagate(int i)
		{
			if(plus[i]){
				max1[i] = Math.max(max0[2*i], max0[2*i+1]);
				max0[i] = Math.max(max1[2*i], max1[2*i+1]);
			}else{
				max0[i] = Math.max(max0[2*i], max0[2*i+1]);
				max1[i] = Math.max(max1[2*i], max1[2*i+1]);
			}
		}

		public void flip(int l, int r){ flip(l, r, 0, H, 1); }

		private void flip(int l, int r, int cl, int cr, int cur)
		{
			if(cur >= H){
				int d = max0[cur]; max0[cur] = max1[cur]; max1[cur] = d;
			}else if(l <= cl && cr <= r){
				plus[cur] ^= true;
				propagate(cur);
			}else{
				int mid = cl+cr>>>1;
				if(cl < r && l < mid){
					flip(l, r, cl, mid, 2*cur);
				}
				if(mid < r && l < cr){
					flip(l, r, mid, cr, 2*cur+1);
				}
				propagate(cur);
			}
		}

		public int[] count(int l, int r) { return l >= r ? null : count(l, r, 0, H, 1); }

		private int[] count(int l, int r, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				return new int[]{max0[cur], max1[cur]};
			}else{
				int mid = cl+cr>>>1;
				int[] L = null, R = null;
				if(cl < r && l < mid){
					L = count(l, r, cl, mid, 2*cur);
				}
				if(mid < r && l < cr){
					R = count(l, r, mid, cr, 2*cur+1);
				}
				if(plus[cur]) {
					if (L != null) {
						int d = L[0];
						L[0] = L[1];
						L[1] = d;
					}
					if (R != null) {
						int d = R[0];
						R[0] = R[1];
						R[1] = d;
					}
				}
				if(L == null)return R;
				if(R == null)return L;

				return new int[]{
						Math.max(L[9], R[0]),
						Math.max(L[1], R[1])
				};
			}
		}
	}


	public static int[] buildCentroidTree(int[][] g) {
		int n = g.length;
		int[] ctpar = new int[n];
		Arrays.fill(ctpar, -1);
		buildCentroidTree(g, 0, new boolean[n], new int[n], new int[n], new int[n], ctpar);
		return ctpar;
	}

	private static int buildCentroidTree(int[][] g, int root, boolean[] sed, int[] par, int[] ord, int[] des, int[] ctpar)
	{
		// parent and level-order
		ord[0] = root;
		par[root] = -1;
		int r = 1;
		for(int p = 0;p < r;p++) {
			int cur = ord[p];
			for(int nex : g[cur]){
				if(par[cur] != nex && !sed[nex]){
					ord[r++] = nex;
					par[nex] = cur;
				}
			}
		}
		// if(r == 1)return;

		// DP and find a separator
		int sep = -1; // always exists
		outer:
		for(int i = r-1;i >= 0;i--){
			int cur = ord[i];
			des[cur] = 1;
			for(int e : g[cur]){
				if(par[cur] != e && !sed[e])des[cur] += des[e];
			}
			if(r-des[cur] <= r/2){
				for(int e : g[cur]){
					if(par[cur] != e && !sed[e] && des[e] >= r/2+1)continue outer;
				}
				sep = cur;
				break;
			}
		}

		sed[sep] = true;
		for(int e : g[sep]){
			if(!sed[e])ctpar[buildCentroidTree(g, e, sed, par, ord, des, ctpar)] = sep;
		}
		return sep;
	}

	/**
	 * w: 頂点重み
	 * wt: weight table. wt[i] = (頂点iのsepからの重み合計。sepは含まない)
	 * dt: distance table. dt[i] = (頂点iのsepからの距離)
	 * vs: vertices. vssのテンポラリ
	 * vss: vertices. vss[j] = (j番目のneckにある頂点のpreorder)
	 * ctch: children of centroid tree
	 * stack, inds, rulers: テンポラリ
	 * @author uwi
	 *
	 */
	static class Context
	{
		boolean[] seps; // is separator?
		long[] wt; // [ind] = w between sep and ind
		int[] dt;
		int[] vs;
		int[][] vss; // [neckind][vertices]
		int[][] ctch;
		int[][] g;
		int[] stack;
		int[] inds;
		int[] rulers; // temp. [neckp] = separator
		int[] cpar;
		int[] cdep;
		int[] crights;
	}

	public Context dfsTopCT(int[] cpar, int[][] g) {
		int n = g.length;
		int ctroot = -1;
		for(int i = 0;i < n;i++)if(cpar[i] == -1)ctroot = i;

		Context cx = new Context();
		cx.cpar = cpar;
		cx.seps = new boolean[n];
		cx.wt = new long[n];
		cx.dt = new int[n];
		cx.vs = new int[n];
		cx.vss = new int[n][];
		cx.ctch = parentToChildren(cpar);
		cx.g = g;
		cx.stack = new int[n];
		cx.inds = new int[n];
		cx.rulers = new int[n];
		cx.cdep = new int[n];
		cx.crights = new int[n];
		dfs(ctroot, cx);
		return cx;
	}

	private void dfs(int sep, Context cx)
	{
		cx.seps[sep] = true;
		int neckp = 0;
		for(int neck : cx.g[sep]){
			if(cx.seps[neck])continue;

			int sp = 0;
			cx.inds[sp] = 0;
			//			cx.wt[neck] = w[neck];
			cx.dt[neck] = 1;
			int vsp = 0;
			cx.stack[sp++] = neck;
			while(sp > 0){
				int cur = cx.stack[sp-1];
				if(cx.inds[sp-1] == 0){
					cx.vs[vsp++] = cur;
					if(cx.cpar[cur] == sep)cx.rulers[neckp] = cur;
				}
				if(cx.inds[sp-1] == cx.g[cur].length){
					cx.crights[cur] = vsp;
					sp--;
					continue;
				}
				int e = cx.g[cur][cx.inds[sp-1]++];
				if(!cx.seps[e] && !(sp-2 >= 0 && e == cx.stack[sp-2])){
					//					cx.wt[e] = cx.wt[cur] + w[e];
					cx.dt[e] = sp + 1;
					cx.stack[sp] = e;
					cx.inds[sp] = 0;
					sp++;
				}
			}
			cx.vss[neckp] = Arrays.copyOf(cx.vs, vsp);
			neckp++;
		}

		process(sep, cx, Arrays.copyOf(cx.vss, neckp));

		for(int e : cx.ctch[sep]){
			cx.cdep[e] = cx.cdep[sep] + 1;
			dfs(e, cx);
		}
	}

	int[][] vimap;
	int[][] vind;
	int[][] vrights;
	SegmentTreeRXQ[][] sts;

	private void process(int sep, Context cx, int[][] vss)
	{
		sts[sep] = new SegmentTreeRXQ[vss.length];
		for(int i = 0;i < vss.length;i++){
			int[] ds = new int[vss[i].length];
			for(int j = 0;j < vss[i].length;j++){
				vimap[cx.cdep[sep]][vss[i][j]] = j;
				vind[cx.cdep[sep]][vss[i][j]] = i;
				vrights[cx.cdep[sep]][vss[i][j]] = cx.crights[vss[i][j]];
				ds[j] = cx.dt[vss[i][j]];
			}
			sts[sep][i] = new SegmentTreeRXQ(ds);
		}
	}

	public static int[][] parentToChildren(int[] par)
	{
		int n = par.length;
		int[] ct = new int[n];
		for(int i = 0;i < n;i++){
			if(par[i] >= 0){
				ct[par[i]]++;
			}
		}
		int[][] g = new int[n][];
		for(int i = 0;i < n;i++){
			g[i] = new int[ct[i]];
		}
		for(int i = 0;i < n;i++){
			if(par[i] >= 0){
				g[par[i]][--ct[par[i]]] = i;
			}
		}

		return g;
	}

	public static int[][] parents(int[][] g, int root) {
		int n = g.length;
		int[] par = new int[n];
		Arrays.fill(par, -1);

		int[] depth = new int[n];
		depth[0] = 0;

		int[] q = new int[n];
		q[0] = root;
		for (int p = 0, r = 1; p < r; p++) {
			int cur = q[p];
			for (int nex : g[cur]) {
				if (par[cur] != nex) {
					q[r++] = nex;
					par[nex] = cur;
					depth[nex] = depth[cur] + 1;
				}
			}
		}
		return new int[][]{par, q, depth};
	}


	public static int[][] packU(int n, int[] from, int[] to) {
		return packU(n, from, to, from.length);
	}

	public static int[][] packU(int n, int[] from, int[] to, int sup) {
		int[][] g = new int[n][];
		int[] p = new int[n];
		for (int i = 0; i < sup; i++) p[from[i]]++;
		for (int i = 0; i < sup; i++) p[to[i]]++;
		for (int i = 0; i < n; i++) g[i] = new int[p[i]];
		for (int i = 0; i < sup; i++) {
			g[from[i]][--p[from[i]]] = to[i];
			g[to[i]][--p[to[i]]] = from[i];
		}
		return g;
	}


	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}
	
	public static void main(String[] args) throws Exception { new D2().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
