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 JAVA):

Solution:
//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.InputMismatchException;

public class D4 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		// 直径の端点を出す
		// 各クエリに対し、端点をrootにしたところから最大点をとりだす

		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[][] pars = parents(g, 0);
//		int[] par = pars[0], ord = pars[1], dep = pars[2];
		int[] dia = diameter(g);

		int[] ends = {dia[1], dia[2]};
		int[][] ppar = new int[2][];
		int[][] iords = new int[2][];
		int[][] rights = new int[2][];
		SegmentTreeRXQ[] sts = new SegmentTreeRXQ[2];
		int p = 0;
		for(int end : ends){
			int[][] pars = parents(g, end);
			int[] par = pars[0], dep = pars[2];
			int[][] rs = makeRights(g, par, end);
			int[] iord = rs[1], right = rs[2];
			ppar[p] = par;
			iords[p] = iord;
			rights[p] = right;
			int[] deps = new int[n];
			for(int i = 0;i < n;i++){
				deps[iord[i]] = dep[i];
			}
			SegmentTreeRXQ st = new SegmentTreeRXQ(deps);
			for(int i = 0;i < n-1;i++){
				if(ws[i] == 1) {
					int un = par[from[i]] == to[i] ? from[i] : to[i];
					st.flip(iord[un], right[iord[un]]+1);
				}
			}
			sts[p] = st;
			p++;
		}

		for(int Q = ni();Q > 0;Q--){
			int i = ni()-1;
//			ws[i] ^= 1;
			int ans = 0;
			for(int k = 0;k < 2;k++) {
				int un = ppar[k][from[i]] == to[i] ? from[i] : to[i];
				sts[k].flip(iords[k][un], rights[k][iords[k][un]] + 1);
				ans = Math.max(ans, sts[k].max0[1]);
			}
			out.println(ans);
		}
	}

	public static int[] sortByPreorder(int[][] g, int root){
		int n = g.length;
		int[] stack = new int[n];
		int[] ord = new int[n];
		boolean[] ved = new boolean[n];
		stack[0] = root;
		int p = 1;
		int r = 0;
		ved[root] = true;
		while(p > 0){
			int cur = stack[p-1];
			ord[r++] = cur;
			p--;
			for(int e : g[cur]){
				if(!ved[e]){
					ved[e] = true;
					stack[p++] = e;
				}
			}
		}
		return ord;
	}

	public static class SegmentTreeRXQ {
		public final int M, H, N, LH;
		public boolean[] plus;
		public int[] max0;
		public int[] max1;
		public static final int I = Integer.MAX_VALUE/3;

		public SegmentTreeRXQ(int n)
		{
			N = n;
			M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
			H = M>>>1;
			LH = Integer.numberOfTrailingZeros(H);
			plus = new boolean[H];
			max0 = new int[M];
			max1 = new int[M];
			Arrays.fill(max0, -I);
			Arrays.fill(max1, -I);
		}

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

		private void push(int cur, int len)
		{
			if(!plus[cur])return;
			int L = cur*2, R = L + 1;
			{int d = max0[L]; max0[L] = max1[L]; max1[L] = d;}
			{int d = max0[R]; max0[R] = max1[R]; max1[R] = d;}
			if(L < H){
				plus[L] ^= true;
				plus[R] ^= true;
			}
			plus[cur] = false;
		}

		private void flip1(int cur, int q)
		{
			{int d = max0[cur]; max0[cur] = max1[cur]; max1[cur] = d;}
			if(cur < H) {
				plus[cur] ^= true;
			}
		}

		private void pushlr(int l, int r)
		{
			for(int i = LH;i >= 1;i--) {
				if (l >>> i << i != l) push(l >>> i, i);
				if (r >>> i << i != r) push(r >>> i, i);
			}
		}

		public void flip(int l, int r) {
			if(l >= r)return;
			l += H; r += H;
			pushlr(l, r);
			for(int ll = l, rr = r, q = 0;ll < rr;ll>>>=1,rr>>>=1, q++){
				if((ll&1) == 1) flip1(ll++, q);
				if((rr&1) == 1) flip1(--rr, q);
			}
			for(int i = 1;i <= LH;i++){
				if(l>>>i<<i != l)propagate(l>>>i);
				if(r>>>i<<i != r)propagate(r>>>i);
			}
		}

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

	/**
	 * ルートrootの木gに対し、行きがけ順で訪れたときの各ノードの子孫を表す範囲の終端を計算する。
	 * 格納はソート順で行われている。ルートが[0]に対応する。
	 * [ord, iord, right]
	 *
	 * @usage ノード番号xに対して対応する範囲は[iord[x], right[iord[x]]].
	 * @param g
	 * @param par
	 * @param root
	 * @return
	 */
	public static int[][] makeRights(int[][] g, int[] par, int root)
	{
		int n = g.length;
		int[] ord = sortByPreorder(g, root);
		int[] iord = new int[n];
		for(int i = 0;i < n;i++)iord[ord[i]] = i;

		int[] right = new int[n];
		for(int i = n-1;i >= 1;i--){
			if(right[i] == 0)right[i] = i;
			int p = iord[par[ord[i]]];
			right[p] = Math.max(right[p], right[i]);
		}
		return new int[][]{ord, iord, right};
	}


	public static int[] diameter(int[][] g)
	{
		int n = g.length;
		int f0 = -1, f1 = -1, d01 = -1;
		int[] q = new int[n];
		boolean[] ved = new boolean[n];
		{
			int qp = 0;
			q[qp++] = 0; ved[0] = true;
			for(int i = 0;i < qp;i++){
				int cur = q[i];
				for(int e : g[cur]){
					if(!ved[e]){
						ved[e] = true;
						q[qp++] = e;
						continue;
					}
				}
			}
			f0 = q[n-1];
		}
		{
			int[] d = new int[n];
			int qp = 0;
			Arrays.fill(ved, false);
			q[qp++] = f0; ved[f0] = true;
			for(int i = 0;i < qp;i++){
				int cur = q[i];
				for(int e : g[cur]){
					if(!ved[e]){
						ved[e] = true;
						q[qp++] = e;
						d[e] = d[cur] + 1;
						continue;
					}
				}
			}
			f1 = q[n-1];
			d01 = d[f1];
		}
		return new int[]{d01, f0, f1};
	}


	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 D4().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)); }
}