#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m,pos;
void addEdge(vector<int> adj[],int u,int v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}
int dfs(vector<int> adj[],vector<int> &pre,int start,bool visited[])
{
	int k=0;
	visited[start] = true;
	pre.push_back(start);
	pos[start]=pre.size()-1;

	for(int i=0;i<adj[start].size();i++)
	{

		int nbr = adj[start][i];
		if(!visited[nbr])
		{
			k+=dfs(adj,pre,nbr,visited);

		}
	}
	m[start] = k;
	return 1+k;
}
int main()
{
	   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	int n,q;

	cin>>n>>q;
	vector<int> adj[n+1];
	for(int i=2;i<=n;i++)
	{
		int val;
		cin>>val;
		addEdge(adj,i,val);
	}
	vector<int> pre;
	bool visited[n+1];
	memset(visited,false,sizeof(visited));
	dfs(adj,pre,1,visited);
	/*for(int i=1;i<=n;i++)
	{
		cout<<m[i]<<"    ";
	}*/
	//cout<<endl;
	while(q--)
	{
		int u,k;
		cin>>u>>k;
		if(m[u]<k-1)
		{
			cout<<-1<<"\n";
			continue;
		}

		//cout<<pos[u]<<" "<<m[k]<<endl;
		int answer=pre[pos[u]+k-1];
		cout<<answer<<"\n";
	}

}


#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> m,pos;
void addEdge(vector<int> adj[],int u,int v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}
int dfs(vector<int> adj[],vector<int> &pre,int start,bool visited[])
{
	int k=0;
	visited[start] = true;
	pre.push_back(start);
	pos[start]=pre.size()-1;

	for(int i=0;i<adj[start].size();i++)
	{

		int nbr = adj[start][i];
		if(!visited[nbr])
		{
			k+=dfs(adj,pre,nbr,visited);

		}
	}
	m[start] = k;
	return 1+k;
}
int main()
{
	   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int n,q;

	cin>>n>>q;
	vector<int> adj[n+1];
	for(int i=2;i<=n;i++)
	{
		int val;
		cin>>val;
		addEdge(adj,i,val);
	}
	vector<int> pre;
	bool visited[n+1];
	memset(visited,false,sizeof(visited));
	dfs(adj,pre,1,visited);
	/*for(int i=1;i<=n;i++)
	{
		cout<<m[i]<<"    ";
	}*/
	//cout<<endl;
	while(q--)
	{
		int u,k;
		cin>>u>>k;
		if(m[u]<k-1)
		{
			cout<<-1<<"\n";
			continue;
		}

		//cout<<pos[u]<<" "<<m[k]<<endl;
		int answer=pre[pos[u]+k-1];
		cout<<answer<<"\n";
	}

}


#include<bits/stdc++.h>
using namespace std;
int n;
bool visited[300000+50];
unordered_map<int,int> m,pos;
void addEdge(vector<int> adj[],int u,int v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
}
int dfs(vector<int> adj[],vector<int> &pre,int start,bool visited[])
{
	int k=0;
	visited[start] = true;
	pre.push_back(start);
	pos[start]=pre.size()-1;

	for(int i=0;i<adj[start].size();i++)
	{

		int nbr = adj[start][i];
		if(!visited[nbr])
		{
			k+=dfs(adj,pre,nbr,visited);

		}
	}
	m[start] = k;
	return 1+k;
}
int main()
{
	   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	int q;

	cin>>n>>q;
	vector<int> adj[n+1];
	for(int i=2;i<=n;i++)
	{
		int val;
		cin>>val;
		addEdge(adj,i,val);
	}
	vector<int> pre;
//	memset(visited,false,sizeof(visited));
	dfs(adj,pre,1,visited);
	/*for(int i=1;i<=n;i++)
	{
		cout<<m[i]<<"    ";
	}*/
	//cout<<endl;
	while(q--)
	{
		int u,k;
		cin>>u>>k;
		if(m[u]<k-1)
		{
			cout<<-1<<"\n";
			continue;
		}

		//cout<<pos[u]<<" "<<m[k]<<endl;
		int answer=pre[pos[u]+k-1];
		cout<<answer<<"\n";
	}

}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>

// watsonlee Coding

using namespace std;

#define ll long long

void dfs(list <ll> * adj, ll index, bool * visited,vector <ll> & vec) {
	visited[index] = true;
	vec.push_back(index + 1);
	list <ll> ::iterator it = adj[index].begin();
	for (; it != adj[index].end(); it++) {
		if (!visited[*it]) {
			dfs(adj, *it, visited, vec);
		}
	}
}

