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.*;

public class WaterTree implements Runnable {
    static ArrayList<Integer>[] graph;
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new WaterTree(), "spargeltarzan", 1L<< 29);
        thread.start();
        thread.join();
    }
    public void run() {
        try {solve();} catch (Exception e) {
            System.err.println(e);
        }
    }
    public void solve() throws Exception {
        FastScanner scanner = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, false);
        int n = scanner.nextInt();
        graph = new ArrayList[n];
        for(int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        for(int i = 1; i < n; i++) {
            int a = scanner.nextInt()-1;
            int b = scanner.nextInt()-1;
            graph[a].add(b); graph[b].add(a);
        }
        inval = new int[n];
        parent = new int[n];
        sz = new int[n];
        cnt = 0;
        dfsOrder(0,0);
        graph = null;
        SegTree sg = new SegTree(n);
        int q = scanner.nextInt();
        sg.update(1, 0, n-1,2 * q, 0, n-1);
        for(int Q = 1; Q <= q; Q++) {
            int type = scanner.nextInt();
            int v = scanner.nextInt()-1;
            if (type == 1) {
                //fill operation
                int val = sg.query(1, inval[v], inval[v] + sz[v], 0, n-1);
                if (val == 2 * q && v != 0) {
                    sg.update(1,inval[parent[v]], inval[parent[v]], 2 * q, 0, n-1);
                }
                sg.update(1,inval[v], inval[v] + sz[v], Q, 0, n-1);
            } else if (type == 2) {
                //empty operation
                sg.update(1,inval[v],inval[v], 2*q,0, n-1);
            } else {
                int val = sg.query(1, inval[v], inval[v] + sz[v], 0, n-1);
                if (val == 2*q) out.println(0);
                else out.println(1);
            }
        }
        out.flush();
    }
    static int[] inval, sz;
    static int[] parent;
    static int cnt;
    static void dfsOrder(int cur, int p) {
        inval[cur] = cnt++;
        parent[cur] = p;
        for(int edge: graph[cur]) {
            if (edge == p) continue;
            dfsOrder(edge, cur);
            sz[cur] += sz[edge] + 1;
        }
    }
    static class SegTree {
        int[] max, delta;
        public SegTree(int ss) {
            max = new int[4 * ss+1];
            delta = new int[4 * ss+1];
        }
        public int query(int cur, int l, int r, int lo, int hi) {
            if (l > hi || r < lo) return -1;
            if (r >= hi && l<=lo) {
                if (delta[cur] != 0) return delta[cur];
                return max[cur];
            }
            prop(cur);
            int mid = (hi + lo)/2;
            int left = query(cur*2, l, r, lo, mid);
            int right = query(cur*2 + 1, l,r,mid + 1, hi);
            update(cur);
            if (left >= right) return left;
            return right;
        }
        void prop(int cur) {
            if (delta[cur] > 0) {
                delta[cur * 2] = delta[cur];
                delta[cur * 2 + 1] = delta[cur];
                delta[cur] = 0;
            }
        }
        void update(int cur) {
            int temp = delta[cur*2] != 0 ? delta[cur*2] : max[cur*2];
            int t2 = delta[cur*2+1] != 0 ? delta[cur*2+1] : max[cur*2+1];
            max[cur] = temp;
            if (t2 > max[cur]) max[cur] = t2;
        }
        void update(int cur, int l, int r, int amt, int lo, int hi) {
            if (l > hi || r < lo) {
                return;
            }
            if (r >= hi && l <= lo) {
                delta[cur] = amt;
                return;
            }
            prop(cur);
            int mid = (hi + lo)/2;
            update(cur*2, l, r, amt, lo, mid);
            update(cur*2 + 1, l, r, amt, mid + 1, hi);
            update(cur);
        }
        
    }
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        
        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }
        
        public FastScanner() {
            this(new InputStreamReader(System.in));
        }
        
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        
        long nextLong() {
            return Long.parseLong(next());
        }
        
        double nextDouble() {
            return Double.parseDouble(next());
        }
        
        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
