using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var map = NArr(n - 1); var q = NN; var query = NArr(q); var levels = Enumerable.Repeat(int.MaxValue, n).ToArray(); levels[0] = 0; var tree = new List<(int to, int lv)>[n]; for (var i = 0; i < n; ++i) tree[i] = new List<(int to, int lv)>(); for (var i = 0; i < map.Length; ++i) { tree[map[i][1] - 1].Add((i + 1, map[i][0])); } DFS(0, tree, levels); var dic = new Dictionary(); for (var i = 0; i < levels.Length; ++i) { if (dic.ContainsKey(levels[i])) ++dic[levels[i]]; else dic[levels[i]] = 1; } dic[int.MaxValue] = 0; var llist = new List(dic.Keys); llist.Sort(); var clist = new int[llist.Count]; for (var i = 0; i < llist.Count; ++i) { clist[i] = dic[llist[i]] + (i > 0 ? clist[i - 1] : 0); } var ans = new int[q]; for (var i = 0; i < q; ++i) { if (query[i][0] == 1) { var pos = LowerBound(0, query[i][1], llist); if (query[i][1] < llist[pos]) --pos; ans[i] = clist[pos]; } else { ans[i] = levels[query[i][1] - 1]; } } WriteLine(string.Join("\n", ans.Select(ai => ai == int.MaxValue ? -1 : ai))); } static void DFS(int cur, List<(int to, int lv)>[] tree, int[] levels) { foreach (var next in tree[cur]) { levels[next.to] = Math.Max(levels[cur], next.lv); DFS(next.to, tree, levels); } } static int LowerBound(int left, int min, List list) { if (list[left] >= min) return left; var ng = left; var ok = list.Count; while (ok - ng > 1) { var center = (ng + ok) / 2; if (list[center] < min) ng = center; else ok = center; } return ok; } }