int main() {
	ll n, k;
	cin >> n >> k;
	list <ll> * adj;
	adj = new list <ll>[n];
	for (ll j = 1; j < n; j++) {
		ll a;
		cin >> a;
		adj[a-1].push_back(j);
	}
	ll a, b;
	vector <ll> vec;
	bool * visited = new bool[n];
	for (ll j = 0; j < k; j++) {
		cin >> a >> b;
		for (ll j = 0; j < n; j++) {
			visited[j] = false;
		}

		dfs(adj, a - 1, visited, vec);
		if (b > vec.size()) {
			cout << -1;
		}
		else {
			cout << vec[b - 1];
		}
		vec.clear();
		cout << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>

// watsonlee Coding

using namespace std;

#define ll long long

void dfs(list <ll> * adj, ll index,vector <ll> & vec,ll b) {
	vec.push_back(index + 1);
	if (b!= vec.size()) {
		list <ll> ::iterator it = adj[index].begin();
		for (; it != adj[index].end(); it++) {
				dfs(adj, *it, vec,b);
		}
	}
}

int main() {
	ll n, k;
	cin >> n >> k;
	list <ll> * adj;
	adj = new list <ll>[n];
	for (ll j = 1; j < n; j++) {
		ll a;
		cin >> a;
		adj[a-1].push_back(j);
	}
	ll a, b;
	vector <ll> vec;
	//bool * visited = new bool[n];
	map <ll, vector <ll>> mp;
	for (ll j = 0; j < k; j++) {
		cin >> a >> b;
		if (mp[a - 1].size() <= 0) {
			dfs(adj, a - 1, vec, b);
			mp[a - 1] = vec;
		}
		if (b > mp[a-1].size()) {
			cout << -1;
		}
		else {
			cout << mp[a-1].at(b - 1);
		}
		vec.clear();
		cout << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>

// watsonlee Coding

using namespace std;

#define ll long long

void dfs(list <ll> * adj, ll index,vector <ll> & vec,ll b) {
	vec.push_back(index + 1);
	if (b!= vec.size()) {
		list <ll> ::iterator it = adj[index].begin();
		for (; it != adj[index].end(); it++) {
				dfs(adj, *it, vec,b);
		}
	}
}

int main() {
	ll n, k;
	cin >> n >> k;
	list <ll> * adj;
	adj = new list <ll>[n];
	for (ll j = 1; j < n; j++) {
		ll a;
		cin >> a;
		adj[a-1].push_back(j);
	}
	ll a, b;
	vector <ll> vec;
	//bool * visited = new bool[n];
	for (ll j = 0; j < k; j++) {
		cin >> a >> b;
		dfs(adj, a - 1, vec,b);

		if (b > vec.size()) {
			cout << -1;
		}
		else {
			cout << vec[b - 1];
		}
		vec.clear();
		cout << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>

// watsonlee Coding

using namespace std;

#define ll long long

void dfs(list <ll> * adj, ll index,vector <ll> & vec,ll &t,ll * L,ll * arr) {
	L[index] = ++t;
	vec.push_back(index+1);
	list <ll> ::iterator it = adj[index].begin();
	for (; it != adj[index].end(); it++) {
		dfs(adj, *it, vec, t,L,arr);
	}
	arr[index] = t - L[index];
}

int main() {
	ll n, k;
	cin >> n >> k;
	list <ll> * adj;
	adj = new list <ll>[n];
	for (ll j = 1; j < n; j++) {
		ll a;
		cin >> a;
		adj[a-1].push_back(j);
	}
	ll * L = new ll[n];
	ll * arr = new ll[n];
	for (ll j = 0; j < n; j++) {
		L[j] = 0;
		arr[j] = 0;
	}
	ll a, b;
	ll t = 0;
	vector <ll> vec;
	dfs(adj, 0, vec, t, L,arr);
	for (ll j = 0; j < k; j++) {
		cin >> a >> b;
		if (b > arr[a - 1] + 1) {
			cout << -1;
		}
		else {
			cout << vec[b + L[a - 1] - 2];
		}
		cout << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>

// watsonlee Coding

using namespace std;

#define ll long long

void dfs(list <ll> * adj, ll index, bool * visited,vector <ll> & vec,ll b) {
	visited[index] = true;
	vec.push_back(index + 1);
	if (b!= vec.size()) {
		list <ll> ::iterator it = adj[index].begin();
		for (; it != adj[index].end(); it++) {
			if (!visited[*it]) {
				dfs(adj, *it, visited, vec,b);
			}
		}
	}
}

int main() {
	ll n, k;
	cin >> n >> k;
	list <ll> * adj;
	adj = new list <ll>[n];
	for (ll j = 1; j < n; j++) {
		ll a;
		cin >> a;
		adj[a-1].push_back(j);
	}
	ll a, b;
	vector <ll> vec;
	bool * visited = new bool[n];
	for (ll j = 0; j < k; j++) {
		cin >> a >> b;
		for (ll j = 0; j < n; j++) {
			visited[j] = false;
		}
		dfs(adj, a - 1, visited, vec,b);

		if (b > vec.size()) {
			cout << -1;
		}
		else {
			cout << vec[b - 1];
		}
		vec.clear();
		cout << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>

// watsonlee Coding

using namespace std;

#define ll long long

void dfs(list <ll> * adj, ll index,vector <ll> & vec,ll b) {
	vec.push_back(index + 1);
		list <ll> ::iterator it = adj[index].begin();
		for (; it != adj[index].end(); it++) {
				dfs(adj, *it, vec,b);
		}
}

int main() {
	ll n, k;
	cin >> n >> k;
	list <ll> * adj;
	adj = new list <ll>[n];
	for (ll j = 1; j < n; j++) {
		ll a;
		cin >> a;
		adj[a-1].push_back(j);
	}
	ll a, b;
	vector <ll> vec;
	//bool * visited = new bool[n];
	map <ll, vector <ll>> mp;
	for (ll j = 0; j < k; j++) {
		cin >> a >> b;
		if (mp[a - 1].size() <= 0) {
			dfs(adj, a - 1, vec, b);
			mp[a - 1] = vec;
		}
		if (b > mp[a-1].size()) {
			cout << -1;
		}
		else {
			cout << mp[a-1].at(b - 1);
		}
		vec.clear();
		cout << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <list>

// watsonlee Coding

using namespace std;

#define ll long long

void dfs(list <ll> * adj, ll index, bool * visited,vector <ll> & vec) {
	visited[index] = true;
	vec.push_back(index + 1);
	list <ll> ::iterator it = adj[index].begin();
	for (; it != adj[index].end(); it++) {
		if (!visited[*it]) {
			dfs(adj, *it, visited, vec);
		}
	}
}

int main() {
	ll n, k;
	cin >> n >> k;
	list <ll> * adj;
	adj = new list <ll>[n];
	for (ll j = 1; j < n; j++) {
		ll a;
		cin >> a;
		adj[a-1].push_back(j);
	}
	for (ll j = 0; j < k; j++) {
		ll a, b;
		cin >> a >> b;
		bool * visited = new bool[n];
		for (ll j = 0; j < n; j++) {
			visited[j] = false;
		}
		vector <ll> vec;
		dfs(adj, a - 1, visited, vec);
		if (b > vec.size()) {
			cout << -1;
		}
		else {
			cout << vec[b - 1];
		}
		cout << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<int>goes_in;
vector<int> goes_out;
int c;
void DFS(int u, vector<vector<int>> &adj, vector<int> &ans)
{
    ans.push_back(u);
    goes_in[u] = c;
    c++;
    for(int w : adj[u])
    {
        DFS(w,adj, ans);
    }
    goes_out[u] = c;
}

int main()
{
    int V, q;
    cin >> V >> q;
    goes_in.resize(V+1);
    goes_out.resize(V+1);
    c = 0;
    adj.resize(V+1);
    for(int i = 1; i <= V - 1; i++)
    {
        int temp;
        cin >> temp;
        adj[temp].push_back(i+1);
    }
    vector<int> ans;
    DFS(1, adj, ans);
    while(q--)
    {
        int u, k;
        cin >> u >> k;
        int len = goes_out[u] - goes_in[u];
        //cout << len << endl;
        if(len < k)
            cout << "-1" << endl;
        else
        {
            for(int i = 0; i < ans.size(); i++)
            {
                if(ans[i] == u)
                {
                    cout << ans[i+k-1] << endl;
                    break;
                }
            }
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<int>goes_in;
vector<int> goes_out;
unordered_map<int,int> index_of_nodes;
int c;
void DFS(int u, vector<vector<int>> &adj, vector<int> &ans)
{
    ans.push_back(u);
    index_of_nodes[u] = ans.size() - 1;
    goes_in[u] = c;
    c++;
    for(int w : adj[u])
    {
        DFS(w,adj, ans);
    }
    goes_out[u] = c;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int V, q;
    cin >> V >> q;
    goes_in.resize(V+1);
    goes_out.resize(V+1);
    c = 0;
    adj.resize(V+1);
    for(int i = 1; i <= V - 1; i++)
    {
        int temp;
        cin >> temp;
        adj[temp].push_back(i+1);
    }
    vector<int> ans;
    DFS(1, adj, ans);
    while(q--)
    {
        int u, k;
        cin >> u >> k;
        int len = goes_out[u] - goes_in[u];
        //cout << len << endl;
        if(len < k)
            cout << "-1" << endl;
        else
        {
            cout << ans[index_of_nodes[u]+k-1] << endl;
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<int>goes_in;
vector<int> goes_out;
int c;
void DFS(int u, vector<vector<int>> &adj, vector<int> &ans)
{
    ans.push_back(u);
    goes_in[u] = c;
    c++;
    for(int w : adj[u])
    {
        DFS(w,adj, ans);
    }
    goes_out[u] = c;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int V, q;
    cin >> V >> q;
    goes_in.resize(V+1);
    goes_out.resize(V+1);
    c = 0;
    adj.resize(V+1);
    for(int i = 1; i <= V - 1; i++)
    {
        int temp;
        cin >> temp;
        adj[temp].push_back(i+1);
    }
    vector<int> ans;
    DFS(1, adj, ans);
    while(q--)
    {
        int u, k;
        cin >> u >> k;
        int len = goes_out[u] - goes_in[u];
        //cout << len << endl;
        if(len < k)
            cout << "-1" << endl;
        else
        {
            for(int i = 0; i < ans.size(); i++)
            {
                if(ans[i] == u)
                {
                    cout << ans[i+k-1] << endl;
                    break;
                }
            }
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int vert;

vector<int>goes_in(vert+1);
vector<int> goes_out(vert+1);
int c = 1;
void DFS(int u, list<int> adj[], vector<int> &ans)
{
    ans.push_back(u);
    goes_in[u] = c;
    c++;
    list<int>::iterator it;
    for(it = adj[u].begin(); it != adj[u].end(); it++)
    {
        DFS(*it,adj, ans);
    }
    goes_out[u] = c;
}

int main()
{
    int V, q;
    cin >> V >> q;
    vert  = V;
    list<int> adj[V+1];
    for(int i = 1; i <= V - 1; i++)
    {
        int temp;
        cin >> temp;
        adj[temp].push_back(i+1);
    }
    vector<int> ans;
    DFS(1, adj, ans);
    while(q--)
    {
        int u, k;
        cin >> u >> k;
        int len = goes_out[u] - goes_in[u];
        //cout << len << endl;
        if(len < k)
            cout << "-1" << endl;
        else
        {
            for(int i = 0; i < ans.size(); i++)
            {
                if(ans[i] == u)
                {
                    cout << ans[i+k-1] << endl;
                    break;
                }
            }
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

void DFS(int u, list<int> adj[], vector<int> &ans, vector<bool> isLeaf)
{
    if(adj[u].size() == 0)
        isLeaf[u] = true;
    ans.push_back(u);
    list<int>::iterator it;
    for(it = adj[u].begin(); it != adj[u].end(); it++)
    {
        DFS(*it,adj, ans, isLeaf);
    }
}

int main()
{
    int V, q;
    cin >> V >> q;
    list<int> adj[V+1];
    for(int i = 1; i <= V - 1; i++)
    {
        int temp;
        cin >> temp;
        adj[temp].push_back(i+1);
    }
    vector<int> ans;
    vector<vector<int>> allDFS;
    vector<bool> isLeaf(V+1, false);
    for(int i = 1; i <= V; i++)
    {
        DFS(i, adj, ans, isLeaf);
        allDFS.push_back(ans);
        ans.clear();
    }
    while(q--)
    {
        int u, k;
        cin >> u >> k;
        int n = allDFS[u-1].size();
        if(n < k)
            cout << "-1" << endl;
        else
        {
            cout << allDFS[u-1][k-1] << endl;
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

vector<int>goes_in;
vector<int> goes_out;
int c;
void DFS(int u, list<int> adj[], vector<int> &ans)
{
    ans.push_back(u);
    goes_in[u] = c;
    c++;
    list<int>::iterator it;
    for(it = adj[u].begin(); it != adj[u].end(); it++)
    {
        DFS(*it,adj, ans);
    }
    goes_out[u] = c;
}

int main()
{
    int V, q;
    cin >> V >> q;
    goes_in.resize(V);
    goes_out.resize(V);
    c = 0;
    list<int> adj[V+1];
    for(int i = 1; i <= V - 1; i++)
    {
        int temp;
        cin >> temp;
        adj[temp].push_back(i+1);
    }
    vector<int> ans;
    DFS(1, adj, ans);
    while(q--)
    {
        int u, k;
        cin >> u >> k;
        int len = goes_out[u] - goes_in[u];
        //cout << len << endl;
        if(len < k)
            cout << "-1" << endl;
        else
        {
            for(int i = 0; i < ans.size(); i++)
            {
                if(ans[i] == u)
                {
                    cout << ans[i+k-1] << endl;
                    break;
                }
            }
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

void DFS(int u, list<int> adj[], vector<bool> &visited, vector<int> &ans)
{
    visited[u] = true;
    ans.push_back(u);
    list<int>::iterator it;
    for(it = adj[u].begin(); it != adj[u].end(); it++)
    {
        DFS(*it,adj, visited, ans);
    }
}

int main()
{
    int V, q;
    cin >> V >> q;
    list<int> adj[V+1];
    for(int i = 1; i <= V - 1; i++)
    {
        int temp;
        cin >> temp;
        //adj[i+1].push_back(temp);
        adj[temp].push_back(i+1);
    }
    while(q--)
    {
        int u, k;
        cin >> u >> k;
        vector<int> ans;
        vector<bool> visited(V+1, false);
        DFS(u, adj, visited, ans);
        if(ans.size() < k)
            cout << "-1" << endl;
        else
        {
            cout << ans[k-1] << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ak ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair

#define INF (long)1e9
#define EPS 1e-9

vector<ll> adj[100007];
ll vis[100007];
vector<ll> ans[100007];

void dfs(ll a,ll source){
	for(ll i=0;i<adj[a].size();i++){
		if(!vis[adj[a][i]]){
			vis[adj[a][i]]=1;
			ans[source].push_back(adj[a][i]);
			dfs(adj[a][i],source);
		}
	}
}

int main()
{
	ak
	ll n;cin>>n;ll k;cin>>k;
	for(ll i=2;i<=n;i++){
		ll a;cin>>a;
		if(a!=i)
		adj[a].push_back(i);
	}

	for(ll i=1;i<10004;i++)
	sort(adj[i].begin(),adj[i].end());
	for(ll i=1;i<10004;i++){
		memset(vis,0,sizeof(vis));
		ans[i].push_back(i);
		dfs(i,i);
	}

	for(ll i=0;i<k;i++){
		ll a,b;cin>>a>>b;
		ll s1=ans[a].size();
		if(b>s1)
		cout<<-1<<"\n";
		else
		cout<<ans[a][b-1]<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ak ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair

#define INF (long)1e9
#define EPS 1e-9

vector<ll> adj[200007];
ll vis[200007];
vector<ll> ans;
ll start[200007];
ll end1[200007];
map<ll,ll> m1;

void dfs(ll a){
	static int time2=0;
	vis[a]=1;
	ans.push_back(a);
	start[a]=(time2);
	for(ll i=0;i<adj[a].size();i++){
		if(!vis[adj[a][i]]){
			time2++;
			dfs(adj[a][i]);
		}
	}
	end1[a]=time2;
}

int main()
{
	ak
	ll n;cin>>n;ll k;cin>>k;
	for(ll i=2;i<=n;i++){
		ll a;cin>>a;
		adj[a].push_back(i);
	}
	dfs(1);
	for(ll i=0;i<ans.size();i++)
		m1[ans[i]]=i;
	for(ll i=0;i<k;i++){
		ll a,b;cin>>a>>b;
		if((end1[a]-start[a]+1)<k)
		cout<<-1<<"\n";
        else{
        	ll p1=m1[a];
            cout<<ans[p1+b-1]<<"\n";
        }
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ak ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair

#define INF (long)1e9
#define EPS 1e-9

vector<ll> adj[100007];
ll vis[100007];
vector<ll> ans;

void dfs(ll a,ll source){
	for(ll i=0;i<adj[a].size();i++){
		if(!vis[adj[a][i]]){
			vis[adj[a][i]]=1;
			ans.push_back(adj[a][i]);
			dfs(adj[a][i],source);
		}
	}
}

int main()
{
	ak
	ll n;cin>>n;ll k;cin>>k;
	for(ll i=2;i<=n;i++){
		ll a;cin>>a;
		if(a!=i)
		adj[a].push_back(i);
	}

	for(ll i=1;i<10004;i++)
	sort(adj[i].begin(),adj[i].end());

	for(ll i=0;i<k;i++){
		ll a,b;cin>>a>>b;
		memset(vis,0,sizeof(vis));
		ans.clear();
		ans.push_back(a);
		dfs(a,a);
		ll s1=ans.size();
		if(b>s1)
		cout<<-1<<"\n";
		else
		cout<<ans[b-1]<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ak ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair

#define INF (long)1e9
#define EPS 1e-9

vector<ll> adj[100007];
ll vis[100007];
vector<ll> ans[100007];

void dfs(ll a,ll source){
	for(ll i=0;i<adj[a].size();i++){
		if(!vis[adj[a][i]]){
			vis[adj[a][i]]=1;
			ans[source].push_back(adj[a][i]);
			dfs(adj[a][i],source);
		}
	}
}

int main()
{
	ak
	ll n;cin>>n;ll k;cin>>k;
	for(ll i=2;i<=n;i++){
		ll a;cin>>a;
		if(a!=i)
		adj[a].push_back(i);
	}

	for(ll i=1;i<100004;i++)
	sort(adj[i].begin(),adj[i].end());
	for(ll i=1;i<100004;i++){
		memset(vis,0,sizeof(vis));
		ans[i].push_back(i);
		dfs(i,i);
	}

	for(ll i=0;i<k;i++){
		ll a,b;cin>>a>>b;
		ll s1=ans[a].size();
		if(b>s1)
		cout<<-1<<"\n";
		else
		cout<<ans[a][b-1]<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ak ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair

#define INF (long)1e9
#define EPS 1e-9

vector<ll> adj[200007];
ll vis[200007];
vector<ll> ans;
ll start[200007];
ll end1[200007];
map<ll,ll> m1;

void dfs(ll a,ll *time2){
	vis[a]=1;
	ans.push_back(a);
	start[a]=(*(time2));
	for(ll i=0;i<adj[a].size();i++){
		if(!vis[adj[a][i]]){
			(*(time2))++;
			dfs(adj[a][i],time2);
		}
	}
	end1[a]=(*time2);
}

int main()
{
	ak
	ll n;cin>>n;ll k;cin>>k;
	for(ll i=2;i<=n;i++){
		ll a;cin>>a;
		if(a!=i)
		adj[a].push_back(i);
	}
	ll time1=0;
	dfs(1,&time1);
	for(ll i=0;i<ans.size();i++)
		m1[ans[i]]=i;
	for(ll i=0;i<k;i++){
		ll a,b;cin>>a>>b;
		if(end1[a]-start[a]+1<k)
		cout<<-1<<"\n";
        else{
        	ll p1=m1[a];
            cout<<ans[p1+b-1]<<"\n";
        }
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ak ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair

#define INF (long)1e9
#define EPS 1e-9

vector<ll> adj[200007];
ll vis[200007];
vector<ll> ans;
ll start[200007];
ll end1[200007];
map<ll,ll> m1;

void dfs(ll a){
	static int time2=0;
	vis[a]=1;
	ans.push_back(a);
	start[a]=(time2);
	for(ll i=0;i<adj[a].size();i++){
		if(!vis[adj[a][i]]){
			time2++;
			dfs(adj[a][i]);
		}
	}
	end1[a]=time2;
}

int main()
{
	ak
	ll n;cin>>n;ll k;cin>>k;
	for(ll i=2;i<=n;i++){
		ll a;cin>>a;
		adj[a].push_back(i);
	}
	dfs(1);
	for(ll i=0;i<ans.size();i++)
		m1[ans[i]]=i;
	for(ll i=0;i<k;i++){
		ll a,b;cin>>a>>b;
		if((end1[a]-start[a]+1)<b)
		cout<<-1<<"\n";
        else{
        	ll p1=m1[a];
            cout<<ans[p1+b-1]<<"\n";
        }
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ak ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair

#define INF (long)1e9
#define EPS 1e-9

vector<ll> adj[10007];
ll vis[10007];
vector<ll> ans[10007];

void dfs(ll a,ll source){
	for(ll i=0;i<adj[a].size();i++){
		if(!vis[adj[a][i]]){
			vis[adj[a][i]]=1;
			ans[source].push_back(adj[a][i]);
			dfs(adj[a][i],source);
		}
	}
}

int main()
{
	ak
	ll n;cin>>n;ll k;cin>>k;
	for(ll i=2;i<=n;i++){
		ll a;cin>>a;
		if(a!=i)
		adj[a].push_back(i);
	}

	for(ll i=1;i<10004;i++)
	sort(adj[i].begin(),adj[i].end());
	for(ll i=1;i<10004;i++){
		memset(vis,0,sizeof(vis));
		ans[i].push_back(i);
		dfs(i,i);
	}

	for(ll i=0;i<k;i++){
		ll a,b;cin>>a>>b;
		ll s1=ans[a].size();
		if(b>s1)
		cout<<-1<<"\n";
		else
		cout<<ans[a][b-1]<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

void dfs(bool visited[],int s,int &p,int k,int &ans,vector<int> adj[],int x,vector<int> v[])
{
      //cout<<s<<" "<<p<<endl;
      v[x].push_back(s);
      if(p==k)
      {
            ans=s;

      }

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                  p=p+1;
                  dfs(visited,adj[s][i],p,k,ans,adj,x,v);
            }

      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int a[n+3];
      for(int i=2;i<=n;i++)
            cin>>a[i];

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           int x=a[i];
           adj[x].push_back(i);
     }
     vector<int> v[n+1];
     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else if(v[u].size()!=0)
           {
                 if(k-1>=v[u].size())
                 {
                       cout<<"-1"<<endl;
                 }
                 else
                 cout<<v[u][k-1]<<endl;
           }

           else
           {
                 bool visited[n+1];
                 for(int i=0;i<=n;i++)
                 visited[i]=false;

                 int p=1;
                 int ans=-1;
                 dfs(visited,u,p,k,ans,adj,u,v);

           cout<<ans<<endl;

           }
     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;

void dfs(bool visited[],int s,int &p,int k,int &ans,vector<int> adj[],int x,vector<int> v[])
{
      //cout<<s<<" "<<p<<endl;
      v[x].push_back(s);
      if(p==k)
      {
            ans=s;

      }

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                  p=p+1;
                  dfs(visited,adj[s][i],p,k,ans,adj,x,v);
            }

      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int num;

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           cin>>num;
           adj[num].push_back(i);
     }
     vector<int> v[n+1];
     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else if(v[u].size()!=0)
           {
                 if(k-1>=v[u].size())
                 {
                       cout<<"-1"<<endl;
                 }
                 else
                 cout<<v[u][k-1]<<endl;
           }

           else
           {
                 bool visited[n+1];
                 for(int i=0;i<=n;i++)
                 visited[i]=false;

                 int p=1;
                 int ans=-1;
                 dfs(visited,u,p,k,ans,adj,u,v);

           cout<<ans<<endl;

           }
     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;

void dfs(bool visited[],int s,vector<int> adj[],vector<int >&v,int t[][2] )
{
      //cout<<s<<" "<<p<<endl;
      v.push_back(s);
      t[s][0]=v.size()-1;

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                 dfs(visited,adj[s][i],adj,v,t);
            }

      }
      t[s][1]=v.size()-1;
}
int main()
{
      int n,q;
      cin>>n>>q;
      int num;

     vector<int> adj[n+1];
     int t[n+1][2];
     for(int i=2;i<=n;i++)
     {
           cin>>num;
           adj[num].push_back(i);
     }
     vector<int> v;
      bool visited[n+1];
     for(int i=0;i<=n;i++)
     visited[i]=false;

     int p=1;
     int ans=-1;
     dfs(visited,1,adj,v,t);

     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else
           {
               int x=0;
               while(x<v.size())
               {
                     if(v[x]==u)
                     break;
                     x++;
               }
               int j=x+k-1;
               if(j>=v.size() || j>t[u][1])
               {
                     cout<<"-1"<<endl;
               }
               else
               {
                     cout<<v[j]<<endl;
               }
           }

     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;
vector<int> v[200007];
void dfs(bool visited[],int s,int &p,int k,int &ans,vector<int> adj[],int x)
{
      //cout<<s<<" "<<p<<endl;
      v[x].push_back(s);
      if(p==k)
      {
            ans=s;

      }

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                  p=p+1;
                  dfs(visited,adj[s][i],p,k,ans,adj,x);
            }

      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int a[n+3];
      for(int i=2;i<=n;i++)
            cin>>a[i];

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           int x=a[i];
           adj[x].push_back(i);
     }
     vector<int> v[n+1];
     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else if(v[u].size()!=0)
           {
                 if(k-1>=v[u].size())
                 {
                       cout<<"-1"<<endl;
                 }
                 else
                 cout<<v[u][k-1]<<endl;
           }

           else
           {
                 bool visited[n+1];
                 for(int i=0;i<=n;i++)
                 visited[i]=false;

                 int p=1;
                 int ans=-1;
                 dfs(visited,u,p,k,ans,adj,u);

           cout<<ans<<endl;

           }
     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;
vector<int> v[200001];
void dfs(bool visited[],int s,int &p,int k,int &ans,vector<int> adj[],int x)
{
      //cout<<s<<" "<<p<<endl;
      v[x].push_back(s);
      if(p==k)
      {
            ans=s;

      }

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                  p=p+1;
                  dfs(visited,adj[s][i],p,k,ans,adj,x);
            }

      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int a[n+3];
      for(int i=2;i<=n;i++)
            cin>>a[i];

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           int x=a[i];
           adj[x].push_back(i);
     }
     vector<int> v[n+1];
     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else if(v[u].size()!=0)
           {
                 if(k-1>=v[u].size())
                 {
                       cout<<"-1"<<endl;
                 }
                 else
                 cout<<v[u][k-1]<<endl;
           }

           else
           {
                 bool visited[n+1];
                 for(int i=0;i<=n;i++)
                 visited[i]=false;

                 int p=1;
                 int ans=-1;
                 dfs(visited,u,p,k,ans,adj,u);

           cout<<ans<<endl;

           }
     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;
void dfs(bool visited[],int s,int &p,int k,int &ans,vector<int> adj[])
{
      if(p==k)
      {
            ans=s;

      return;
      }

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[i])
            {
                  p=p+1;
                  dfs(visited,s,p,k,ans,adj);
            }
            if(p==k)
            return;
      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int a[n+3];
      for(int i=2;i<=n;i++)
            cin>>a[i];

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           int x=a[i];
           adj[x].push_back(i);
     }
     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
           else
           {
                 bool visited[n+1];
                 for(int i=0;i<=n;i++)
                 visited[i]=false;

                 int p=1;
                 int ans=-1;
                 dfs(visited,u,p,k,ans,adj);

           cout<<ans<<endl;

           }
     }
      return 0;
}


#include<bits/stdc++.h>
using namespace std;
void dfs(bool visited[],int s,int &p,int k,int &ans)
{
      if(p==k)
      {
            ans=s;

      return;
      }

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[i])
            {
                  p=p+1;
                  dfs(visited,s,p,k,ans);
            }
            if(p==k)
            return;
      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int a[n+3];
      for(int i=2;i<=n;i++)
            cin>>a[i];

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           int x=a[i];
           adj[x].push_back(i);
     }
     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
           else
           {
                 bool visited[n+1];
                 for(int i=0;i<=n;i++)
                 visited[i]=false;

                 int p=1;
                 int ans=-1;
                 dfs(visited,u,p,k,ans);

           cout<<ans<<endl;

           }
     }
      return 0;
}


#include<bits/stdc++.h>
using namespace std;

void dfs(bool visited[],int s,vector<int> adj[],vector<int >&v,int t[][2] )
{
      //cout<<s<<" "<<p<<endl;
      v.push_back(s);
      t[s][0]=v.size()-1;

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                 dfs(visited,adj[s][i],adj,v,t);
            }

      }
      t[s][1]=v.size()-1;
}
int main()
{
      int n,q;
      cin>>n>>q;
      int num;

     vector<int> adj[n+1];
     int t[n+1][2];
     for(int i=2;i<=n;i++)
     {
           cin>>num;
           adj[num].push_back(i);
     }
     vector<int> v;
      bool visited[n+1];
     for(int i=0;i<=n;i++)
     visited[i]=false;

     int p=1;
     int ans=-1;
     dfs(visited,1,adj,v,t);

     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else
           {
               int x=0;
               while(x<v.size())
               {
                     if(v[x]==u)
                     break;
                     x++;
               }
               int j=x+k-1;
               if(j>=v.size() || j<t[u][0] || j>t[u][1])
               {
                     cout<<"-1"<<endl;
               }
               else
               {
                     cout<<v[j]<<endl;
               }
           }

     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;

void dfs(bool visited[],int s,vector<int> adj[],vector<int >&v )
{
      //cout<<s<<" "<<p<<endl;
      v.push_back(s);

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                 dfs(visited,adj[s][i],adj,v);
            }

      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int num;

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           cin>>num;
           adj[num].push_back(i);
     }
     vector<int> v;
      bool visited[n+1];
     for(int i=0;i<=n;i++)
     visited[i]=false;

     int p=1;
     int ans=-1;
     dfs(visited,1,adj,v);

     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else
           {
               int x=0;
               while(x<v.size())
               {
                     if(v[x]==u)
                     break;
                     x++;
               }
               int j=x+k-1;
               if(j>=v.size())
               {
                     cout<<"-1"<<endl;
               }
               else
               {
                     cout<<v[j]<<endl;
               }
           }

     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;

void dfs(bool visited[],int s,int &p,int k,int &ans,vector<int> adj[])
{
      //cout<<s<<" "<<p<<endl;
      //v[x].push_back(s);
      if(p==k)
      {
            ans=s;
            return;

      }

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                  p=p+1;
                  dfs(visited,adj[s][i],p,k,ans,adj);
            }
            if(p==k)
            return;

      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int num;

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           cin>>num;
           adj[num].push_back(i);
     }
   //  vector<int> v[n+1];
     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
        /*  else if(v[u].size()!=0)
           {
                 if(k-1>=v[u].size())
                 {
                       cout<<"-1"<<endl;
                 }
                 else
                 cout<<v[u][k-1]<<endl;
           }
           */
           else
           {
                 bool visited[n+1];
                 for(int i=0;i<=n;i++)
                 visited[i]=false;

                 int p=1;
                 int ans=-1;
                 dfs(visited,u,p,k,ans,adj);

           cout<<ans<<endl;

           }
     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;
void dfs(bool visited[],int s,int &p,int k,int &ans,vector<int> adj[])
{
      //cout<<s<<" "<<p<<endl;
      if(p==k)
      {
            ans=s;

      return;
      }

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                  p=p+1;
                  dfs(visited,adj[s][i],p,k,ans,adj);
            }
            if(p==k)
            return;
      }
}
int main()
{
      int n,q;
      cin>>n>>q;
      int a[n+3];
      for(int i=2;i<=n;i++)
            cin>>a[i];

     vector<int> adj[n+1];
     for(int i=2;i<=n;i++)
     {
           int x=a[i];
           adj[x].push_back(i);
     }
     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
           else
           {
                 bool visited[n+1];
                 for(int i=0;i<=n;i++)
                 visited[i]=false;

                 int p=1;
                 int ans=-1;
                 dfs(visited,u,p,k,ans,adj);

           cout<<ans<<endl;

           }
     }
      return 0;
}


#include<bits/stdc++.h>
using namespace std;

void dfs(bool visited[],int s,vector<int> adj[],vector<int >&v,int t[][2] )
{
      //cout<<s<<" "<<p<<endl;
      v.push_back(s);
      t[s][0]=v.size()-1;

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                 dfs(visited,adj[s][i],adj,v,t);
            }

      }
      t[s][1]=v.size()-1;
}
int main()
{
      int n,q;
      cin>>n>>q;
      int num;

     vector<int> adj[n+1];
     int t[n+1][2];
     for(int i=2;i<=n;i++)
     {
           cin>>num;
           adj[num].push_back(i);
     }
     vector<int> v;
      bool visited[n+1];
     for(int i=0;i<=n;i++)
     visited[i]=false;

     int p=1;
     int ans=-1;
     dfs(visited,1,adj,v,t);

     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else
           {
               int x=0;
               while(x<v.size())
               {
                     if(v[x]==u)
                     break;
                     x++;
               }
               int j=x+k-1;
               if(j>=v.size() || j<t[s][0] || j>t[s][1])
               {
                     cout<<"-1"<<endl;
               }
               else
               {
                     cout<<v[j]<<endl;
               }
           }

     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include<bits/stdc++.h>
using namespace std;

void dfs(bool visited[],int s,vector<int> adj[],vector<int >&v,int t[][2] )
{
      //cout<<s<<" "<<p<<endl;
      v.push_back(s);
      t[s][0]=v.size()-1;

      visited[s]=true;

      for(int i=0;i<adj[s].size();i++)
      {
            if(!visited[adj[s][i]])
            {
                 dfs(visited,adj[s][i],adj,v,t);
            }

      }
      t[s][1]=v.size()-1;
}
int main()
{
      int n,q;
      cin>>n>>q;
      int num;

     vector<int> adj[n+1];
     int t[n+1][2];
     for(int i=2;i<=n;i++)
     {
           cin>>num;
           adj[num].push_back(i);
     }
     vector<int> v;
      bool visited[n+1];
     for(int i=0;i<=n;i++)
     visited[i]=false;

     int p=1;
     int ans=-1;
     dfs(visited,1,adj,v,t);

     for(int i=0;i<q;i++)
     {
           int u,k;
           cin>>u>>k;
           if(k==1)
           cout<<u<<endl;
          else
           {

               int j=t[u][0]+k-1;
               if(j>=t[u][0] &&j<=t[u][1])
               {
                     cout<<v[j]<<endl;
               }
            else
               {
                     cout<<"-1"<<endl;
               }

           }

     }
      return 0;
}
/*
3
6
8
-1
9
4
*/


#include <bits/stdc++.h>
#include<numeric>
#include<cstdlib>
#define ll long long int
#define all(x) x.begin(),x.end()

 #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define v vector<long long int>
#define vv vector< int>
#define gcd(m,n) __gcd(m,n)
#define vp vector<pair<long long int ,long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int ,long long int>
#define f(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,n) for (ll i=a ; i<n ;i+=1)
#define fe(d,e) for(auto d:e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll,ll>
# define N 100005LL
using namespace std;

v visited(N);
vector<v> graph(N);
vector<ll> ans={-1};
v level(N);
v ind(N);
ll sz[N];

void dfs(ll node, ll par){

    visited[node]=1;
    if (node==1)level[1]=0;
    else level[node] = level[par]+1;
    ans.pb(node);
    for(auto x:graph[node]){
        if (!visited[x]){
            dfs(x,node);
            sz[node]+=sz[x];

        }

    }
     ++sz[node];

}

int main(){

    fastio;

    ll n,r1,q;
    cin>>n>>q;
    graph.resize(n+1);
    level.resize(n+1);
    visited.resize(n+1);
    ind.resize(n+1);
    // vector<pair<ll,ll>> arr;
    ll t=2;
    f(i,n-1){

            ll x,y;
            cin>>x;
            graph[x].pb(t);
            graph[t].pb(x);
            t+=1;
        }
    for(ll i=1;i<=n;i++){
        sort(graph[i].begin(),graph[i].end());
    }
    dfs(1,-1);

    loop(i,1,n+1){
        ind[ans[i]]=i;
    }
    // for (auto x:ind)cout<<x<<" ";
    f(i,q){
        ll u,k;
        cin>>u>>k;
        if (k>sz[u])puts("-1");

        else cout<<ans[k-1+ind[u]]<<"\n";

    }

    }


#include <bits/stdc++.h>
#include<numeric>
#include<cstdlib>
#define ll long long int
#define all(x) x.begin(),x.end()

 #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define v vector<long long int>
#define vv vector< int>
#define gcd(m,n) __gcd(m,n)
#define vp vector<pair<long long int ,long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int ,long long int>
#define f(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,n) for (ll i=a ; i<n ;i+=1)
#define fe(d,e) for(auto d:e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll,ll>
# define N 1000000LL
using namespace std;

v visited(N);
vector<v> graph(N);
vector<ll> ans={-1};
v level(N);
v ind(N);
ll sz[N];

void dfs(ll node, ll par){

    visited[node]=1;
    if (node==1)level[1]=0;
    else level[node] = level[par]+1;
    ans.pb(node);
    for(auto x:graph[node]){
        if (!visited[x]){
            dfs(x,node);
            sz[node]+=sz[x];

        }

    }
     ++sz[node];

}

int main(){

    fastio;

    ll n,r1,q;
    cin>>n>>q;
    graph.resize(n+1);
    level.resize(n+1);
    visited.resize(n+1);
    ind.resize(n+1);
    // vector<pair<ll,ll>> arr;
    ll t=2;
    f(i,n-1){

            ll x,y;
            cin>>x;
            graph[x].pb(t);
            graph[t].pb(x);
            t+=1;
        }
    for(ll i=1;i<=n;i++){
        sort(graph[i].begin(),graph[i].end());
    }
    dfs(1,-1);

    loop(i,1,n+1){
        ind[ans[i]]=i;
    }
    // for (auto x:ind)cout<<x<<" ";
    f(i,q){
        ll u,k;
        cin>>u>>k;
        if (k>sz[u])puts("-1");

        else cout<<ans[k-1+ind[u]]<<"\n";

    }

    }


#include <bits/stdc++.h>
#include<numeric>
#include<cstdlib>
#define ll long long int
#define all(x) x.begin(),x.end()

 #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define v vector<long long int>
#define vv vector< int>
#define gcd(m,n) __gcd(m,n)
#define vp vector<pair<long long int ,long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int ,long long int>
#define f(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,n) for (ll i=a ; i<n ;i+=1)
#define fe(d,e) for(auto d:e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll,ll>
# define N 100005LL
using namespace std;

v visited(N);
vector<v> graph(N);
vector<ll> ans={-1};
v level(N);
v ind(N);

void dfs(ll node, ll par){

    visited[node]=1;
    if (node==1)level[1]=0;
    else level[node] = level[par]+1;
    ans.pb(node);
    for(auto x:graph[node]){
        if (!visited[x]){
            dfs(x,node);

        }
    }

}

int main(){

    fastio;

    ll n,r1,q;
    cin>>n>>q;
    graph.resize(n+1);
    level.resize(n+1);
    visited.resize(n+1);
    ind.resize(n+1);
    // vector<pair<ll,ll>> arr;
    ll t=2;
    f(i,n-1){

            ll x,y;
            cin>>x;
            graph[x].pb(t);
            graph[t].pb(x);
            t+=1;
        }
    for(ll i=1;i<=n;i++){
        sort(graph[i].begin(),graph[i].end());
    }
    dfs(1,-1);

    loop(i,1,n+1){
        ind[ans[i]]=i;
    }
    // for (auto x:ind)cout<<x<<" ";
    f(i,q){
        ll u,k;
        cin>>u>>k;
        if (k==1)cout<<u<<"\n";
        else if (k-1+ind[u]<=n && level[ans[k-1+ind[u]]]>level[u]){
            cout<<ans[k-1+ind[u]]<<"\n";
            // cout<<level[u]<<"\n";
            // cout<<level[ans[k-1+ind[u]]]<<"\n";

            }
        else cout<<-1<<"\n";

    }

    }


#include <bits/stdc++.h>
#include<numeric>
#include<cstdlib>
#define ll long long int
#define all(x) x.begin(),x.end()

 #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define v vector<long long int>
#define vv vector< int>
#define gcd(m,n) __gcd(m,n)
#define vp vector<pair<long long int ,long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int ,long long int>
#define f(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,n) for (ll i=a ; i<n ;i+=1)
#define fe(d,e) for(auto d:e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll,ll>
# define N 100005LL
using namespace std;

v visited(N);
vector<v> graph(N);
vector<ll> ans={-1};
v level(N);
v ind(N);
ll sz[N];

void dfs(ll node, ll par){

    visited[node]=1;
    if (node==1)level[1]=0;
    else level[node] = level[par]+1;
    ans.pb(node);
    for(auto x:graph[node]){
        if (!visited[x]){
            dfs(x,node);
            sz[node]+=sz[x];

        }

    }
     ++sz[node];

}

int main(){

    fastio;

    ll n,r1,q;
    cin>>n>>q;
    graph.resize(n+1);
    level.resize(n+1);
    visited.resize(n+1);
    ind.resize(n+1);
    // vector<pair<ll,ll>> arr;
    ll t=2;
    f(i,n-1){

            ll x,y;
            cin>>x;
            graph[x].pb(t);
            graph[t].pb(x);
            t+=1;
        }
    for(ll i=1;i<=n;i++){
        sort(graph[i].begin(),graph[i].end());
    }
    dfs(1,-1);

    loop(i,1,n+1){
        ind[ans[i]]=i;
    }
    // for (auto x:ind)cout<<x<<" ";
    f(i,q){
        ll u,k;
        cin>>u>>k;
        if (k>sz[u])cout<<-1<<"\n";

        else cout<<ans[k-1+ind[u]]<<"\n";

    }

    }


#include <bits/stdc++.h>
#include<numeric>
#include<cstdlib>
#define ll long long int
#define all(x) x.begin(),x.end()

 #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define v vector<long long int>
#define vv vector< int>
#define gcd(m,n) __gcd(m,n)
#define vp vector<pair<long long int ,long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int ,long long int>
#define f(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,n) for (ll i=a ; i<n ;i+=1)
#define fe(d,e) for(auto d:e)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll,ll>
# define N 1000000LL
using namespace std;

v visited(N);
vector<v> graph(N);
vector<ll> ans={-1};
v level(N);
v ind(N);
ll sz[N];

void dfs(ll node, ll par){

    visited[node]=1;
    if (node==1)level[1]=0;
    else level[node] = level[par]+1;
    ans.pb(node);
    for(auto x:graph[node]){
        if (!visited[x]){
            dfs(x,node);
            sz[node]+=sz[x];

        }

    }
     ++sz[node];

}

int main(){

    fastio;

    ll n,r1,q;
    cin>>n>>q;
    graph.resize(n+1);
    level.resize(n+1);
    visited.resize(n+1);
    ind.resize(n+1);
    // vector<pair<ll,ll>> arr;
    ll t=2;
    f(i,n-1){

            ll x,y;
            cin>>x;
            graph[x].pb(t);
            graph[t].pb(x);
            t+=1;
        }
    for(ll i=1;i<=n;i++){
        sort(graph[i].begin(),graph[i].end());
    }
    dfs(1,-1);

    loop(i,1,n+1){
        ind[ans[i]]=i;
    }
    // for (auto x:ind)cout<<x<<" ";
    f(i,q){
        ll u,k;
        cin>>u>>k;
        if (k>sz[u])cout<<-1<<"\n";

        else cout<<ans[k-1+ind[u]]<<"\n";

    }

    }


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
vector<int> vis(200005,0);
vector<ll> ans,v[200005],ch(200005,0);
void dfs(int s,int u){
	vis[s]=1;
	ans.pb(s);
	for(ll i=0;i<v[s].size();++i)
		if(vis[v[s][i]]==0)
			dfs(v[s][i],s);
	ch[u]+=(ch[s]+1);
}
int main(){
	fast();
	int n,q;
	cin>>n>>q;
	for(int i=2;i<=n;++i){
		int t;
		cin>>t;
		v[t].pb(i);
	}
	dfs(1,0);
	while(q--){
		int x,y;
		cin>>x>>y;
		int low=upper_bound (ans.begin(), ans.end(), x)-ans.begin();
		if(y>ch[x]+1)
			cout<<-1<<endl;
		else
			cout<<ans[low+y-2]<<endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
vector<int> vis(200005,0);
vector<ll> ans,v[200005],ch(200005,1);
void dfs(int s,int u){
	vis[s]=1;
	ans.pb(s);
	for(ll i=0;i<v[s].size();++i)
		if(vis[v[s][i]]==0)
			dfs(v[s][i],s);
	ch[u]+=ch[s];
}
int main(){
	fast();
	int n,q;
	cin>>n>>q;
	for(int i=2;i<=n;++i){
		int t;
		cin>>t;
		v[t].pb(i);
	}
	dfs(1,0);
	vector<ll> m(200005,0);
	for(int i=0;i<n;++i)
		m[ans[i]]=i+1;
	while(q--){
		int x,y;
		cin>>x>>y;
		if(y>ch[x])
			cout<<-1<<endl;
		else
			cout<<ans[m[x]+y-2]<<endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
vector<int> vis(200005,0);
vector<int> ans;
int y;
void dfs(vector<ll> v[],int s){
	vis[s]=1;
	ans.pb(s);
	if(ans.size()==y)
		return;
	for(ll i=0;i<v[s].size();++i)
		if(vis[v[s][i]]==0)
			dfs(v,v[s][i]);
}
int main(){
	fast();
	int n,q;
	cin>>n>>q;
	vector<ll> v[n+1];
	for(int i=2;i<=n;++i){
		int t;
		cin>>t;
		v[t].pb(i);
	}
	while(q--){
		int x;
		cin>>x>>y;
		ans.clear();
		for(int i=0;i<=n+5;++i)
			vis[i]=0;
		dfs(v,x);
		// return 0;
		if(y>ans.size())
			cout<<-1<<endl;
		else
			cout<<ans[y-1]<<endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
vector<int> vis(200005,0);
vector<ll> ans,v[200005],ch(200005,1);
void dfs(int s,int u){
	vis[s]=1;
	ans.pb(s);
	for(ll i=0;i<v[s].size();++i)
		if(vis[v[s][i]]==0)
			dfs(v[s][i],s);
	ch[u]+=ch[s];
}
int main(){
	fast();
	int n,q;
	cin>>n>>q;
	for(int i=2;i<=n;++i){
		int t;
		cin>>t;
		v[t].pb(i);
	}
	dfs(1,0);
	while(q--){
		int x,y;
		cin>>x>>y;
		int low=upper_bound (ans.begin(), ans.end(), x)-ans.begin();
		if(y>ch[x])
			cout<<-1<<endl;
		else
			cout<<ans[low+y-2]<<endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
vector<int> vis(200005,0);
vector<int> ans;
int y;
void dfs(vector<ll> v[],int s){
	vis[s]=1;
	ans.pb(s);
	if(ans.size()==y)
		return;
	for(ll i=0;i<v[s].size();++i)
		if(vis[v[s][i]]==0)
			dfs(v,v[s][i]);
}
int main(){
	fast();
	int n,q;
	cin>>n>>q;
	vector<ll> v[n+1];
	for(int i=2;i<=n;++i){
		int t;
		cin>>t;
		v[t].pb(i);
	}
	for(int i=1;i<=n;++i)
		sort(v[i].begin(),v[i].end());
	while(q--){
		int x;
		cin>>x>>y;
		ans.clear();
		for(int i=0;i<=n+5;++i)
			vis[i]=0;
		dfs(v,x);
		// return 0;
		if(y>ans.size())
			cout<<-1<<endl;
		else
			cout<<ans[y-1]<<endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pi 3.1415926535897932384
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
vector<int> vis(200005,0);
vector<int> ans;
void dfs(vector<ll> v[],int s){
	vis[s]=1;
	ans.pb(s);
	for(ll i=0;i<v[s].size();++i)
		if(vis[v[s][i]]==0)
			dfs(v,v[s][i]);
}
int main(){
	fast();
	int n,q;
	cin>>n>>q;
	vector<ll> v[n+1];
	for(int i=2;i<=n;++i){
		int t;
		cin>>t;
		v[t].pb(i);
	}
	for(int i=1;i<=n;++i)
		sort(v[i].begin(),v[i].end());
	while(q--){
		int x,y;
		cin>>x>>y;
		ans.clear();
		for(int i=0;i<=n+5;++i)
			vis[i]=0;
		dfs(v,x);
		// return 0;
		if(y>ans.size())
			cout<<-1<<endl;
		else
			cout<<ans[y-1]<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;

void dfs2(vector<int>adj[],int start,int finish,vector<int>& pp)
{

        for(int i:adj[start])
        {
            if(i!=finish)
            {

            pp.push_back(i);
            dfs2(adj,i,start,pp);

            }

        }

}

void dfs(vector<int>adj[],int parent[],int start,int finish)
{

        for(int i:adj[start])
        {
            if(i!=finish)
            {
            parent[i]=start;
            dfs(adj,parent,i,start);

            }

        }

}

int main()
{

     ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,q;
    cin>>n>>q;

    vector<int>adj[n+1];

    int parent[n+1];

    memset(parent,0,sizeof(parent));

    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        adj[i].push_back(x);
        adj[x].push_back(i);

    }

    dfs(adj,parent,1,-1);

parent[1]=-1;

    while(q--)
    {

     int u,k;

    vector<int>pp;
     cin>>u>>k;

        dfs2(adj,u,parent[u],pp);

        if(k==1)
        {
            cout<<u<<"\n";
        }
        else if(pp.size()<k-1)
        {
            cout<<-1<<"\n";
        }

        else
        {

            cout<<pp[k-2]<<"\n";
        }
pp.clear();

    }

}


#include<bits/stdc++.h>
using namespace std;

 vector<int>adj[100000];
    vector<int>ans[100000];

int dfs(int start,int ss)
{

for(int i:adj[start])
{

    ans[ss].push_back(i);

    dfs(i,ss);

}

}

int main()
{

    int n,q;
    cin>>n>>q;

    //memset(visit,false,sizeof(visit));

    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        adj[x].push_back(i);

    }

    for(int i=1;i<=n;i++)
    {

        dfs(i,i);
    }

/*for(int i=0;i<=ans[1].size();i++)
{

    cout<<i<<" -> "<<ans[1][i]<<endl;
}
*/

    while(q--)
    {

        int u,v;
        cin>>u>>v;

        if(v==1)
        {

            cout<<u<<endl;
        }
       else  if(ans[u].size()<v-1)
        {

            cout<<-1<<endl;
        }

        else
        {

            cout<<ans[u][v-2]<<endl;
        }

    }

}


#include<bits/stdc++.h>
using namespace std;

void dfs(vector<int>adj[],int child[],int start,int finish,vector<int>&hello)
{

hello.push_back(start);
child[start]=1;
        for(int i:adj[start])
        {
            if(i!=finish)
            {

            dfs(adj,child,i,start,hello);
            child[start]+=child[i];

            }

        }

}

int main()
{

     ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,q;
    cin>>n>>q;

    vector<int>adj[n+1];
    vector<int >hello;

    int child[n+1];

    memset(child,0,sizeof(child));

    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        adj[i].push_back(x);
        adj[x].push_back(i);

    }

    dfs(adj,child,1,-1,hello);

    int var[n+1];

int count1=1;
for(int i:hello)
{

    var[i]=count1;
    count1++;
}

//    cout<<endl<<endl;
    while(q--)
    {

     int u,k;

     cin>>u>>k;

     if(k>child[u])
     {
         cout<<-1<<endl;
     }

     else
     {

         cout<<hello[var[u]+k-2]<<endl;
     }

    }

}


#include<bits/stdc++.h>
using namespace std;

int dfs(vector<int>adj[],vector<int>ans[],int start,int ss)
{

for(int i:adj[start])
{

    ans[ss].push_back(i);

    dfs(adj,ans,i,ss);

}

}

int main()
{

    int n,q;
    cin>>n>>q;
    vector<int>adj[n+1];
    vector<int>ans[n+1];
    bool visit[n+1];
    memset(visit,false,sizeof(visit));

    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        adj[x].push_back(i);

    }

    for(int i=1;i<=n;i++)
    {

        dfs(adj,ans,i,i);
    }

/*for(int i=0;i<=ans[1].size();i++)
{

    cout<<i<<" -> "<<ans[1][i]<<endl;
}
*/

    while(q--)
    {

        int u,v;
        cin>>u>>v;

        if(v==1)
        {

            cout<<u<<endl;
        }
       else  if(ans[u].size()<v-1)
        {

            cout<<-1<<endl;
        }

        else
        {

            cout<<ans[u][v-2]<<endl;
        }

    }

}


#include<bits/stdc++.h>
using namespace std;

 vector<int>adj[10000];
    vector<int>ans[10000];

int dfs(int start,int ss)
{

for(int i:adj[start])
{

    ans[ss].push_back(i);

    dfs(i,ss);

}

}

int main()
{

    int n,q;
    cin>>n>>q;

    //memset(visit,false,sizeof(visit));

    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        adj[x].push_back(i);

    }

    for(int i=1;i<=n;i++)
    {

        dfs(i,i);
    }

/*for(int i=0;i<=ans[1].size();i++)
{

    cout<<i<<" -> "<<ans[1][i]<<endl;
}
*/

    while(q--)
    {

        int u,v;
        cin>>u>>v;

        if(v==1)
        {

            cout<<u<<endl;
        }
       else  if(ans[u].size()<v-1)
        {

            cout<<-1<<endl;
        }

        else
        {

            cout<<ans[u][v-2]<<endl;
        }

    }

}


#include<bits/stdc++.h>
using namespace std;

int dfs(vector<int>adj[],vector<int>ans[],int start,int ss)
{

for(int i:adj[start])
{

    ans[ss].push_back(i);

    dfs(adj,ans,i,ss);

}

}

int main()
{

    int n,q;
    cin>>n>>q;
    vector<int>adj[n+1];
    vector<int>ans[n+1];
    bool visit[n+1];
    memset(visit,false,sizeof(visit));

    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        adj[x].push_back(i);

    }

    for(int i=1;i<=n;i++)
    {

        dfs(adj,ans,i,i);
    }

for(int i=0;i<=ans[1].size();i++)
{

    cout<<i<<" -> "<<ans[1][i]<<endl;
}

    while(q--)
    {

        int u,v;
        cin>>u>>v;

        if(v==1)
        {

            cout<<u<<endl;
        }
       else  if(ans[u].size()<v)
        {

            cout<<-1<<endl;
        }

        else
        {

            cout<<ans[u][v-2]<<endl;
        }

    }

}


#include<bits/stdc++.h>
using namespace std;

void dfs2(vector<int>adj[],int start,int finish,vector<int>& pp)
{

        for(int i:adj[start])
        {
            if(i!=finish)
            {

            pp.push_back(i);
            dfs2(adj,i,start,pp);

            }

        }

}

void dfs(vector<int>adj[],int parent[],int start,int finish)
{

        for(int i:adj[start])
        {
            if(i!=finish)
            {
            parent[i]=start;
            dfs(adj,parent,i,start);

            }

        }

}

int main()
{

    int n,q;
    cin>>n>>q;

    vector<int>adj[n+1];

    int parent[n+1];

    memset(parent,0,sizeof(parent));

    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        adj[i].push_back(x);
        adj[x].push_back(i);

    }

    dfs(adj,parent,1,-1);

parent[1]=-1;

    while(q--)
    {

     int u,k;

    vector<int>pp;
     cin>>u>>k;

        dfs2(adj,u,parent[u],pp);

        if(k==1)
        {
            cout<<u<<endl;
        }
        else if(pp.size()<k-1)
        {
            cout<<-1<<endl;
        }

        else
        {

            cout<<pp[k-2]<<endl;
        }
pp.clear();

    }

}


#include<bits/stdc++.h>
using namespace std;

int dfs(vector<int>adj[],vector<int>ans[],int start,int ss)
{

for(int i:adj[start])
{

    ans[ss].push_back(i);

    dfs(adj,ans,i,ss);

}

}

int main()
{

    int n,q;
    cin>>n>>q;
    vector<int>adj[n+1];
    vector<int>ans[n+1];

    //memset(visit,false,sizeof(visit));

    for(int i=2;i<=n;i++)
    {
        int x;
        cin>>x;
        adj[x].push_back(i);

    }

    for(int i=1;i<=n;i++)
    {

        dfs(adj,ans,i,i);
    }

/*for(int i=0;i<=ans[1].size();i++)
{

    cout<<i<<" -> "<<ans[1][i]<<endl;
}
*/

    while(q--)
    {

        int u,v;
        cin>>u>>v;

        if(v==1)
        {

            cout<<u<<endl;
        }
       else  if(ans[u].size()<v-1)
        {

            cout<<-1<<endl;
        }

        else
        {

            cout<<ans[u][v-2]<<endl;
        }

    }

}


/******************************************************/
/******************************************************/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         Al-Shahriar Tonmoy - Red_Sparkle         **/
/**  Department of Computer Science and Engineering  **/
/**               Barisal University                 **/
/**               It's time to bloom.                **/
/******************************************************/
/******************************************************/

#include<bits/stdc++.h>
using namespace std;
#define     ll              long long int
#define     ul              unsigned long long int
#define     dob             double
#define     st              string
#define     ch              char
#define     pairl           pair<ll,ll>
#define     pairs           pair<string,int>
#define     pairi           pair<int,string>
#define     pairss          pair<string,string>
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     fr              first
#define     se              second
#define     T               true
#define     F               false
#define     sqr(x)          pow(x,2)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     maxi            1000000000000014
#define     mini            -100000000000014
#define     mod             1000000007
#define     an              2000005
#define     maps            map<string,int>
#define     mapi            map<int,int>
#define     pi              push_back
#define     po              pop_back
#define     ve              vector<ll>
#define     mp              make_pair

ve b,a[an],ts;
bool c[an];
int u[an],v[an],xi[an];
mapi mpe;

void DFS(int source)
{

    c[source]=T;

    //for(int i=0;i<a[source].size();i++) cout<<a[source][i]<<" ";
    //cout<<endl;
    if(a[source].size()==0) x[source]=1;
    for(int i=0;i<a[source].size();i++){

        if(c[a[source][i]]==F){

            DFS(a[source][i]);

        }
        x[source]+=x[a[source][i]];

    }
    ts.pi(source);
    return;

}

int main()
{

    int n,k;

    cin>>n>>k;

    for(int i=1;i<n;i++){

        int p;

        cin>>p;

        a[p].pi(i+1);

    }

    for(int i=0;i<=n;i++) reverse(all(a[i]));

    for(int i=0;i<=n;i++) b.pi(i);

    for(int i=1;i<b.size();i++) if(c[b[i]]==F) DFS(b[i]);

    reverse(all(ts));

    for(int i=0;i<k;i++){

        cin>>u[i]>>v[i];

    }

    for(int i=0;i<ts.size();i++) mpe[ts[i]]=i;

    int c;

    //for(int i=0;i<ts.size();i++) cout<<ts[i]<<" ";
    //cout<<endl;

    for(int i=0;i<k;i++){

        c=mpe[u[i]]+v[i]-1;
        if(c<n && v[i]<=x[u[i]]) cout<<ts[c]<<endl;
        else cout<<-1<<endl;

    }

	return 0;
}
/*****************  ALHAMDULILLAH  *****************/


/******************************************************/
/******************************************************/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         Al-Shahriar Tonmoy - Red_Sparkle         **/
/**  Department of Computer Science and Engineering  **/
/**               Barisal University                 **/
/**               It's time to bloom.                **/
/******************************************************/
/******************************************************/

#include<bits/stdc++.h>
using namespace std;
#define     ll              long long int
#define     ul              unsigned long long int
#define     dob             double
#define     st              string
#define     ch              char
#define     pairl           pair<ll,ll>
#define     pairs           pair<string,int>
#define     pairi           pair<int,string>
#define     pairss          pair<string,string>
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     fr              first
#define     se              second
#define     T               true
#define     F               false
#define     sqr(x)          pow(x,2)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     maxi            1000000000000014
#define     mini            -100000000000014
#define     mod             1000000007
#define     an              2000005
#define     maps            map<string,int>
#define     mapi            map<int,int>
#define     pi              push_back
#define     po              pop_back
#define     ve              vector<ll>
#define     mp              make_pair

ve b,a[an],ts;
bool c[an];
int u[an],v[an],xi[an];
mapi mpe;

void DFS(int source)
{

    c[source]=T;

    //for(int i=0;i<a[source].size();i++) cout<<a[source][i]<<" ";
    //cout<<endl;
    if(a[source].size()==0) xi[source]=1;
    for(int i=0;i<a[source].size();i++){

        if(c[a[source][i]]==F){

            DFS(a[source][i]);

        }
        xi[source]+=xi[a[source][i]]+1;

    }
    ts.pi(source);
    return;

}

int main()
{

    int n,k;

    cin>>n>>k;

    for(int i=1;i<n;i++){

        int p;

        cin>>p;

        a[p].pi(i+1);

    }

    for(int i=0;i<=n;i++) reverse(all(a[i]));

    for(int i=0;i<=n;i++) b.pi(i);

    for(int i=1;i<b.size();i++) if(c[b[i]]==F) DFS(b[i]);

    reverse(all(ts));

    for(int i=0;i<k;i++){

        cin>>u[i]>>v[i];

    }

    for(int i=0;i<ts.size();i++) mpe[ts[i]]=i;

    int c;

    //for(int i=0;i<ts.size();i++) cout<<ts[i]<<" ";
    //cout<<endl;

    for(int i=0;i<k;i++){

        c=mpe[u[i]]+v[i]-1;
        if(v[i]<xi[u[i]]) cout<<ts[c]<<endl;
        else cout<<-1<<endl;

    }

	return 0;
}
/*****************  ALHAMDULILLAH  *****************/


/******************************************************/
/******************************************************/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         Al-Shahriar Tonmoy - Red_Sparkle         **/
/**  Department of Computer Science and Engineering  **/
/**               Barisal University                 **/
/**               It's time to bloom.                **/
/******************************************************/
/******************************************************/

#include<bits/stdc++.h>
using namespace std;
#define     ll              long long int
#define     ul              unsigned long long int
#define     dob             double
#define     st              string
#define     ch              char
#define     pairl           pair<ll,ll>
#define     pairs           pair<string,int>
#define     pairi           pair<int,string>
#define     pairss          pair<string,string>
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     fr              first
#define     se              second
#define     T               true
#define     F               false
#define     sqr(x)          pow(x,2)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     maxi            1000000000000014
#define     mini            -100000000000014
#define     mod             1000000007
#define     an              2000005
#define     maps            map<string,int>
#define     mapi            map<int,int>
#define     pi              push_back
#define     po              pop_back
#define     ve              vector<ll>
#define     mp              make_pair

ve b,a[an],ts;
bool c[an];
int u[an],v[an],xi[an];
mapi mpe;

void DFS(int source)
{

    c[source]=T;

    //for(int i=0;i<a[source].size();i++) cout<<a[source][i]<<" ";
    //cout<<endl;
    if(a[source].size()==0) xi[source]=1;
    for(int i=0;i<a[source].size();i++){

        if(c[a[source][i]]==F){

            DFS(a[source][i]);

        }
        xi[source]+=xi[a[source][i]]+1;

    }
    ts.pi(source);
    return;

}

int main()
{

    int n,k;

    cin>>n>>k;

    for(int i=1;i<n;i++){

        int p;

        cin>>p;

        a[p].pi(i+1);

    }

    for(int i=0;i<=n;i++) reverse(all(a[i]));

    for(int i=0;i<=n;i++) b.pi(i);

    for(int i=1;i<b.size();i++) if(c[b[i]]==F) DFS(b[i]);

    reverse(all(ts));

    for(int i=0;i<k;i++){

        cin>>u[i]>>v[i];

    }

    for(int i=0;i<ts.size();i++) mpe[ts[i]]=i;

    int c;

    //for(int i=0;i<ts.size();i++) cout<<ts[i]<<" ";
    //cout<<endl;

    for(int i=0;i<k;i++){

        c=mpe[u[i]]+v[i]-1;
        if(c<n && v[i]<=xi[u[i]]) cout<<ts[c]<<endl;
        else cout<<-1<<endl;

    }

	return 0;
}
/*****************  ALHAMDULILLAH  *****************/


/******************************************************/
/******************************************************/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         Al-Shahriar Tonmoy - Red_Sparkle         **/
/**  Department of Computer Science and Engineering  **/
/**               Barisal University                 **/
/**               It's time to bloom.                **/
/******************************************************/
/******************************************************/

#include<bits/stdc++.h>
using namespace std;
#define     ll              long long int
#define     ul              unsigned long long int
#define     dob             double
#define     st              string
#define     ch              char
#define     pairl           pair<ll,ll>
#define     pairs           pair<string,int>
#define     pairi           pair<int,string>
#define     pairss          pair<string,string>
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     fr              first
#define     se              second
#define     T               true
#define     F               false
#define     sqr(x)          pow(x,2)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     maxi            1000000000000014
#define     mini            -100000000000014
#define     mod             1000000007
#define     an              2000005
#define     maps            map<string,int>
#define     mapi            map<int,int>
#define     pi              push_back
#define     po              pop_back
#define     ve              vector<ll>
#define     mp              make_pair

ve b,a[an],ts;
bool c[an];
int u[an],v[an],xi[an];
mapi mpe;

void DFS(int source)
{

    c[source]=T;

    //for(int i=0;i<a[source].size();i++) cout<<a[source][i]<<" ";
    //cout<<endl;
    if(a[source].size()==0) xi[source]=1;
    for(int i=0;i<a[source].size();i++){

        if(c[a[source][i]]==F){

            DFS(a[source][i]);

            xi[source]+=(xi[a[source][i]]+1);

        }

    }
    ts.pi(source);
    return;

}

int main()
{

    int n,k;

    cin>>n>>k;

    for(int i=1;i<n;i++){

        int p;

        cin>>p;

        a[p].pi(i+1);

    }

    for(int i=0;i<=n;i++) reverse(all(a[i]));

    DFS(1);

    reverse(all(ts));

    for(int i=0;i<k;i++){

        cin>>u[i]>>v[i];

    }

    for(int i=0;i<ts.size();i++) mpe[ts[i]]=i;

    int c;

    //for(int i=0;i<ts.size();i++) cout<<ts[i]<<" ";
    //cout<<endl;

    for(int i=0;i<k;i++){

        c=mpe[u[i]]+v[i]-1;
        if(v[i]<=xi[u[i]]) cout<<ts[c]<<endl;
        else cout<<-1<<endl;

    }

	return 0;
}
/*****************  ALHAMDULILLAH  *****************/


/******************************************************/
/******************************************************/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         Al-Shahriar Tonmoy - Red_Sparkle         **/
/**  Department of Computer Science and Engineering  **/
/**               Barisal University                 **/
/**               It's time to bloom.                **/
/******************************************************/
/******************************************************/

#include<bits/stdc++.h>
using namespace std;
#define     ll              long long int
#define     ul              unsigned long long int
#define     dob             double
#define     st              string
#define     ch              char
#define     pairl           pair<ll,ll>
#define     pairs           pair<string,int>
#define     pairi           pair<int,string>
#define     pairss          pair<string,string>
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     fr              first
#define     se              second
#define     T               true
#define     F               false
#define     sqr(x)          pow(x,2)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     maxi            1000000000000014
#define     mini            -100000000000014
#define     mod             1000000007
#define     an              2000005
#define     maps            map<string,int>
#define     mapi            map<int,int>
#define     pi              push_back
#define     po              pop_back
#define     ve              vector<ll>
#define     mp              make_pair

ve b,a[an],ts;
bool c[an];
int u[an],v[an],xi[an];
mapi mpe;

void DFS(int source)
{

    c[source]=T;

    //for(int i=0;i<a[source].size();i++) cout<<a[source][i]<<" ";
    //cout<<endl;
    xi[source]=1;
    for(int i=0;i<a[source].size();i++){

        if(c[a[source][i]]==F){

            DFS(a[source][i]);

            xi[source]+=(xi[a[source][i]]);

        }

    }
    //cout<<endl;
    //cout<<xi[source]<<" "<<source<<endl;
    //cout<<endl;
    ts.pi(source);
    return;

}

int main()
{

    int n,k;

    cin>>n>>k;

    for(int i=1;i<n;i++){

        int p;

        cin>>p;

        a[p].pi(i+1);

    }

    for(int i=0;i<=n;i++) reverse(all(a[i]));

    DFS(1);

    reverse(all(ts));

    for(int i=0;i<k;i++){

        cin>>u[i]>>v[i];

    }

    for(int i=0;i<ts.size();i++) mpe[ts[i]]=i;

    int c;

    //for(int i=0;i<ts.size();i++) cout<<ts[i]<<" ";
    //cout<<endl;

    for(int i=0;i<k;i++){

        c=mpe[u[i]]+v[i]-1;
        //cout<<xi[u[i]]<<" ";
        if(v[i]<=xi[u[i]]) cout<<ts[c]<<endl;
        else cout<<-1<<endl;

    }

	return 0;
}
/*****************  ALHAMDULILLAH  *****************/


/******************************************************/
/******************************************************/
/**            BISMILLAHIR RAHMANIR RAHIM            **/
/**         Al-Shahriar Tonmoy - Red_Sparkle         **/
/**  Department of Computer Science and Engineering  **/
/**               Barisal University                 **/
/**               It's time to bloom.                **/
/******************************************************/
/******************************************************/

#include<bits/stdc++.h>
using namespace std;
#define     ll              long long int
#define     ul              unsigned long long int
#define     dob             double
#define     st              string
#define     ch              char
#define     pairl           pair<ll,ll>
#define     pairs           pair<string,int>
#define     pairi           pair<int,string>
#define     pairss          pair<string,string>
#define     max3(a,b,c)     max(a,max(b,c))
#define     max4(a,b,c,d)   max(max3(a,b,c),d)
#define     min3(a,b,c)     min(a,min(b,c))
#define     min4(a,b,c,d)   min(a,min3(b,c,d))
#define     fr              first
#define     se              second
#define     T               true
#define     F               false
#define     sqr(x)          pow(x,2)
#define     all(v)          v.begin(),v.end()
#define     fast            ios_base::sync_with_stdio(0);cin.tie(0)
#define     mem(a,v)        memset(a,v,sizeof(a))
#define     maxi            1000000000000014
#define     mini            -100000000000014
#define     mod             1000000007
#define     an              2000005
#define     maps            map<string,int>
#define     mapi            map<int,int>
#define     pi              push_back
#define     po              pop_back
#define     ve              vector<ll>
#define     mp              make_pair

ve b,a[an],ts;
bool c[an];
int u[an],v[an],xi[an];
mapi mpe;

void DFS(int source)
{

    c[source]=T;

    //for(int i=0;i<a[source].size();i++) cout<<a[source][i]<<" ";
    //cout<<endl;
    if(a[source].size()==0) xi[source]=1;
    for(int i=0;i<a[source].size();i++){

        if(c[a[source][i]]==F){

            DFS(a[source][i]);

        }
        xi[source]+=xi[a[source][i]]+1;

    }
    ts.pi(source);
    return;

}

int main()
{

    int n,k;

    cin>>n>>k;

    for(int i=1;i<n;i++){

        int p;

        cin>>p;

        a[p].pi(i+1);

    }

    for(int i=0;i<=n;i++) reverse(all(a[i]));

    DFS(1);

    reverse(all(ts));

    for(int i=0;i<k;i++){

        cin>>u[i]>>v[i];

    }

    for(int i=0;i<ts.size();i++) mpe[ts[i]]=i;

    int c;

    //for(int i=0;i<ts.size();i++) cout<<ts[i]<<" ";
    //cout<<endl;

    for(int i=0;i<k;i++){

        c=mpe[u[i]]+v[i]-1;
        if(v[i]<xi[u[i]]) cout<<ts[c]<<endl;
        else cout<<-1<<endl;

    }

	return 0;
}
/*****************  ALHAMDULILLAH  *****************/


#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+7;
const int INF=2e9;

int chk[N*2],n,k;

vector<vector<int>>vt,cnt;

int dfs(){
    vector<pii> idx;
    queue<pii> Q;
    Q.push({1,1});
    while(!Q.empty()){
        pii fr=Q.front();
        Q.pop();
        idx.push_back(fr);
        for(auto &next:vt[fr.se])Q.push({fr.fi+1,next});
    }
    sort(idx.begin(),idx.end());
    for(int r=idx.size()-1;r>=0;r--){
        pii i=idx[r];
        int sum=1;
        for(int j=0;j<vt[i.se].size();j++){
            sum+=chk[vt[i.se][j]];
            cnt[i.se][j]=sum;
        }
        chk[i.se]=sum;
    }
}
void solve(int s,int e){
    if(e==1){
        printf("%d\n",s);
        return;
    }
    int x=lower_bound(cnt[s].begin(),cnt[s].end(),e)-cnt[s].begin();
    if(cnt[s][x]==INF){
        printf("-1\n");
        return;
    }
    if(x==0)solve(vt[s][x],e-1);
    else solve(vt[s][x],e-cnt[s][x-1]);
}
int main(void){
    //freopen("input.txt","r",stdin);

    scanf("%d %d",&n,&k);
    vt.resize(n+1);
    cnt.resize(n+1);
    for(int i=2;i<=n;i++){
        int x;
        scanf("%d",&x);
        vt[x].push_back(i);
    }
    for(int i=1;i<=n;i++)sort(vt[i].begin(),vt[i].end()),cnt[i].resize(vt[i].size()),cnt[i].push_back(INF);
    dfs();
    while(k--){
        int s,e;
        scanf("%d %d",&s,&e);
        solve(s,e);
    }
}


#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+7;
const int INF=2e9;

int n,k;

vector<vector<int>>vt,cnt;

int dfs(int x){
    int sum=1;

    for(int i=0;i<vt[x].size();i++){
        sum+=dfs(vt[x][i]);
        cnt[x][i]=sum;
    }
    return sum;
}
void solve(int s,int e){
    if(e==1){
        printf("%d\n",s);
        return;
    }
    int x=lower_bound(cnt[s].begin(),cnt[s].end(),e)-cnt[s].begin();
    if(cnt[s][x]==INF){
        printf("-1\n");
        return;
    }
    if(x==0)solve(vt[s][x],e-1);
    else solve(vt[s][x],e-cnt[s][x-1]);
}
int main(void){
    //freopen("input.txt","r",stdin);

    scanf("%d %d",&n,&k);
    vt.resize(n+1);
    cnt.resize(n+1);
    for(int i=2;i<=n;i++){
        int x;
        scanf("%d",&x);
        vt[x].push_back(i);
    }
    for(int i=1;i<=n;i++)sort(vt[i].begin(),vt[i].end()),cnt[i].resize(vt[i].size()),cnt[i].push_back(INF);

    dfs(1);
    while(k--){
        int s,e;
        scanf("%d %d",&s,&e);
        solve(s,e);
    }
}


#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+7;
const int INF=2e9;

int n,k;

vector<vector<int>>vt,cnt;

int dfs(int x){
    int sum=1;

    for(int i=0;i<vt[x].size();i++){
        sum+=dfs(vt[x][i]);
        cnt[x][i]=sum;
    }
    return sum;
}
void solve(int s,int e){
    if(e==0)printf("1"),exit(0);
    if(e==1){
        printf("%d\n",s);
        return;
    }
    int x=lower_bound(cnt[s].begin(),cnt[s].end(),e)-cnt[s].begin();
    if(cnt[s][x]==INF){
        printf("-1\n");
        return;
    }
    if(x==0)solve(vt[s][x],e-1);
    else solve(vt[s][x],e-cnt[s][x-1]);
}
int main(void){
    //freopen("input.txt","r",stdin);

    scanf("%d %d",&n,&k);
    vt.resize(n+1);
    cnt.resize(n+1);
    for(int i=2;i<=n;i++){
        int x;
        scanf("%d",&x);
        vt[x].push_back(i);
    }
    for(int i=1;i<=n;i++)sort(vt[i].begin(),vt[i].end()),cnt[i].resize(vt[i].size()),cnt[i].push_back(INF);

    dfs(1);
    while(k--){
        int s,e;
        scanf("%d %d",&s,&e);
        solve(s,e);
    }
}


#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+7;
const int INF=2e9;

int n,k;

vector<vector<int>>vt,cnt;

int dfs(int x){
    int sum=1;

    for(int i=0;i<vt[x].size();i++){
        sum+=dfs(vt[x][i]);
        cnt[x][i]=sum;
    }
    return sum;
}
void solve(int s,int e){
    if(e==1){
        printf("%d\n",s);
        return;
    }
    int x=lower_bound(cnt[s].begin(),cnt[s].end(),e)-cnt[s].begin();
    if(cnt[s][x]==INF){
        printf("-1\n");
        return;
    }
    if(x==0)solve(vt[s][x],e-1);
    else solve(vt[s][x],e-cnt[s][x-1]);
}
int main(void){
    //freopen("input.txt","r",stdin);

    scanf("%d %d",&n,&k);
    vt.resize(n+1);
    cnt.resize(n+1);
    for(int i=2;i<=n;i++){
        int x;
        scanf("%d",&x);
        vt[x].push_back(i);
    }
    for(int i=1;i<=n;i++)sort(vt[i].begin(),vt[i].end()),cnt[i].resize(vt[i].size()),cnt[i].push_back(INF);
    if(n>10000)return 0;
    dfs(1);

    while(k--){
        int s,e;
        scanf("%d %d",&s,&e);
        solve(s,e);
    }
}


#include<bits/stdc++.h>

using namespace std;
const int N=2e5+20;

int n,k,x,y,dfn[N],id[N],ed[N];
vector<int>G[N];
int sz=0;
void dfs(int x){
    dfn[x]=++sz;
    id[sz]=x;
    for(int i=0;i<G[x].size();i++){
        dfs(G[x][i]);
    }
    ed[x]=sz;
}
int main(void){
    //freopen("input.txt","r",stdin);

    scanf("%d%d",&n,&k);
    for(int i=2;i<=n;i++){
        scanf("%d",&x);
        G[x].push_back(i);
    }
    dfs(1);
    for(int i=0;i<k;i++){
        scanf("%d%d",&x,&y);
        int to=dfn[x]-1+y;
        if(to>ed[x])printf("-1\n");
        else printf("%d\n",id[to]);
    }
}


#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+7;
const int INF=2e9;

int n,k;

vector<vector<int>>vt,cnt;

int dfs(int x){
    int sum=1;

    for(int i=0;i<vt[x].size();i++){
        sum+=dfs(vt[x][i]);
        cnt[x][i]=sum;
    }
    return sum;
}
void solve(int s,int e){
    if(e==1){
        printf("%d\n",s);
        return;
    }
    int x=lower_bound(cnt[s].begin(),cnt[s].end(),e)-cnt[s].begin();
    if(cnt[s][x]==INF){
        printf("-1\n");
        return;
    }
    if(x==0)solve(vt[s][x],e-1);
    else solve(vt[s][x],e-cnt[s][x-1]);
}
int main(void){
    //freopen("input.txt","r",stdin);

    scanf("%d %d",&n,&k);
    vt.resize(n+1);
    cnt.resize(n+1);
    for(int i=2;i<=n;i++){
        int x;
        scanf("%d",&x);
        vt[x].push_back(i);
    }
    for(int i=1;i<=n;i++)sort(vt[i].begin(),vt[i].end()),cnt[i].resize(vt[i].size()),cnt[i].push_back(INF);
    if(n>100000)return 0;
    dfs(1);

    while(k--){
        int s,e;
        scanf("%d %d",&s,&e);
        solve(s,e);
    }
}


#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+7;
const int INF=2e9;

int n,k;

vector<vector<int>>vt,cnt;

int dfs(int x){
    int sum=1;

    for(int i=0;i<vt[x].size();i++){
        sum+=dfs(vt[x][i]);
        cnt[x][i]=sum;
    }
    return sum;
}
void solve(int s,int e){
    if(e==1){
        printf("%d\n",s);
        return;
    }
    int x=lower_bound(cnt[s].begin(),cnt[s].end(),e)-cnt[s].begin();
    if(cnt[s][x]==INF){
        printf("-1\n");
        return;
    }
    if(x==0)solve(vt[s][x],e-1);
    else solve(vt[s][x],e-cnt[s][x-1]);
}
int main(void){
    //freopen("input.txt","r",stdin);

    scanf("%d %d",&n,&k);
    vt.resize(n+1);
    cnt.resize(n+1);
    for(int i=2;i<=n;i++){
        int x;
        scanf("%d",&x);
        vt[x].push_back(i);
    }
    for(int i=1;i<=n;i++)sort(vt[i].begin(),vt[i].end()),cnt[i].resize(vt[i].size()),cnt[i].push_back(INF);

    dfs(1);
    if(n>100000)return 0;
    while(k--){
        int s,e;
        scanf("%d %d",&s,&e);
        solve(s,e);
    }
}


#include <bits/stdc++.h>

#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

using namespace std;

vector <ll> adj[int(2 * pow(10, 5) + 1)];

vector <int> seq;

void addedge(ll from, ll to)
{
    adj[from].pb(to);
}
ll tot = 1;
int dfs(bool isvisited[], ll v, int cnt[])
{
    int c = 1;
    seq.pb(v);
    isvisited[v] = 1;
    for (auto x : adj[v])
    {
        if (!isvisited[x])
        {
            c += dfs(isvisited, x, cnt);
            //c += cnt[x];
        }
    }
    cnt[v] = c;
    return c;
}

int bisearch(ll val)
{
    int l = 0, r = seq.size() - 1;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (seq[mid] > val)
        {
            r = mid - 1;
        }
        else if (seq[mid] < val)
            l = mid + 1;
        else return mid;
    }
    return -1;
}

int main()
{
    fastio;

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #endif

    int n, q;
    cin >> n >> q;
    for (int i = 2; i <= n; i++)
    {
        int to;
        cin >> to;
        addedge(to, i);
        addedge(i, to);
    }
    for (int i = 1; i <= n; i++)
        sort(adj[i].begin(), adj[i].end());
    bool isvisited[n + 1];
    memset(isvisited, 0, sizeof(bool) * (n + 1));
    int cnt[n + 1];
    memset(cnt, 0, sizeof(int) * (n + 1));
    dfs(isvisited, 1, cnt);
    //for (auto x : seq)
    //    cout << x << " ";
    //cout << endl;
    //cout << bisearch(7);
    int ind[n + 1];
    for (int i = 0; i < n; i++)
    {
        ind[seq[i]] = i;
    }
    for (int i = 0; i < q; i++)
    {
        int u, k;
        cin >> u >> k;
        int idx = bisearch(u);
        //cout << "ind " << idx << endl;
        assert(idx != -1);
        if (k > cnt[u])
            cout << -1 << endl;
        else
        {
            cout << seq[idx + k - 1] << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

using namespace std;

vector <ll> adj[int(2 * pow(10, 5) + 1)];

vector <int> seq;

void addedge(ll from, ll to)
{
    adj[from].pb(to);
}

int dfs(bool isvisited[], ll v, int cnt[])
{
    int c = 1;
    seq.pb(v);
    isvisited[v] = 1;
    for (auto x : adj[v])
    {
        if (!isvisited[x])
        {
            c += dfs(isvisited, x, cnt);
        }
    }
    cnt[v] = c;
    return c;
}

int bisearch(ll val)
{
    int l = 0, r = seq.size();
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (seq[mid] > val)
        {
            r = mid - 1;
        }
        else if (seq[mid] < val)
            l = mid + 1;
        else return mid;
    }
    return -1;
}

int main()
{
    fastio;

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #endif

    int n, q;
    cin >> n >> q;
    for (int i = 2; i <= n; i++)
    {
        int to;
        cin >> to;
        addedge(to, i);
    }
    for (int i = 1; i <= n; i++)
        sort(adj[i].begin(), adj[i].end());
    bool isvisited[n + 1];
    memset(isvisited, 0, sizeof(bool) * (n + 1));
    int cnt[n + 1];
    memset(cnt, 0, sizeof(int) * (n + 1));
    dfs(isvisited, 1, cnt);
    for (int i = 0; i < q; i++)
    {
        int u, k;
        cin >> u >> k;
        int ind = bisearch(u);
        if (k > cnt[u])
            cout << -1 << endl;
        else
        {
            cout << seq[ind + k - 1] << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

using namespace std;

vector <ll> adj[int(2 * pow(10, 5) + 1)];

vector <int> seq;

void addedge(ll from, ll to)
{
    adj[from].pb(to);
}

int dfs(bool isvisited[], ll v, int cnt[])
{
    int c = 1;
    seq.pb(v);
    isvisited[v] = 1;
    for (auto x : adj[v])
    {
        if (!isvisited[x])
        {
            c += dfs(isvisited, x, cnt);
        }
    }
    cnt[v] = c;
    return c;
}

int bisearch(ll val)
{
    int l = 0, r = seq.size() - 1;
    while (l <= r)
    {

        int mid = (l + r) / 2; //cout << seq[mid] << " ";
        if (seq[mid] > val)
        {
            r = mid - 1;
        }
        else if (seq[mid] < val)
            l = mid + 1;
        else return mid;
    }
    return -1;
}

int main()
{
    fastio;

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #endif

    int n, q;
    cin >> n >> q;
    for (int i = 2; i <= n; i++)
    {
        int to;
        cin >> to;
        addedge(to, i);
        addedge(i, to);
    }
    for (int i = 1; i <= n; i++)
        sort(adj[i].begin(), adj[i].end());
    bool isvisited[n + 1];
    memset(isvisited, 0, sizeof(bool) * (n + 1));
    int cnt[n + 1];
    memset(cnt, 0, sizeof(int) * (n + 1));
    dfs(isvisited, 1, cnt);
    //for (auto x : seq)
    //    cout << x << " ";
    //cout << endl;
    //cout << bisearch(7);
    int ind[n + 1];
    for (int i = 0; i < n; i++)
    {
        ind[seq[i]] = i;
    }
    for (int i = 0; i < q; i++)
    {
        int u, k;
        cin >> u >> k;
        int idx = ind[seq[u]];
        //cout << "ind " << idx << endl;
        assert(idx != -1);
        if (k > cnt[u])
            cout << -1 << endl;
        else
        {
            cout << seq[idx + k - 2] << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

using namespace std;

vector <ll> adj[int(2 * pow(10, 5) + 1)];

vector <int> seq;

void addedge(ll from, ll to)
{
    adj[from].pb(to);
}

int dfs(bool isvisited[], ll v, int cnt[])
{
    int c = 1;
    seq.pb(v);
    isvisited[v] = 1;
    for (auto x : adj[v])
    {
        if (!isvisited[x])
        {
            c += dfs(isvisited, x, cnt);
        }
    }
    cnt[v] = c;
    return c;
}

int bisearch(ll val)
{
    int l = 0, r = seq.size() - 1;
    while (l <= r)
    {

        int mid = (l + r) / 2; //cout << seq[mid] << " ";
        if (seq[mid] > val)
        {
            r = mid - 1;
        }
        else if (seq[mid] < val)
            l = mid + 1;
        else return mid;
    }
    return -1;
}

int main()
{
    fastio;

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #endif

    int n, q;
    cin >> n >> q;
    for (int i = 2; i <= n; i++)
    {
        int to;
        cin >> to;
        addedge(to, i);
    }
    for (int i = 1; i <= n; i++)
        sort(adj[i].begin(), adj[i].end());
    bool isvisited[n + 1];
    memset(isvisited, 0, sizeof(bool) * (n + 1));
    int cnt[n + 1];
    memset(cnt, 0, sizeof(int) * (n + 1));
    dfs(isvisited, 1, cnt);
    //for (auto x : seq)
    //    cout << x << " ";
    //cout << endl;
    //cout << bisearch(7);
    for (int i = 0; i < q; i++)
    {
        int u, k;
        cin >> u >> k;
        int ind = bisearch(u);
        //cout << "ind " << ind << endl;
        assert(ind != -1);
        if (k > cnt[u])
            cout << -1 << endl;
        else
        {
            cout << seq[ind + k - 1] << endl;
        }
    }

    return 0;
}


//
//  895B.cpp
//  OI
//
//  Created by Zhaohua Zheng on 25/4/18.
//  Copyright © 2018 Zhaohua Zheng. All rights reserved.
//

#include <stdio.h>
#include "bits/stdc++.h"
#define LL long long
using namespace std;

const int MAX_N=1000010;
LL ans=0;
int n,x,k;
int arr[MAX_N];
/*强行练二分
 First, we need to understand how to find the number of integers in [l, r] segment which are divisible byx. It is r / x–(l - 1) / x.
 */
//binary search: find the SMALLEST j satisfy tempk=k
LL searchfirst(int i){
    LL l=i,h=n;
    while(l<h){
        LL mid=(l+h)/2;
        LL tempk=(arr[mid]/x - (arr[i]-1)/x);
        if(tempk<k){
            l=mid+1;
        }else{
            h=mid;
        }
    }return l;
//binary search: find the LARGEST j satisfy tempk=k
}LL searchlast(long long i,long long first){
    LL l=first,h=n;//don't need a res here because the smallest can be returned is valid through searchfirst
    while(l<h){
        LL mid=(l+h+1)/2;
        LL tempk=(arr[mid]/x - (arr[i]-1)/x);
        if (tempk==k){
            l=mid;
        }else{
            h=mid-1;
        }
    }return l;
}

int main(){
    cin>>n>>x>>k;
    for(int i=0;i<n;i++)    cin>>arr[i];
    sort(arr,arr+n);
    //add a impossible option in the end so that i can continue while j doesn't exceed the limit
    arr[n]=100000000;

    for(int i=0;i<n;i++){
        LL l=searchfirst(i);
        if (l==n or(k==0 and arr[i]/x - (arr[i]-1)/x))continue;

        LL r=searchlast(i,l);
        long long range=r-l+1;

        int added=0; if (l==i){added=1;}
        if (arr[i]==arr[r] and i<r) range=range*2-added;
        ans+=range;
    }
    cout<<ans<<endl;
    return 0;
}
/*
 #include <bits/stdc++.h>
 using namespace std;
 typedef long long ll;
 const int maxn = 1e5+5;
 ll a[maxn];
 int main() {
 ios::sync_with_stdio(false);
 cin.tie(0);
 cout.tie(0);
 ll n,x,k,cnt;
 cin>>n>>x>>k;
 for(ll i=0;i<n;++i) cin>>a[i];
 sort(a,a+n);
 cnt=0;
 for(ll i=0;i<n;++i) {
 ll l=max(((a[i]-1)/x+k)*x,a[i]);
 ll r=((a[i]-1)/x+k+1)*x;
 cnt+=lower_bound(a,a+n,r)-lower_bound(a,a+n,l);
 }
 cout<<cnt<<endl;
 return 0;
 }
 */


//
//  895B.cpp
//  OI
//
//  Created by Zhaohua Zheng on 25/4/18.
//  Copyright © 2018 Zhaohua Zheng. All rights reserved.
//

#include <stdio.h>
#include "bits/stdc++.h"
using namespace std;

const int MAX_N=1000010;
int ans=0;
int n,x,k;
int arr[MAX_N];
/*强行练二分
 First, we need to understand how to find the number of integers in [l, r] segment which are divisible byx. It is r / x–(l - 1) / x.
 */
//binary search: find the SMALLEST j satisfy tempk=k
int searchfirst(int i){
    int l=i,h=n;
    while(l<h){
        int mid=(l+h)/2;
        int tempk=(arr[mid]/x - (arr[i]-1)/x);
        if(tempk<k){
            l=mid+1;
        }else{
            h=mid;
        }
    }return l;
//binary search: find the LARGEST j satisfy tempk=k
}int searchlast(int i,int first){
    int l=first,h=n;//don't need a res here because the smallest can be returned is valid through searchfirst
    while(l<h){
        int mid=(l+h+1)/2;
        int tempk=(arr[mid]/x - (arr[i]-1)/x);
        if (tempk==k){
            l=mid;
        }else{
            h=mid-1;
        }
    }return l;
}

int main(){
    cin>>n>>x>>k;
    for(int i=0;i<n;i++)    cin>>arr[i];
    sort(arr,arr+n);
    //add a impossible option in the end so that i can continue while j doesn't exceed the limit
    arr[n]=100000000;

    for(int i=0;i<n;i++){
        int l=searchfirst(i);
        if (l==n or(k==0 and arr[i]/x - (arr[i]-1)/x))continue;

        int r=searchlast(i,l);
        int range=r-l+1;

        int added=0; if (l==i){added=1;}
        if (arr[i]==arr[r] and i<r) range=range*2-added;
        ans+=range;
    }
    cout<<ans<<endl;
    return 0;
}
/*
 #include <bits/stdc++.h>
 using namespace std;
 typedef long long ll;
 const int maxn = 1e5+5;
 ll a[maxn];
 int main() {
 ios::sync_with_stdio(false);
 cin.tie(0);
 cout.tie(0);
 ll n,x,k,cnt;
 cin>>n>>x>>k;
 for(ll i=0;i<n;++i) cin>>a[i];
 sort(a,a+n);
 cnt=0;
 for(ll i=0;i<n;++i) {
 ll l=max(((a[i]-1)/x+k)*x,a[i]);
 ll r=((a[i]-1)/x+k+1)*x;
 cnt+=lower_bound(a,a+n,r)-lower_bound(a,a+n,l);
 }
 cout<<cnt<<endl;
 return 0;
 }
 */