In this problem you will have to help Berland army with organizing their command delivery system.

There are $n$ officers in Berland army. The first officer is the commander of the army, and he does not have any superiors. Every other officer has exactly one direct superior. If officer $a$ is the direct superior of officer $b$, then we also can say that officer $b$ is a direct subordinate of officer $a$.

Officer $x$ is considered to be a subordinate (direct or indirect) of officer $y$ if one of the following conditions holds:  officer $y$ is the direct superior of officer $x$;  the direct superior of officer $x$ is a subordinate of officer $y$. 

For example, on the picture below the subordinates of the officer $3$ are: $5, 6, 7, 8, 9$.

The structure of Berland army is organized in such a way that every officer, except for the commander, is a subordinate of the commander of the army.

Formally, let's represent Berland army as a tree consisting of $n$ vertices, in which vertex $u$ corresponds to officer $u$. The parent of vertex $u$ corresponds to the direct superior of officer $u$. The root (which has index $1$) corresponds to the commander of the army.

Berland War Ministry has ordered you to give answers on $q$ queries, the $i$-th query is given as $(u_i, k_i)$, where $u_i$ is some officer, and $k_i$ is a positive integer.

To process the $i$-th query imagine how a command from $u_i$ spreads to the subordinates of $u_i$. Typical DFS (depth first search) algorithm is used here.

Suppose the current officer is $a$ and he spreads a command. Officer $a$ chooses $b$ — one of his direct subordinates (i.e. a child in the tree) who has not received this command yet. If there are many such direct subordinates, then $a$ chooses the one having minimal index. Officer $a$ gives a command to officer $b$. Afterwards, $b$ uses exactly the same algorithm to spread the command to its subtree. After $b$ finishes spreading the command, officer $a$ chooses the next direct subordinate again (using the same strategy). When officer $a$ cannot choose any direct subordinate who still hasn't received this command, officer $a$ finishes spreading the command.

Let's look at the following example: [Image] 

If officer $1$ spreads a command, officers receive it in the following order: $[1, 2, 3, 5 ,6, 8, 7, 9, 4]$.

If officer $3$ spreads a command, officers receive it in the following order: $[3, 5, 6, 8, 7, 9]$.

If officer $7$ spreads a command, officers receive it in the following order: $[7, 9]$.

If officer $9$ spreads a command, officers receive it in the following order: $[9]$.

To answer the $i$-th query $(u_i, k_i)$, construct a sequence which describes the order in which officers will receive the command if the $u_i$-th officer spreads it. Return the $k_i$-th element of the constructed list or -1 if there are fewer than $k_i$ elements in it.

You should process queries independently. A query doesn't affect the following queries.


-----Input-----

The first line of the input contains two integers $n$ and $q$ ($2 \le n \le 2 \cdot 10^5, 1 \le q \le 2 \cdot 10^5$) — the number of officers in Berland army and the number of queries.

The second line of the input contains $n - 1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i < i$), where $p_i$ is the index of the direct superior of the officer having the index $i$. The commander has index $1$ and doesn't have any superiors.

The next $q$ lines describe the queries. The $i$-th query is given as a pair ($u_i, k_i$) ($1 \le u_i, k_i \le n$), where $u_i$ is the index of the officer which starts spreading a command, and $k_i$ is the index of the required officer in the command spreading sequence.


-----Output-----

Print $q$ numbers, where the $i$-th number is the officer at the position $k_i$ in the list which describes the order in which officers will receive the command if it starts spreading from officer $u_i$. Print "-1" if the number of officers which receive the command is less than $k_i$.

You should process queries independently. They do not affect each other.


-----Example-----
Input
9 6
1 1 1 3 5 3 5 7
3 1
1 5
3 4
7 3
1 8
1 9

Output
3
6
8
-1
9
4

Difficulty: introductory

Starter code:

Solutions:
n, q = map(int, input().split())
par = [int(v)-1 for v in input().split()]
chs = [[] for i in range(n)]
for i, p in enumerate(par):
    chs[p].append(i+1)
vis = [0 for _ in range(n)]
bel = [1 for _ in range(n)]
stack = [0]
order = [0]
while stack:
    v = stack[-1]
    if len(chs[v]) == vis[v]:
        if v != 0:
            bel[par[v-1]] += bel[v]
        stack.pop()
        continue
    ch = chs[v][vis[v]]
    vis[v] += 1
    order.append(ch)
    stack.append(ch)
