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

public class WaterTree
{
    static final int maxn = 500000;
    static int time = 0;

    static final Edge[] g = new Edge[maxn];
    static final int[] T = new int[maxn], C = new int[maxn], size = new int[maxn], skip = new int[maxn],
    d = new int[maxn], len = new int[maxn], pow = new int[maxn], enter = new int[maxn], exit = new int[maxn], eseg = new int[1<<20];
    static final int[][] seg = new int[maxn][];

    static int root(final int i, final int p, final int lvl)
    {
        T[i] = p; size[i] = 1; d[i] = lvl; enter[i] = time++;
        for(Edge e = g[i]; e!=null; e=e.nxt)
            if(e.to!=p)
                size[i] += root(e.to, i, lvl+1);
        exit[i] = time;
        return size[i];
    }

    static void build(final int i, final int s)
    {
        skip[i] = s; len[i] = 0;
        ++len[s];
        for(Edge e = g[i]; e!=null; e=e.nxt)
            if(e.to!=T[i])
                if(size[e.to]*2>=size[i]) build(e.to, s);
                else build(e.to, e.to);

        if(i==s && len[i]>1)
        {
            int pw = 1, ln = len[i];
            while(pw<ln) pw <<= 1;
            pow[i] = pw;
            final int[] v = seg[i] = new int[2*pw];
        }
        else{ pow[i] = 0; seg[i] = null; }
    }

    static void update(final int i, int nod, final int cost)
    {
        final int[] v = seg[i];
        if(v==null) return;
        nod = d[nod] - d[i] + pow[i];
        v[nod] = cost;
        for(nod>>=1; nod>0; nod>>=1) v[nod] = Math.max(v[2*nod], v[2*nod+1]);
    }
    static int query(int nod)
    {
        int max = C[0];
        for(; nod!=0; nod=T[skip[nod]])
            if(skip[nod]!=nod) max = Math.max(max, query(skip[nod],nod));
            else max = Math.max(max, C[nod]);
        return max;
    }
    static int query(int i, int nod)
    {
        final int[] v = seg[i];
        nod = d[nod]-d[i]+1;
        int l = 0, r = pow[i], max = 0; i = 1;
        while(r>nod)
        {
            final int mid = l+r>>1; i<<=1;
            if(mid==nod) break;
            else if(mid<nod){l = mid; max = Math.max(max, v[i++]);}
            else r = mid;
        }
        return Math.max(max, v[i]);
    }

    static void update(int nod, int cost)
    {
        eseg[nod] = cost;
        for(nod>>=1; nod>0; nod>>=1) eseg[nod] = Math.max(eseg[2*nod], eseg[2*nod+1]);
    }
    static int q(final int i, final int l, final int r, final int a, final int b)
    {
        if(l>=a && r<=b) return eseg[i];
        final int mid = l+r>>1;
        int max = a<mid ? q(2*i,l,mid,a,b) : 0;
        if(b>mid) max = Math.max(max, q(2*i+1,mid,r,a,b));
        return max;
    }

    public static void main(String[] klein)
    {
        new Thread(null, new Runnable(){
            public void run()
            {
                try{ main(); }
                catch(Exception e){}
            }
        }, "", 1<<25).start();
    }

    private static void main() throws IOException
    {
        final IntIO in = new IntIO(System.in);

        final int n = in.getInt();
        for(int i = 1; i<n; i++)
        {
            final int a = in.getInt()-1, b = in.getInt()-1;
            g[a] = new Edge(b,g[a]);
            g[b] = new Edge(a,g[b]);
        }

        root(0,0,0);
        build(0,0);

        final PrintWriter out = new PrintWriter(System.out);
        for(int q = in.getInt(), t = 1; q>0; q--, t++)
        {
            final int c = in.getInt(), v = in.getInt()-1;
            if(c==1) update(skip[v], v, C[v] = t);
            else if(c==2) update(enter[v]+(1<<19), t);
            else
            {
                final int filled = query(v), emptied = q(1, 0, 1<<19, enter[v], exit[v]);
                if(filled>emptied) out.write('1');
                else out.write('0');
                out.write('\n');
            }
        }

        out.flush();
    }

    private static class Edge
    {
        final int to; final Edge nxt;
        Edge(final int t, final Edge n)
        {
            to = t; nxt = n;
        }
    }

    private static class IntIO extends BufferedInputStream
    {
        public IntIO(final InputStream in)
        {
            super(in);
        }

        public IntIO(final InputStream in, final int buf_size)
        {
            super(in, buf_size);
        }

        public int getInt() throws IOException
        {
            int chr = read();
            while(chr<=' ') chr = read();
            int sum = chr-'0';
            while((chr = read())>' ') sum = (sum<<3)+sum+sum + chr-'0';
            return sum;
        }
    }
}
