165_D. Beard Graph_36702
Let's define a non-oriented connected graph of n vertices and n - 1 edges as a beard, if all of its vertices except, perhaps, one, have the degree of 2 or 1 (that is, there exists no more than one vertex, whose degree is more than two). Let us remind you that the degree of a vertex is the number of edges that connect to it. 

Let each edge be either black or white. Initially all edges are black.

You are given the description of the beard graph. Your task is to analyze requests of the following types: 

  * paint the edge number i black. The edge number i is the edge that has this number in the description. It is guaranteed that by the moment of this request the i-th edge is white 
  * paint the edge number i white. It is guaranteed that by the moment of this request the i-th edge is black 
  * find the length of the shortest path going only along the black edges between vertices a and b or indicate that no such path exists between them (a path's length is the number of edges in it) 



The vertices are numbered with integers from 1 to n, and the edges are numbered with integers from 1 to n - 1.

Input

The first line of the input contains an integer n (2 ≤ n ≤ 105) — the number of vertices in the graph. Next n - 1 lines contain edges described as the numbers of vertices vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) connected by this edge. It is guaranteed that the given graph is connected and forms a beard graph, and has no self-loops or multiple edges.

The next line contains an integer m (1 ≤ m ≤ 3·105) — the number of requests. Next m lines contain requests in the following form: first a line contains an integer type, which takes values ​​from 1 to 3, and represents the request type.

If type = 1, then the current request is a request to paint the edge black. In this case, in addition to number type the line should contain integer id (1 ≤ id ≤ n - 1), which represents the number of the edge to paint.

If type = 2, then the current request is a request to paint the edge white, its form is similar to the previous request.

If type = 3, then the current request is a request to find the distance. In this case, in addition to type, the line should contain two integers a, b (1 ≤ a, b ≤ n, a can be equal to b) — the numbers of vertices, the distance between which must be found.

The numbers in all lines are separated by exactly one space. The edges are numbered in the order in which they are given in the input.

Output

For each request to "find the distance between vertices a and b" print the result. If there is no path going only along the black edges between vertices a and b, then print "-1" (without the quotes). Print the results in the order of receiving the requests, separate the numbers with spaces or line breaks.

Examples

Input

3
1 2
2 3
7
3 1 2
3 1 3
3 2 3
2 2
3 1 2
3 1 3
3 2 3


Output

1
2
1
1
-1
-1


Input

6
1 5
6 4
2 3
3 5
5 6
6
3 3 4
2 5
3 2 6
3 1 2
2 3
3 3 1


Output

3
-1
3
2

Note

In the first sample vertices 1 and 2 are connected with edge number 1, and vertices 2 and 3 are connected with edge number 2. Before the repainting edge number 2 each vertex is reachable from each one along the black edges. Specifically, the shortest path between 1 and 3 goes along both edges.

If we paint edge number 2 white, vertex 3 will end up cut off from other vertices, that is, no path exists from it to any other vertex along the black edges.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;

public class Solution {
    static class TreeNode {
        int l;
        int r;
        int m;
        int v;
        TreeNode le;
        TreeNode ri;

        public TreeNode(int lb, int rb) {
            l = lb;
            r = rb;
            m = (lb + rb) / 2;

            if (l == r) {
                v = 1;
                le = ri = null;
            } else {
                le = new TreeNode(lb, m);
                ri = new TreeNode(m + 1, rb);
                v = le.v + ri.v;
            }
        }

        public void set(int i, int val) {
            if (l == r)
                v = val;
            else {
                if (i <= m)
                    le.set(i, val);
                else
                    ri.set(i, val);

                v = le.v + ri.v;
            }
        }

        public int get(int lb, int rb) {
            if (l == lb && r == rb)
                return v;
            else if (rb <= m)
                return le.get(lb, rb);
            else if (lb > m)
                return ri.get(lb, rb);
            else
                return le.get(lb, m) + ri.get(m + 1, rb);
        }
    }

    static class PairV implements Comparable<PairV> {
        int a;
        int b;

        public PairV(int A, int B) {
            a = A;
            b = B;
        }

        @Override
        public int compareTo(PairV p) {
            return a != p.a ? a - p.a : b - p.b;
        }
    }

    static Vector<Vector<Integer>> G;
    static int[] colsE;
    static int[] indE;
    static int[] colsV;
    static int[] indV;
    static TreeMap<PairV, Integer> map;

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        int n = Integer.parseInt(in.readLine());
        G = new Vector<Vector<Integer>>();

        for (int i = 0; i < n; i++)
            G.add(new Vector<Integer>());

        colsE = new int[n - 1];
        indE = new int[n - 1];
        colsV = new int[n];
        indV = new int[n];
        map = new TreeMap<Solution.PairV, Integer>();