FST = {}
for i, c in enumerate(order):
    FST[c] = i
out = []
for _ in range(q):
    u, k = map(lambda x: int(x) - 1, input().split())
    if k >= bel[u]:
        out.append(-1)
    else:
        out.append(order[FST[u] + k] + 1)
print('\n'.join(map(str, out)))

n,q=map(int,input().split())
p=[-1]+[int(s)-1 for s in input().split()]
o=[0]*n
l=[0]*n
s=[1]*n
v=[1]*n
for i in range(n-1,0,-1):
  s[p[i]] += s[i]
for i in range(1,n):
  l[i]=v[p[i]]
  v[p[i]] += s[i]
  o[l[i]]=i
  v[i]=l[i]+1
res=[0]*q
for i in range(q):
  u,k=map(int,input().split())
  u-=1
  k-=1
  res[i]=str(o[l[u]+k]+1 if k < s[u] else -1)
print('\n'.join(res))

n, q = map(int, input().split())
v = list(map(int, input().split()))
d = [list() for i in range(n)]
r = [1] * n
l = [0] * n
for j in range(n-1):
    d[v[j]-1].append(j+1)
def push():
    stack = list()
    re_stack = list()
    stack.append(0)
    while True:
        if len(stack) == 0:
            return re_stack
        tmp = stack.pop(0)
        re_stack.append(tmp)
        tmp_ = d[tmp]
        stack = tmp_ + stack
s = push()
for i in range(n):
    l[s[i]] = i
for i in range(n-1, -1, -1):
    for j in d[s[i]]:
        r[s[i]] += r[j]
ss=[]
for x in range(q):
    u, k = map(int, input().split())
    if k > r[u-1]:
        ss.append(-1)
    else:
        ss.append(s[l[u-1]+k-1]+1)
print('\n'.join(map(str, ss)))

n, q = map(int, input().split())
sups = list(map(int, input().split()))

dir_subs = [[] for i in range(n+1)]
num_subs = [0]*(n+1)

sups = [0,0]+sups
for i in range(2, n+1):
	dir_subs[sups[i]].append(i)

#ordered = [1]
prev_parent = 0
#qe = [-1]+dir_subs[1]
qe = [1]
ordered = []

while len(qe)>0:
	new = qe.pop()

	if new > 0:
		#new_parent = sups[new]
		ordered.append(new)
		#num_subs[new_parent] += 1
		
		#qe = dir_subs[new] + [-new] + qe
		qe.append(-new)
		num = len(dir_subs[new])
		for i in range(num):
			qe.append(dir_subs[new][num-1-i])

		#if new_parent == prev_parent:
		#	num_subs[new_parent] += num_subs[curr_parent]
		#	prev_parent = new_parent
	else:
		parent = -new
		for child in dir_subs[parent]:
			num_subs[parent] += 1
			num_subs[parent] += num_subs[child]

#print(num_subs)
#print(ordered)
# [0, 8, 0, 5, 0, 2, 0, 1, 0, 0]


indices = [0]*(n+1)

for i in range(n):
	indices[ordered[i]] = i

def ans_query(u, k):
	if k-1 > num_subs[u]:
		return -1
	
	start = indices[u]
	end = ordered[start+k-1]
	return end

s = ''
for i in range(q):
	u, k = map(int, input().split())
	s += (str(ans_query(u,k))+'\n')
	if i%20 == 0:
		print(s[:-1])
		s = ''

if len(s) > 0:
	print(s[:-1])

from sys import *
from math import *

#setrecursionlimit(20000)
generals = []

class Node:

    def __init__ (self, value):
        self.value = value
        self.children = []
        self.start = -1
        self.end = -1

    def add(self, fnode):
        self.children.append(fnode)


def explore(node, index):
    stack = []
    stack.append(node)
    while len(stack) > 0:
        n = stack.pop()
        generals.append(n.value)
        n.start = index
        index+=1
        for child in n.children[::-1]:
            stack.append(child)


    stack = []
    stack.append(node)
    while len(stack) > 0:
        n = stack.pop()
        for i in range(len(n.children)):
            child = n.children[i]
            if i == len(n.children)-1:
                child.end = n.end
            else:
                child.end = n.children[i+1].start
        for child in n.children[::-1]:
            stack.append(child)


def printN(node):
    print(node.value, end=" ")
    print(node.start, end=" ")
    print(node.end)
    for n in node.children:
        printN(n)

nodes = []


