#include <bits/stdc++.h>
using namespace std;
//
const int maxn = 1e5 + 5;
//
int n, q, V[maxn], rmq[maxn][30];
//
void build (void)
{
    for (int i = 1; i <= n; ++i)
        rmq[i][0] = V[i];
    for (int j = 1; j < 30; ++j)
        for (int i = 1; i <= n; ++i)
            rmq[i][j] = rmq[rmq[i][j - 1]][j - 1];
}
int solve (int u, int x)
{
    int v = u;
    //
    for (int i = 0; (1 << i) <= x; ++i)
        if (x >> i & 1)
            v = rmq[v][i];
    return v;
}
//
void process (void)
{
    cin >> n >> q;
    for (int i = 1; i <= n; ++i)
        cin >> V[i];
    build();
    for (int u, x; q--; cout << '\n')
        cin >> u >> x,
        cout << solve(u, x);
}
//
signed main (void)
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    process();
}