I,R=input,range def S(n): return int(n)-1 def J(): return map(S,I().split()) N=int(I()) p=[i for i in R(N)] def r(i): m=p[i] while i!=m: p[i]=i=p[m] m=p[i] return i E,L=[set() for i in R(N)],[0]*N for i in R(1,N): L[i],A=J() E[A].add(i) p[r(i)]=r(A) n={0} while len(n): i=n.pop() for e in E[i]:L[e]=max(L[e],L[i]) n|=E[i] Q=int(I()) t,y,X=[0]*Q,[0]*Q,set() for i in R(Q): t[i],y[i]=J() X.add(y[i]) D,d,m,e={},0,[{0},set()],0 for x in sorted(X): n=m[e] while len(n): i=n.pop() if L[i]>x:m[1-e].add(i) else:d,n=d+1,n|E[i] D[x],e=d,1-e for i in R(Q):print([-1,L[y[i]]+1][r(y[i])<1] if t[i] else D[y[i]])