line = stdin.readline().rstrip().split()
n = int(line[0])
q = int(line[1])

#n = 100000
#q = 5


for i in range(n):
    nodes.append(Node(i+1))

dependencies = list(map(int, stdin.readline().rstrip().split()))
#dependencies = range(1, 100000)
for i in range(len(dependencies)):
    nodes[dependencies[i]-1].add(nodes[i+1])

nodes[0].start = 0
nodes[0].end = n
explore(nodes[0], 0)

#printN(nodes[0])




for i in range(q):
    line = stdin.readline().rstrip().split()
    u = int(line[0])
    k = int(line[1])-1

    node = nodes[u-1]
    if k < node.end - node.start:
        print(generals[node.start + k])
    else:
        print(-1)





#input = open('e1.in').readline

n, q = map(int, input().split())
children = [[] for i in range(n)]
size = [0]*n
for i, p in enumerate(map(int, input().split()), 1):
	children[p-1].append(i)

size = [1]*n


dfs = []
index_to_rank = [0]*n



def traverse():
	visited = [False] * n
	stack = [(0, None)]
	while stack:
		i, parent = stack[-1]
		if visited[i]:
			stack.pop()
			if parent is not None:
				size[parent] += size[i]

		else:
			visited[i] = True
			index_to_rank[i] = len(dfs)
			dfs.append(i)
			for j in reversed(children[i]):
				stack.append((j, i))

answer = []
def answer_queries():
	print = answer.append
	for i in range(q):
		u, k = map(int, input().split())
		index = u-1
		if size[index] < k:
			print(-1)
		else:
			print(dfs[index_to_rank[index]+k-1]+1)

traverse()
answer_queries()
print('\n'.join(map(str, answer)))

n, q = list(map(int,input().split()))
officers = list(map(int,input().split()))

s = [[] for _ in range(n)]
c = [1] * n
b = [0] * n

for i,v in enumerate(officers):
	s[v-1].append(i+1)

traversal = []
def dfs():
	stack = [0]
	while stack:
		n = stack.pop()
		traversal.append(n)
		for child in reversed(s[n]):
			stack.append(child)
dfs()

for i in range(n):
	b[traversal[i]] = i

for i in range(n-1,-1,-1):
	for child in s[i]:
		c[i] += c[child]
# print(c)
answer = []
for _ in range(q):
	u, k = list(map(int,input().split()))
	start = b[u-1]
	if k <= c[u-1]:
		answer.append((str(traversal[start+k-1]+1)))
		# print(traversal[start+k-1]+1)
	else:
		answer.append("-1")
print('\n'.join(answer))


# import sys 
# sys.stdin = open('in.txt','r')

n, q = list(map(int, input().split()))
g = [[] for i in range(n+1)]

p = list(map(int, input().split()))
for i in range(n-1):
	g[p[i]].append(i+2)

timer = 0
children = [0] * (n + 1)
when = [0] * (n + 1)
a = []
instack = [0] * (n + 1)
stack = [1]

while stack:
	u = stack[-1]
	if instack[u] == 1:
		children[u] = len(a) - when[u]
		stack.pop()
		continue
	a.append(u)
	when[u] = len(a) - 1
	instack[u] = 1
	for i in reversed(g[u]):
		stack.append(i)
ans = [0] * q
for i in range(q):
	x, y = list(map(int, input().split()))
	y -= 1
	ans[i] = '-1' if y >= children[x] else str(a[when[x] + y])

print('\n'.join(ans))


	





from collections import deque as dq
from collections import defaultdict as dc
def dfs(stack,vis,child,when,ab):
        while(len(stack)>0):
                a=stack[-1]
                if vis[a]==1:
                        child[a]=len(ab)-when[a]
                        c=stack.pop()
                        continue
                ab.append(a)
                when[a]=len(ab)-1
                #print(a)
                vis[a]=1
                for i in reversed(adj[a]):
                        stack.append(i)
                        #print(stack)
adj=dc(list)
n,q=list(map(int,input().split()))
child=[0]*(n+1)#store the max no. of sub tree element
when=[0]*(n+1)#store i element is at when[i] place in ab
ab=[]#the dfs element
vis=[0]*(n+1)
stack=dq([1])
a=list(map(int,input().split()))
for i in range(2,n+1):
        adj[a[i-2]].append(i)
