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.StringTokenizer;
import java.util.InputMismatchException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.ArrayList;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileNotFoundException;

import java.math.BigInteger;
import java.math.BigDecimal;

import java.text.Format;

public class Main {
    public static void main(String[] args) {
        new Main();
    }
    
    Scanner stdin;
    
    class Edge {
        int vertex;
        int next;
        Edge(int vertex, int next) {
            this.vertex = vertex;
            this.next = next;
        }
    }
    
    Edge[] edges;
    int[] head;
    int edgeCnt;

    void initEdgeTable(int mostEdge) {
        edges = new Edge[mostEdge];
        head = new int[mostEdge];
        edgeCnt = 0;
        Arrays.fill(head, -1);
    }

    void addEdge(int u, int v) {
        edges[edgeCnt] = new Edge(v, head[u]);
        head[u] = edgeCnt;
        edgeCnt++;
    }

    int[] fst, lst;
    int[] father;
    int timestamp;

    void initDfs(int n) {
        fst = new int[n + 1];
        lst = new int[n + 1];
        father = new int[n + 1];
        timestamp = 0;
    }

    void dfs(int u, int f) {
        fst[u] = ++timestamp;
        father[u] = f;
        for (int i = head[u]; i != -1; i = edges[i].next) {
            int v = edges[i].vertex;
            if (v == f)
                continue;
            dfs(v, u);
        }
        lst[u] = timestamp;
    }

    boolean[] filled;

    void fill(int l, int r, int id, int fst, int lst) {
        // if (id == 1)
        //     System.out.println("fill: " + fst + " " + lst);
        if (filled[id])
            return;
        if (l == fst && r == lst) {
            filled[id] = true;
            return;
        }
        int mid = (l + r) >> 1;
        if (lst <= mid) {
            fill(l, mid, id << 1, fst, lst);
        } else if (fst > mid) {
            fill(mid + 1, r, id << 1 | 1, fst, lst);
        } else {
            fill(l, mid, id << 1, fst, mid);
            fill(mid + 1, r, id << 1 | 1, mid + 1, lst);
        }
        filled[id] = (filled[id << 1] && filled[id << 1 | 1]);
    }

    boolean allFilled(int l, int r, int id, int fst, int lst) {
        // if (id == 1)
        //     System.out.println("allFilled? " + fst + " " + lst);
        if (filled[id])
            return true;
        if (l == fst && r == lst)
            return false;
        int mid = (l + r) >> 1;
        if (lst <= mid)
            return allFilled(l, mid, id << 1, fst, lst);
        if (fst > mid)
            return allFilled(mid + 1, r, id << 1 | 1, fst, lst);
        return allFilled(l, mid, id << 1, fst, mid) &&
            allFilled(mid + 1, r, id << 1 | 1, mid + 1, lst);
    }

    void empty(int l, int r, int id, int pos) {
        // if (id == 1)
        //     System.out.println("empty: " + pos);
        if (l == r) {
            filled[id] = false;
            return;
        }
        if (filled[id]) {
            filled[id << 1] = true;
            filled[id << 1 | 1] = true;
        }
        int mid = (l + r) >> 1;
        if (pos <= mid)
            empty(l, mid, id << 1, pos);
        else empty(mid + 1, r, id << 1 | 1, pos);
        filled[id] = false;
    }

    public Main() {
        stdin = new Scanner(System.in);
        int n = stdin.nextInt();
        initEdgeTable(n << 1);
        for (int i = 1; i < n; ++i) {
            int u = stdin.nextInt();
            int v = stdin.nextInt();
            addEdge(u, v);
            addEdge(v, u);
        }
        initDfs(n);
        dfs(1, -1);
        // for (int i = 1; i <= n; ++i) {
        //     System.out.print(i + ": ");
        //     System.out.println(fst[i] + " " + lst[i] + " " + father[i]);
        // }
        filled = new boolean[(n << 2) + 1];
        int cmdCnt = stdin.nextInt();
        for (int cmdi = 1; cmdi <= cmdCnt; ++cmdi) {
            int cmd = stdin.nextInt();
            int arg = stdin.nextInt();
            // System.out.println("cmd: " + cmd + " " + arg);
            switch(cmd) {
            case 1:
                if (!allFilled(1, n, 1, fst[arg], lst[arg])) {
                    fill(1, n, 1, fst[arg], lst[arg]);
                    if (!allFilled(1, n, 1, fst[arg], lst[arg]))
                        System.out.println("fill wrong");
                    if (arg != 1)
                        empty(1, n, 1, fst[father[arg]]);
                }
                break;
            case 2:
                empty(1, n, 1, fst[arg]);
                break;
            case 3:
                if (allFilled(1, n, 1, fst[arg], lst[arg]))
                    System.out.println(1);
                else System.out.println(0);
                break;
            default: break;
            }
        }
    }
}

class Scanner {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public boolean hasNext() {
        try {
            for (; curChar < numChars && isSpaceChar(buf[curChar]); curChar++);

            if (curChar < numChars) {
                return true;
            } else {
                curChar = 0;
                numChars = stream.read(buf);
                if (numChars == -1)
                    return false;
                else return true;
            }
        } catch (IOException e) {
            return false;
        }
    }

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

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

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

    public String next() {
        StringBuilder string = new StringBuilder();
        int c = read();
        while (isSpaceChar(c))
            c = read();
        do {
            string.append((char) c);
            c = read();
        } while (!isSpaceChar(c));
        return string.toString();
    }

    private boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return isWhitespace(c);
    }

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

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

    /*Submit*/
    public Scanner(InputStream stream) {
        this.stream = stream;
    }

    /*Local*/
    // public Scanner(InputStream stream) {
    //     try {
    //         this.stream = new FileInputStream(new File("data.in"));
    //     } catch(FileNotFoundException e) {
    //         System.out.println("File not found");
    //     }
    // }
} // class Scanner