        for (int i = 0; i < n - 1; i++) {
            StringTokenizer st = new StringTokenizer(in.readLine());

            int from = Integer.parseInt(st.nextToken()) - 1;
            int to = Integer.parseInt(st.nextToken()) - 1;

            G.elementAt(from).add(to);
            G.elementAt(to).add(from);

            PairV p1 = new PairV(from, to);
            PairV p2 = new PairV(to, from);

            map.put(p1, i);
            map.put(p2, i);
        }

        int maxI = -1;
        int max = Integer.MIN_VALUE;

        for (int i = 0; i < n; i++) {
            if (G.elementAt(i).size() > max) {
                maxI = i;
                max = G.elementAt(i).size();
            }
        }

        Vector<TreeNode> roots = new Vector<Solution.TreeNode>();

        Vector<Integer> adj = G.elementAt(maxI);
        for (int i = 0; i < adj.size(); i++) {
            int to = adj.elementAt(i);
            int size = dfs(to, maxI, i, 1);

            TreeNode root = new TreeNode(0, size);
            roots.add(root);
        }

        int m = Integer.parseInt(in.readLine());

        for (int i = 0; i < m; i++) {
            StringTokenizer st = new StringTokenizer(in.readLine());
            int t = Integer.parseInt(st.nextToken());

            switch (t) {
            case 1:
            case 2: {
                int edgeI = Integer.parseInt(st.nextToken()) - 1;
                int col = colsE[edgeI];
                int ind = indE[edgeI];
                TreeNode root = roots.elementAt(col);
                root.set(ind, t == 1 ? 1 : 0);
                break;
            }
            case 3: {
                int a = Integer.parseInt(st.nextToken()) - 1;
                int b = Integer.parseInt(st.nextToken()) - 1;

                if (a == b)
                    out.println(0);
                else {
                    if (a == maxI || b == maxI) {
                        if (b == maxI) {
                            int tmp = b;
                            b = a;
                            a = tmp;
                        }

                        int cnt = getCntToRoot(roots, b);
                        out.println(cnt);
                    } else {
                        int colA = colsV[a];
                        int colB = colsV[b];

                        if (colA == colB) {
                            if (indV[a] > indV[b]) {
                                int tmp = a;
                                a = b;
                                b = tmp;
                            }

                            TreeNode root = roots.elementAt(colA);
                            int cnt = root.get(indV[a], indV[b] - 1);
                            if (cnt != indV[b] - indV[a])
                                out.println(-1);
                            else
                                out.println(cnt);
                        } else {
                            int cntA = getCntToRoot(roots, a);
                            int cntB = getCntToRoot(roots, b);

                            if (cntA == -1 || cntB == -1)
                                out.println(-1);
                            else
                                out.println(cntA + cntB);
                        }
                    }
                }
            }
            }
        }

        out.close();
    }

    private static int getCntToRoot(Vector<TreeNode> roots, int b) {
        int colB = colsV[b];
        TreeNode root = roots.elementAt(colB);
        int cnt = root.get(0, indV[b] - 1);
        if (cnt != indV[b])
            return -1;
        else
            return cnt;
    }

    private static int dfs(int v, int from, int col, int d) {
        colsV[v] = col;
        indV[v] = d;

        int edgeI = map.get(new PairV(from, v));

        colsE[edgeI] = col;
        indE[edgeI] = d - 1;

        int ret = 1;

        Vector<Integer> adj = G.elementAt(v);

        for (int i = 0; i < adj.size(); i++) {
            int to = adj.elementAt(i);

            if (to == from)
                continue;

            ret += dfs(to, v, col, d + 1);
        }

        return ret;
    }
}
6
1 5
6 4
2 3
3 5
5 6
6
3 3 4
2 5
3 2 6
3 1 2
2 3
3 3 1
 3
1 2
2 3
7
3 1 2
3 1 3
3 2 3
2 2
3 1 2
3 1 3
3 2 3
 3
2 3
1 3
4
3 3 1
3 2 2
3 1 3
2 2
 10
8 9
4 1
3 4
2 6
7 6
1 5
9 3
10 2
5 6
3
3 5 6
3 5 6
3 9 10
 5
3 4
1 2
2 4
5 3
10
3 4 1
3 5 5
3 3 3
2 3
3 1 1
3 4 4
3 3 1
3 2 3
3 4 3
2 1
 10
10 8
9 1
2 9
1 7
5 4
6 7
3 7
4 7
8 7
10
2 9
3 3 10
2 4
2 5
2 8
1 8
2 7
2 1
1 5
2 6
 5
3 4
1 2
2 5
5 3
10
3 4 1
3 5 5
3 3 3
2 3
3 1 1
3 4 4
3 3 1
3 2 3
3 4 3
2 1
 6
1 5
6 4
2 3
3 5
5 6
6
3 3 4
2 5
3 2 6
3 1 2
2 2
3 3 1
 10
10 8
9 1
2 9
1 7
5 4
6 7
3 7
4 3
8 7
10
2 9
3 3 10
2 4
2 5
2 8
1 8
2 7
2 1
1 5
2 6
 6
1 5
6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