dfs(stack,vis,child,when,ab)
#print(child)
z=[]
for i in range(q):
        x,y=list(map(int ,input().split()))
        y=y-1
        if y>=child[x]:
                z.append(str(-1))
        else:
                z.append(str(ab[when[x]+y]))
print('\n'.join(z))



n,q = [int(x) for x in input().split()]
L = [int(x)-1 for x in input().split()]

G = []
for i in range(n):
    G.append([])
for i in range(n-1):
    G[L[i]].append((i+1,L[i]))
    G[i+1].append((L[i],i+1))
    
L = [-1]+L
G[0].reverse()

for t in range(1,n):
    G[t] = [G[t][0]]+list(reversed(G[t][1:]))

options = [(0,0)]
visited = [0]*n
sub = [1]*n
path = []
while options:
    t = options.pop()
    if visited[t[0]] == 0:
        visited[t[0]] = 1
        path.append(t[0])
        options.extend(G[t[0]])
    elif visited[t[0]] == 1:
        sub[t[0]] += sub[t[1]]

Position = {}
for i in range(n):
    Position[path[i]] = i


for i in range(q):
    u,k = [int(x) for x in input().split()]
    if sub[u-1] < k:
        print(-1)
    else:
        print(path[Position[u-1]+k-1]+1)

import math
from collections import defaultdict
import sys


def main():
    n, q = list(map(int, input().split()))
    children = [[] for _ in range(n)]
    parent = list(map(int, input().split()))
    for i in range(n-1):
        children[parent[i]-1].append(i+1)
    d = []
    cnt = [1] * n
    stack = [0]

    def dfs():
        while stack:
            node = stack.pop()
            d.append(node)
            for i in range(len(children[node])-1, -1, -1):
                stack.append(children[node][i])

    dfs()
    for i in range(n-1, -1, -1):
        total = 0
        for child in children[i]:
            total += cnt[child]
        cnt[i] += total

    indexof = [-1] * n
    for i, val in enumerate(d):
        indexof[val] = i

    for _ in range(q):
        u, k = list(map(int, input().split()))
        index = indexof[u-1]
        pos = index + k - 1
        if k > cnt[u-1]:
            print(-1)
        else:
            print(d[pos]+1)


def __starting_point():
    main()

__starting_point()

from collections import deque


class CodeforcesTask1006ESolution:
    def __init__(self):
        self.result = ''
        self.n_q = []
        self.command = []
        self.queries = []

    def read_input(self):
        self.n_q = [int(x) for x in input().split(" ")]
        self.command = [int(x) for x in input().split(" ")]
        for _ in range(self.n_q[1]):
            self.queries.append([int(x) for x in input().split(" ")])

    def process_task(self):
        tree = [[] for _ in range(self.n_q[0])]
        for x in range(1, self.n_q[0]):
            tree[self.command[x - 1] - 1].append(x + 1)
        starts = {}
        stops = {}
        called = []
        to_visit = deque([1, -1])
        while to_visit:
            visiting = to_visit.popleft()
            if visiting < 0:
                stops[abs(visiting)] = len(called)
            else:
                called.append(visiting)
                starts[visiting] = len(called)
                n = sorted(tree[visiting - 1], reverse=True)
                to_visit.appendleft(-visiting)
                for a in n:
                   to_visit.appendleft(a)
        ranges = [stops[x] - starts[x] + 1 for x in range(1, self.n_q[0] + 1)]
        for query in self.queries:
            if ranges[query[0] - 1] >= query[1]:
                print(called[starts[query[0]] + query[1] - 2])
            else:
                print('-1')


    def get_result(self):
        return self.result


def __starting_point():
    Solution = CodeforcesTask1006ESolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())

__starting_point()

n,q=map(int,input().split())
p=[-1]+[int(s)-1 for s in input().split()]
o=[0]*n
l=[0]*n
s=[1]*n
v=[1]*n
for i in range(n-1,0,-1):
  s[p[i]] += s[i]
for i in range(1,n):
  l[i]=v[p[i]]
  v[p[i]] += s[i]
  o[l[i]]=i
  v[i]=l[i]+1
res=[0]*q
for i in range(q):
  u,k=map(int,input().split())
  u-=1
  k-=1
  res[i]=str(o[l[u]+k]+1 if k < s[u] else -1)
print('\n'.join(res))

n, q=[int(x) for x in input().split()]
parent=[-1]+[int(x) -1 for x in input().split()]
start=[0]*n
end=[1]*n
size=[1]*n
path=[0]*n
for i in range(n-1, 0, -1):
    size[parent[i]]+=size[i]
