I,R=input,range def S(n): return int(n)-1 def J(): return map(S,I().split()) N=int(I()) p,w=[i for i in R(N)],[0]*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) A,B=r(A),r(i) if w[B]x:m[1-e].add(i) else:d,n=d+1,n|E[i] D[x],e=d,1-e z=r(0) for i in R(Q):print([-1,L[y[i]]+1][r(y[i])==z] if t[i] else D[y[i]])