#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   constexpr auto INF = 9223372036854775807;   using namespace std;   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   inline long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int mod_power(long long int x, long long int y) {     long long int res = 1;     x = x % 1000000007;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % 1000000007;       y = y >> 1;       x = (x * x) % 1000000007;     }     return res;   }   long long modInverse(long long n) { return mod_power(n, 1000000007 - 2); }   long long nCrModPFermat(long long n, long long int r) {     if (r == 0) return 1;     vector<long long int> fac(n + 1, 0);     fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % 1000000007;     return (fac[n] * modInverse(fac[r]) % 1000000007 * modInverse(fac[n - r]) %             1000000007) %            1000000007;   }   bool isprime(long long int n) {     if (n == 2) return true;     if (n % 2 == 0 || n == 1) {       return false;     }     for (long long int j = 3; j <= sqrt(n); j += 2) {       if (n % j == 0) {         return false;       }     }     return true;   }   vector<vector<long long int>> arr;   vector<bool> visited;   vector<long long int> out_time;   vector<long long int> pre_time_vertex;   vector<long long int> vertex_pre_time;   vector<long long int> in_time;   vector<long long int> depth;   long long int cnt = 1, d = 0, cnt2 = 1;   void dfs(long long int u) {     pre_time_vertex[cnt] = u;     vertex_pre_time[u] = cnt;     in_time[u] = cnt2;     cnt2++;     cnt++;     visited[u] = true;     for (long long int i : arr[u]) {       if (!visited[i]) {         depth[i] = depth[u] + 1;         dfs(i);       }     }     out_time[u] = cnt2;     cnt2++;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long int n, q;     cin >> n >> q;     vector<long long int> parent(n + 1, -1);     for (long long int i = (long long int)2; i < (long long int)n + 1; i += 1) {       cin >> parent[i];     }     arr.resize(n + 1, vector<long long int>());     visited.resize(n + 1, false);     pre_time_vertex.resize(n + 1, 0);     vertex_pre_time.resize(n + 1, 0);     out_time.resize(n + 1, 0);     in_time.resize(n + 1, 0);     depth.resize(n + 1, 0);     for (long long int i = (long long int)2; i < (long long int)n + 1; i += 1) {       arr[parent[i]].push_back(i);       arr[i].push_back(parent[i]);     }     for (long long int i = (long long int)1; i < (long long int)n + 1; i += 1) {       sort(arr[i].begin(), arr[i].end());     }     dfs(1);     for (long long int i = (long long int)0; i < (long long int)q; i += 1) {       long long int u, k;       cin >> u >> k;       long long int req_pre = vertex_pre_time[u] + k - 1;       if (k == 1) {         cout << u <<   n ;       } else if (req_pre <= n && depth[pre_time_vertex[req_pre]] > depth[u]) {         long long int vertex = pre_time_vertex[req_pre];         if (out_time[u] > out_time[vertex])           cout << pre_time_vertex[req_pre] <<   n ;         else           cout << -1 <<   n ;       } else {         cout << -1 <<   n ;       }     }     return 0;   }