for v in range(1, n):
    start[v]=end[parent[v]]
    end[v]=start[v]+1
    end[parent[v]]+=size[v]
    path[start[v]]=v
for j in range(q):
    u, k=[int(x) -1 for x in input().split()]
    if k >= size[u]:
        print ("-1")
        
    else:
        print(path[start[u]+k]+1)


n, q = [int(x) for x in input().split(" ")]
parent = [-1] + [int(x) - 1 for x in input().split(" ")]
start = [0] * n
path = [0] * n
end = [1] * n
size = [1] * n

for i in range(n - 1, 0, -1):
    size[parent[i]] += size[i]

for v in range(1, n):
    start[v] = end[parent[v]]
    end[v] = start[v] + 1
    end[parent[v]] += size[v]
    path[start[v]] = v

for j in range(q):
    u, k = [int(x) - 1 for x in input().split(" ")]
    print(-1 if k >= size[u] else path[start[u] + k] + 1)


from collections import *
from sys import stdin
from bisect import *


def arr_inp(n):
    if n == 1:
        return [int(x) for x in stdin.readline().split()]
    elif n == 2:
        return [float(x) for x in stdin.readline().split()]
    else:
        return [str(x) for x in stdin.readline().split()]


class graph:
    # initialize graph
    def __init__(self, gdict=None):
        if gdict is None:
            gdict = defaultdict(list)
        self.gdict = gdict

    # add edge
    def add_edge(self, node1, node2):
        self.gdict[node1].append(node2)

    def dfsUtil(self, v):
        stack, self.visit, out = [v], [0] * (n + 1), []

        while (stack):
            s = stack.pop()

            if not self.visit[s]:
                out.append(s)
                self.visit[s] = 1

            for i in sorted(self.gdict[s], reverse=True):
                if not self.visit[i]:
                    stack.append(i)

        return out


n, q = arr_inp(1)
p, g, mem, quary = arr_inp(1), graph(), [1 for i in range(n + 1)], defaultdict(lambda: -1)

for i in range(2, n + 1):
    g.add_edge(p[i - 2], i)

all, ans = g.dfsUtil(1), []
mem2 = {all[i]: i for i in range(n)}

for i in all[::-1]:
    for j in g.gdict[i]:
        mem[i] += mem[j]


for i in range(q):
    u, v = arr_inp(1)
    ans.append(str(-1 if mem[u] < v else all[mem2[u] + v - 1]))

print('\n'.join(ans))


def dfs(dp,node,edges,order):
    visited = set()
    stack = [node]
    while stack:
        curr = stack[-1]
        if curr not in visited:
            visited.add(curr)
            order.append(curr)
            
        count = 0
        for kid in edges[curr]:
            if kid in visited:
                count += 1
            else:
                stack.append(kid)

        if count == len(edges[curr]):
            stack.pop()
            for kid in edges[curr]:
                dp[curr] += dp[kid]

            dp[curr] += 1

        #print(stack)
            

def solve(u,k,dp,order,indices,ans):
    if k > dp[u]:
        ans.append(-1)
        return

    index = indices[u]
    ans.append(order[index+k-1])

def main():
    n,q = list(map(int,input().split()))
    parents = list(map(int,input().split()))
    edges = {}
    for i in range(1,n+1):
        edges[i] = []

    for i in range(2,n+1):
        parent = parents[i-2]
        edges[parent].append(i)

    for i in list(edges.keys()):
        edges[i].sort(reverse = True)

    indices = [-1]*(n+1)
    order = []
    dp = [0]*(n+1)
    dfs(dp,1,edges,order)
    #print(order)
    #print(dp)
    for i in range(n):
        indices[order[i]] = i

    ans = []
    for i in range(q):
        u,k = list(map(int,input().split()))
        solve(u,k,dp,order,indices,ans)

    for i in ans:
        print(i)


main()


# itne me hi thakk gaye?
n, q = list(map(int, input().split()))
parent = [-1] + [int(x) -1 for x in input().split()]
# parent = [i-1 for i in parent]
start = [0 for i in range(n)]
end = [1 for i in range(n)]
size = [1 for i in range(n)]
path = [0 for i in range(n)]
for i in range(n-1, 0, -1):
    size[parent[i]] += size[i]
for v in range(1, n):
	start[v] = end[parent[v]]
	end[v] = start[v] + 1
	end[parent[v]] += size[v]
	path[start[v]] = v
