Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.

The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.

Mike wants to do the following operations with the tree: 

  1. Fill vertex v with water. Then v and all its children are filled with water. 
  2. Empty vertex v. Then v and all its ancestors are emptied. 
  3. Determine whether vertex v is filled with water at the moment. 

Initially all vertices of the tree are empty.

Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 500000) — the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the edges of the tree.

The next line contains a number q (1 ≤ q ≤ 500000) — the number of operations to perform. Each of the following q lines contains two space-separated numbers ci (1 ≤ ci ≤ 3), vi (1 ≤ vi ≤ n), where ci is the operation type (according to the numbering given in the statement), and vi is the vertex on which the operation is performed.

It is guaranteed that the given graph is a tree.

Output

For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.

Examples

Input

5
1 2
5 1
2 3
4 2
12
1 1
2 3
3 1
3 2
3 3
3 4
1 2
2 4
3 1
3 3
3 4
3 5


Output

0
0
0
1
0
1
0
1
import java.util.*;
import java.io.*;
import java.lang.Math.*;

public class MainA {

	private int V; 
	int time;
    private LinkedList<Integer> adj[];
    MainA(int v)
    {
        V = v;
        time=0;
        adj = new LinkedList[v];
        for (int i=0; i<v; ++i)
            adj[i] = new LinkedList();
    }
   
    void addEdge(int v, int w)
    {
        adj[v].add(w);
        adj[w].add(v);
    }
   
    void dfs(int v,boolean visited[],int arrival[],int dep[],int parent[])
    {
        visited[v] = true;
        arrival[v]=time++;
        Iterator<Integer> i = adj[v].listIterator();
        while (i.hasNext())
        {
            int n = i.next();
            if (!visited[n])
            {  
            	parent[n]=v;
            	dfs(n,visited,arrival,dep,parent);
            }
        }
        dep[v]=time-1;
    }
   
	public static void main(String[] args) {

		InputReader in = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n=in.nextInt();
		MainA g=new MainA(n);
		for(int i=0;i<n-1;i++)
		{
			int x=in.nextInt()-1;
			int y=in.nextInt()-1;
			g.addEdge(x, y);
		}
		int arrival[]=new int[n];
		int dep[]=new int[n];
		boolean visited[]=new boolean[n];
		int parent[]=new int[n];
		
		g.dfs(0,visited,arrival,dep,parent);
		int q=in.nextInt();
		while(q-->0)
		{
			int type=in.nextInt();
			int ver=in.nextInt()-1;
			if(type==1)
			{
				if(g.query(n,arrival[ver],dep[ver])!=dep[ver]-arrival[ver]+1)
				{
					g.updateRange(n,arrival[parent[ver]],arrival[parent[ver]],0);
				}
				g.updateRange(n,arrival[ver],dep[ver],1);
				
			}
			else if(type==2)
			{
				g.updateRange(n,arrival[ver],arrival[ver],0);
			}
			else
			{
				int ans=g.query(n,arrival[ver],dep[ver]);
				if(ans==dep[ver]-arrival[ver]+1)out.println(1);
				else out.println(0);
			}
		}
		
		out.close();
	}

	final int MAX = 10000000;        // Max tree size
    int tree[] = new int[MAX];  // To store segment tree
    int lazy[] = new int[MAX];  // To store pending updates
 
    void updateRangeUtil(int si, int ss, int se, int us,int ue, int diff)
    {
        if (lazy[si] != 0)
        {
            tree[si] = (se - ss + 1) * lazy[si];
            if (ss != se)
            {
                lazy[si * 2 + 1] = lazy[si];
                lazy[si * 2 + 2] = lazy[si];
            }
            lazy[si] = 0;
        }
        if (ss > se || ss > ue || se < us)
            return;
        if (ss >= us && se <= ue)
        {
            tree[si] = (se - ss + 1) * diff;
            if (ss != se)
            {
            	lazy[si * 2 + 1] = diff;
                lazy[si * 2 + 2] = diff;
            }
            return;
        }
 
        int mid = (ss + se) / 2;
        updateRangeUtil(si * 2 + 1, ss, mid, us, ue, diff);
        updateRangeUtil(si * 2 + 2, mid + 1, se, us, ue, diff);
        tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2];
    }
 
    void updateRange(int n, int us, int ue, int diff)  {
        updateRangeUtil(0, 0, n - 1, us, ue, diff);
    }
 
    int getSumUtil(int ss, int se, int qs, int qe, int si)
    {
       
        if (lazy[si] != 0)
        {
            tree[si] = (se - ss + 1) * lazy[si];
            if (ss != se)
            {
            	lazy[si * 2 + 1] = lazy[si];
                lazy[si * 2 + 2] = lazy[si];
            }
 
            lazy[si] = 0;
        }
 
        if (ss > se || ss > qe || se < qs)
            return 0;
        if (ss >= qs && se <= qe)
            return tree[si];
        int mid = (ss + se) / 2;
        return getSumUtil(ss, mid, qs, qe, 2 * si + 1) +
               getSumUtil(mid + 1, se, qs, qe, 2 * si + 2);
    }
    
    int query(int n, int qs, int qe)
    {
        if (qs < 0 || qe > n - 1 || qs > qe)
        {
            return -1;
        }
 
        return getSumUtil(0, n - 1, qs, qe, 0);
    }
    
	static class Pairs implements Comparable<Pairs> {
		int x;
		int y;

		Pairs(int a, int b) {
			x = a;
			y = b;
		}

		@Override
		public int compareTo(Pairs o) {
			// TODO Auto-generated method stub
			if (x == o.x)
				return Integer.compare(y, o.y);
			else
				return Long.compare(x, o.x);
		}

	}

	public static void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}

	static class InputReader {

		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int[] nextIntArray(int n) {
			int a[] = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextInt();
			}
			return a;
		}

		public long[] nextLongArray(int n) {
			long a[] = new long[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextInt();
			}
			return a;
		}

		public String readString() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
}