//#include "stdafx.h"
#include <cstdio>
int stramos[250001],n,m;
int main()
{
FILE *f=fopen("stramosi.in","r");
FILE *g=fopen("stramosi.out","w");
int i,j,curent,q,p;
int p1,p2,p3,p4,q1,q2,q3,q4,p5,p6,p7,p8,q5,q6,q7,q8;
fscanf(f,"%d %d",&n,&m);
for(i=1; i<=n; i++)
fscanf(f,"%d",&stramos[i]);
while(m>=4) {
fscanf(f,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&q1,&p1,&q2,&p2,&q3,&p3,&q4,&p4,&q5,&p5,&q6,&p6,&q7,&p7,&q8,&p8);
curent=stramos[q1];
for(j=1; j<p1; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
curent=stramos[q2];
for(j=1; j<p2; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
curent=stramos[q3];
for(j=1; j<p3; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
curent=stramos[q4];
for(j=1; j<p4; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
//////////////////////////////////
curent=stramos[q5];
for(j=1; j<p5; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
curent=stramos[q6];
for(j=1; j<p6; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
curent=stramos[q7];
for(j=1; j<p7; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
curent=stramos[q8];
for(j=1; j<p8; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
m-=4;
}
for(i=0; i<m; i++) {
fscanf(f,"%d %d",&q,&p);
curent=stramos[q];
for(j=1; j<p; j++) {
if(!curent) break;
curent=stramos[curent];
}
fprintf(g,"%d\n",curent);
}
fclose(f);
fclose(g);
return 0;
}