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

const int MAX_N = 25e4 + 1;
const int LOG_MAX_N = 17;

int binary_lifting[LOG_MAX_N + 1][MAX_N], N, M;

ifstream fin("stramosi.in");
ofstream fout("stramosi.out");


int main()
{
    fin >> N >> M;

    for(int i = 1; i <= N; ++i)
        fin >> binary_lifting[0][i];

    for(int k = 1; k <= LOG_MAX_N; ++k)
        for(int i = 1; i <= N; ++i)
            binary_lifting[k][i] = binary_lifting[k - 1][binary_lifting[k - 1][i]];

    for(int i = 1; i <= M; ++i)
    {
        int q, p;
        fin >> q >> p;

        for(int j = 0; q != 0 && j <= LOG_MAX_N; ++j)
        {
            if((1 << j) & p){
                q = binary_lifting[j][q];
            }
        }

        fout << q << '\n';
    }

}