for j in range(q):
	u, k = [int(x) -1 for x in input().split()]
	if k>= size[u]:
		print(-1)
	else:
		print(path[start[u] + k] + 1)


n,q=list(map(int,input().split()))
from heapq import heappush as pu
from heapq import heappop as po
from bisect import bisect_right as br 

tr=[[] for i in range(n)]
size=[1 for i in range(n)]

p=list(map(int,input().split()))
p=[i-1 for i in p]

for i in range(n-1):
	tr[p[i]].append(i+1)
for i in range(n):
	tr[i].sort(reverse=True)

h={}
h[0]=0
s=[0]
m=[]
while s:
	x=s.pop()
	m.append(x)
	for i in tr[x]:
		s.append(i)
		h[i]=h[x]+1

d=[(i,h[i]) for i in range(n)]
d.sort(key=lambda x:x[1])
d=[i[0] for i in d]

for i in range(n-1,0,-1):
	size[p[d[i]-1]]+=size[d[i]]
ind={}
for i in range(n):
	ind[m[i]]=i
for _ in range(q):
	u,k=list(map(int,input().split()))
	u-=1
	if k>size[u]:
		print(-1)
	else:
		i=ind[u]
		print(m[i+k-1]+1)


import sys
input = sys.stdin.readline
from collections import deque

class Graph(object):
	"""docstring for Graph"""
	def __init__(self,n,d): # Number of nodes and d is True if directed
		self.n = n
		self.graph = [[] for i in range(n)]
		self.parent = [-1 for i in range(n)]
		self.directed = d
		
	def addEdge(self,x,y):
		self.graph[x].append(y)
		if not self.directed:
			self.graph[y].append(x)

	def bfs(self, root): # NORMAL BFS
		self.parent = [-1 for i in range(self.n)]
		queue = [root]
		queue = deque(queue)
		vis = [0]*self.n
		while len(queue)!=0:
			element = queue.popleft()
			vis[element] = 1
			for i in self.graph[element]:
				if vis[i]==0:
					queue.append(i)
					self.parent[i] = element

	def dfs(self, root, ans): # Iterative DFS
		stack=[root]
		vis=[0]*self.n
		stack2=[]
		while len(stack)!=0: # INITIAL TRAVERSAL
			element = stack.pop()
			order.append(element)
			if vis[element]:
				continue
			vis[element] = 1
			stack2.append(element)
			temp=[]
			for i in self.graph[element]:
				if vis[i]==0:
					temp.append(i)
			# print (element,temp)
			temp.sort(reverse=True)
			for i in temp:
				stack.append(i)

		while len(stack2)!=0: # BACKTRACING. Modify the loop according to the question
			element = stack2.pop()
			m = 0
			for i in self.graph[element]:
				if i!=self.parent[element]:
					m += ans[i]
			ans[element] = m+1
		return ans

	def shortestpath(self, source, dest): # Calculate Shortest Path between two nodes
		self.bfs(source)
		path = [dest]
		while self.parent[path[-1]]!=-1:
			path.append(parent[path[-1]])
		return path[::-1]

	def ifcycle(self):
		self.bfs(0)
		queue = [0]
		vis = [0]*n
		queue = deque(queue)
		while len(queue)!=0:
			element = queue.popleft()
			vis[element] = 1
			for i in graph[element]:
				if vis[i]==1 and i!=parent[element]:
					return True
				if vis[i]==0:
					queue.append(i)
					vis[i] = 1
		return False

	def reroot(self, root, ans):
		stack = [root]
		vis = [0]*n
		while len(stack)!=0:
			e = stack[-1]
			if vis[e]:
				stack.pop()
				# Reverse_The_Change()
				continue
			vis[e] = 1
			for i in graph[e]:
				if not vis[e]:
					stack.append(i)
			if self.parent[e]==-1:
				continue
			# Change_The_Answers()



n,q = list(map(int,input().split()))
g = Graph(n,False)
g.parent = [-1]+list(map(int,input().split()))
for i in range(1,n):
	g.parent[i] -= 1
	g.addEdge(g.parent[i],i)
count = [0]*n
order = []
g.dfs(0, count)
# print (count)
# print (order)
rank = {}
for i in range(n):
	rank[order[i]] = i
for i in range(q):
	a,k = list(map(int,input().split()))
	if k>count[a-1]:
		print(-1)
	else:
		print(order[rank[a-1]+k-1]+1)