#include <bits/stdc++.h>
#define MOD 1000000007
#define pb push_back
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    int n;
    cin >> n;
    int* f = new int[n+1];
    int d[201][10001];
    int a[201],b[201];
    for(int i=0;i<n;i++) cin >> f[i+1];
    vector<int> v;
    for(int i=1;i<=n;i++){
        int c=i;
        for(int j=1;j<10001;j++){
            d[i][j] = f[c];
            c = f[c];
        }
    }
    map<int,int> m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<10001;j++){
            if(2*j<10001 && d[i][2*j]==d[i][j]){
                a[i]=j;
                for(int k=j+1;2*k<10001;k++){
                    if(d[i][2*k]==d[i][k]){
                        b[i]=k-j;
                        break;
                    }
                }
                break;
            }
        }
    }

    int k=0,l=1;
    for(int i=1;i<=n;i++){
        int aa = k,bb = a[i];
        while(aa!=bb){
            if(aa<bb){
                if(((bb-aa)/l)!=0) aa+=l*((bb-aa)/l);
                else aa+=l;
            }
            else{
                if(((aa-bb)/b[i])!=0) bb+=b[i]*((aa-bb)/b[i]);
                else bb+=b[i];
            }
        }
        k=aa;
        l = (l*b[i])/__gcd(l,b[i]);
    }
    //for(map<int,int>::iterator it = m.begin();it!=m.end();it++) if(it->second==n){k=it->first;break;}
    cout << k << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define pb push_back
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    int n;
    cin >> n;
    int* f = new int[n+1];
    int d[201][10001];
    for(int i=0;i<n;i++) cin >> f[i+1];
    vector<int> v;
    for(int i=1;i<=n;i++){
        int c=i;
        for(int j=1;j<10001;j++){
            d[i][j] = f[c];
            c = f[c];
        }
    }
    map<int,int> m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<10001;j++){
            if(2*j<10001 && d[i][2*j]==d[i][j]){
                m[j]++;
            }
        }
    }
    int k=0;
    for(map<int,int>::iterator it = m.begin();it!=m.end();it++) if(it->second==n){k=it->first;break;}
    cout << k << "\n";
    return 0;
}


#include<bits/stdc++.h>
//Definitions
#define LL long long
#define LLU unsigned long long
#define fora(var,end) for(int var=0;var<end;var++)
#define fore(var,start,end) for(int var=start;var<end;var++)
#define forit(it1,a) for(typeof(a.begin()) it1=a.begin();it1!=a.end();it1++)
#define pub push_back
#define fst first
#define snd second
#define MOD 1000000007
#define mkp make_pair
#define beg begin
#define ed end
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define P(a,b) cout<<a<<" "<<b<<" "
#define PNL printf("\n")
#define vi vector<int>
#define vpi vector<pair<pair<int,int>,int> >
#define FL(a,n,x) fill(a,a+n,x)
#define db1(a) cout<<#a<<":"<<a<<endl;
#define db2(a,b) cout<<#a<<":"<<a<<" , "<<#b<<" : "<<b<<endl;
#define db3(a,b,c) cout<<#a<<":"<<a<<" , "<<#b<<":"<<b<<" , "<<#c<<":"<<c<<endl;
//AP_HAWKDOWN from hereon

using namespace std;
int a[200000];
int vis[100000];
LL dist[100000];
LL d(int x)
{
    if(dist[x]!=-1)
    {
        return dist[x];
    }
    if(vis[x]==2)
    {
        dist[x]=0;
        return 0;
    }
    else
    {
        dist[x]=d(a[x])+1;
        return dist[x];
    }
}
int main()
{
//freopen("ip.cpp","r",stdin);
    int n;
    cin>>n;
    fora(i,n)
    {
        cin>>a[i+1];
        dist[i+1]=-1;
    }
    LL len=0;
    int x;
    LL lcm=1;
    LL ma=-1;
    for(int i=0; i<n; i++)
    {
        len=0;
        x=i+1;
        if(vis[x]==1)
            continue;
        while(!vis[x])
        {
        //    db3(i+1,"CY",x);
            vis[x]++;
            x=a[x];
        }
        while(vis[x]==1)
        {
          //  db2(i+1,x);
            len++;
            vis[x]++;
            x=a[x];
        }
        if(len>0)
        lcm=(lcm*len)/(__gcd(lcm,len));
    }
    for(int i=1;i<=n;i++)
    {
      //  cout<<vis[i]<<" ";
        if(vis[i]==1)
        {
        //    db1(i);
            ma=max(d(i),ma);
        }
    }
    //ma--;
    //cout<<ma<<endl;
    if(ma>lcm)
    {
        lcm*=(((ma-1)/lcm)+1);
    }
    cout<<lcm;
    return 0;
}


#include<bits/stdc++.h>
//Definitions
#define LL long long
#define LLU unsigned long long
#define fora(var,end) for(int var=0;var<end;var++)
#define fore(var,start,end) for(int var=start;var<end;var++)
#define forit(it1,a) for(typeof(a.begin()) it1=a.begin();it1!=a.end();it1++)
#define pub push_back
#define fst first
#define snd second
#define MOD 1000000007
#define mkp make_pair
#define beg begin
#define ed end
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define P(a,b) cout<<a<<" "<<b<<" "
#define PNL printf("\n")
#define vi vector<int>
#define vpi vector<pair<pair<int,int>,int> >
#define FL(a,n,x) fill(a,a+n,x)
#define db1(a) cout<<#a<<":"<<a<<endl;
#define db2(a,b) cout<<#a<<":"<<a<<" , "<<#b<<" : "<<b<<endl;
#define db3(a,b,c) cout<<#a<<":"<<a<<" , "<<#b<<":"<<b<<" , "<<#c<<":"<<c<<endl;
//AP_HAWKDOWN from hereon
using namespace std;
int a[200000];
int vis[100000];
LL dist[100000];
int isCycle(int x)
{
    if(vis[x]==2)
        return 1;

int v[100000];
   // memset(v,0,sizeof(v));
    int temp=x;

    while(v[temp]!=1)
    {

        v[temp]=1;
        temp=a[temp];
    }
    if(temp==x)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int markCycle(int x)
{
    int len=1;
    //cout<<vis[x]<<endl;
    vis[x]=2;
    v[x]=0;
    x=a[x];

    while(vis[x]!=2)
    {
        //db2("marking",x);
        len++;
        v[x]=0;
        vis[x]=2;
        x=a[x];
    }
    return len;
}
int distFromCycle(int i)
{
    if(dist[i]!=-1)
    {
        return dist[i];
    }
    if(vis[i]==2)
    {
        dist[i]=0;
        return 0;
    }
    dist[i]=distFromCycle(a[i])+1;
    return dist[i];

}
void mark(int i)
{
    while(vis[i]==0)
    {
        v[i]=0;
        vis[i]=1;
        i=a[i];
    }
}
int main()
{
//freopen("ip.cpp","r",stdin);
    int n;
  //  cin>>n;
    n=100000;
    fora(i,n)
    {
        a[i+1]=rand()%100000;
     //   cin>>a[i+1];
        dist[i+1]=-1;
    }
    LL len=0;
    int x;
    LL lcm=1;
    LL ma=-1;

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

       if(isCycle(i+1))
       {

          x= markCycle(i+1);
        //  db3("yc",i+1,x);
          if(x!=0)
          {
              lcm=(lcm*x)/__gcd(lcm,(LL)x);
          }
       }
       else
       {
           mark(i+1);
       }

    }

    for(int i=1;i<=n;i++)
    {
      //  cout<<vis[i]<<" ";
        if(vis[i]==1)
        {

            ma=max((LL)distFromCycle(i),ma);
        }
    }
    //ma--;
    //cout<<ma<<endl;
    if(ma>lcm)
    {
        lcm*=(((ma-1)/lcm)+1);
    }
    cout<<lcm;
    return 0;
}


#include<bits/stdc++.h>
//Definitions
#define LL long long
#define LLU unsigned long long
#define fora(var,end) for(int var=0;var<end;var++)
#define fore(var,start,end) for(int var=start;var<end;var++)
#define forit(it1,a) for(typeof(a.begin()) it1=a.begin();it1!=a.end();it1++)
#define pub push_back
#define fst first
#define snd second
#define MOD 1000000007
#define mkp make_pair
#define beg begin
#define ed end
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define P(a,b) cout<<a<<" "<<b<<" "
#define PNL printf("\n")
#define vi vector<int>
#define vpi vector<pair<pair<int,int>,int> >
#define FL(a,n,x) fill(a,a+n,x)
#define db1(a) cout<<#a<<":"<<a<<endl;
#define db2(a,b) cout<<#a<<":"<<a<<" , "<<#b<<" : "<<b<<endl;
#define db3(a,b,c) cout<<#a<<":"<<a<<" , "<<#b<<":"<<b<<" , "<<#c<<":"<<c<<endl;
//AP_HAWKDOWN from hereon
using namespace std;
int a[200000];
int vis[100000];
LL dist[100000];
int isCycle(int x)
{
    if(vis[x]==2)
        return 1;

int v[100000];
    memset(v,0,sizeof(v));
    int temp=x;

    while(v[temp]!=1)
    {

        v[temp]=1;
     //   db2("CY",temp);
        temp=a[temp];
    }
    if(temp==x)
    {
       // db1("HERE");

        return 1;
    }
    else
    {
       // db1("THERE");
        return 0;
    }
}
int markCycle(int x)
{
    int len=1;
    //cout<<vis[x]<<endl;
    vis[x]=2;
    x=a[x];
    while(vis[x]!=2)
    {
        //db2("marking",x);
        len++;
        vis[x]=2;
        x=a[x];
    }
    return len;
}
int distFromCycle(int i)
{
    if(dist[i]!=-1)
    {
        return dist[i];
    }
    if(vis[i]==2)
    {
        dist[i]=0;
        return 0;
    }
    dist[i]=distFromCycle(a[i])+1;
    return dist[i];

}
void mark(int i)
{
    while(vis[i]==0)
    {
        vis[i]=1;
        i=a[i];
    }
}
int main()
{
//freopen("ip.cpp","r",stdin);
    int n;
    cin>>n;
    fora(i,n)
    {
        cin>>a[i+1];
        dist[i+1]=-1;
    }
    LL len=0;
    int x;
    LL lcm=1;
    LL ma=-1;

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

       if(isCycle(i+1))
       {

          x= markCycle(i+1);
        //  db3("yc",i+1,x);
          if(x!=0)
          {
              lcm=(lcm*x)/__gcd(lcm,(LL)x);
          }
       }
       else
       {
           mark(i+1);
       }

    }

    for(int i=1;i<=n;i++)
    {
      //  cout<<vis[i]<<" ";
        if(vis[i]==1)
        {

            ma=max((LL)distFromCycle(i),ma);
        }
    }
    //ma--;
    //cout<<ma<<endl;
    if(ma>lcm)
    {
        lcm*=(((ma-1)/lcm)+1);
    }
    cout<<lcm;
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#define ll long long
#define M 222
#include<stack>
#include<iostream>
using namespace std;
ll ans = 0;
int low[M] , num[M] , label[M] , sz[M];
int f[M] , g[M] , nsvertice = 0;
stack<int> st;
int cnt = 0;
int maxLen = 0;
void minimize(int &a,int b){
    if(a > b) a = b;
}
void maximize(int &a,int b){
    if(a < b) a = b;
}
void dfs(int u){
    st.push(u);
    cnt++;
    low[u] = cnt ; num[u] = cnt;
    if(label[f[u]] == 0){
        if(num[f[u]] == 0){
            dfs(f[u]);
            minimize(low[u] , low[f[u]]);
        }
        else minimize(low[u],num[f[u]]);
    }
    if(low[u] == num[u]){
        int v;
        nsvertice++;
        do{
            v = st.top();
            st.pop();
            sz[nsvertice]++;
            label[v] = u;
        }while(v != u);
    }
}
ll gcd(ll a,ll b){
    if(a < b) swap(a,b);
    while(a%b != 0){
        ll r = a%b;
        a = b;
        b = r;
    }
    return b;
}
ll lcm(ll a,ll b){
    ll x = gcd(a,b);
    a/=x;
    return a*b;
}
ll mul = 1;
main(){
//  freopen("test.inp","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i = 1 ; i <= n ; i++)   scanf("%d",&f[i]);
    for(int i = 1 ; i <= n ; i++)
        if(num[i] == 0) dfs(i);
    for(int i = 1 ; i <= nsvertice ; i++) mul = lcm(mul , sz[i]);
    for(int i = 1 ; i <= n ; i++)
        if(label[i] != label[f[i]]) g[label[i]] = label[f[i]];
    for(int i = 1 ; i <= nsvertice ; i++){
        int tmp = 1;
        int x = i;
        while(g[x] != 0){
            tmp++;
            x = g[x];
        }
        maximize(maxLen , tmp);
    }
    while(ans  < maxLen)    ans+=mul;
    cout<<ans;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#define ll long long
#define M 222
#include<stack>
#include<iostream>
using namespace std;
ll ans = 0;
int low[M] , num[M] , label[M] , sz[M];
int f[M] , g[M] , nsvertice = 0;
stack<int> st;
int cnt = 0;
int maxLen = 1;
void minimize(int &a,int b){
    if(a > b) a = b;
}
void maximize(int &a,int b){
    if(a < b) a = b;
}
void dfs(int u){
    st.push(u);
    cnt++;
    low[u] = cnt ; num[u] = cnt;
    if(label[f[u]] == 0){
        if(num[f[u]] == 0){
            dfs(f[u]);
            minimize(low[u] , low[f[u]]);
        }
        else minimize(low[u],num[f[u]]);
    }
    if(low[u] == num[u]){
        int v;
        nsvertice++;
        do{
            v = st.top();
            st.pop();
            sz[nsvertice]++;
            label[v] = u;
        }while(v != u);
    }
}
ll gcd(ll a,ll b){
    if(a < b) swap(a,b);
    while(a%b != 0){
        ll r = a%b;
        a = b;
        b = r;
    }
    return b;
}
ll lcm(ll a,ll b){
    ll x = gcd(a,b);
    a/=x;
    return a*b;
}
ll mul = 1;
main(){
//  freopen("test.inp","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i = 1 ; i <= n ; i++)   scanf("%d",&f[i]);
    for(int i = 1 ; i <= n ; i++)
        if(num[i] == 0) dfs(i);
    for(int i = 1 ; i <= nsvertice ; i++) mul = lcm(mul , sz[i]);
    for(int i = 1 ; i <= n ; i++)
        if(label[i] != label[f[i]]) g[label[i]] = label[f[i]];
    for(int i = 1 ; i <= n ; i++){
        if(g[i] == 0) continue;
        int tmp = 0;
        int x = i;
        while(g[x] != 0){
            tmp++;
            x = g[x];
        }
        maximize(maxLen , tmp);
    }
    while(ans  < maxLen)    ans+=mul;
    cout<<ans;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#define ll long long
#define M 222
#include<stack>
#include<iostream>
using namespace std;
ll ans = 0;
int low[M] , num[M] , label[M] , sz[M];
int f[M] , g[M] , nsvertice = 0;
stack<int> st;
int cnt = 0;
int maxLen = 0;
void minimize(int &a,int b){
    if(a > b) a = b;
}
void maximize(int &a,int b){
    if(a < b) a = b;
}
void dfs(int u){
    st.push(u);
    cnt++;
    low[u] = cnt ; num[u] = cnt;
    if(label[f[u]] == 0){
        if(num[f[u]] == 0){
            dfs(f[u]);
            minimize(low[u] , low[f[u]]);
        }
        else minimize(low[u],num[f[u]]);
    }
    if(low[u] == num[u]){
        int v;
        nsvertice++;
        do{
            v = st.top();
            st.pop();
            sz[nsvertice]++;
            label[v] = u;
        }while(v != u);
    }
}
ll gcd(ll a,ll b){
    if(a < b) swap(a,b);
    while(a%b != 0){
        ll r = a%b;
        a = b;
        b = r;
    }
    return b;
}
ll lcm(ll a,ll b){
    ll x = gcd(a,b);
    a/=x;
    return a*b;
}
ll mul = 1;
main(){
//  freopen("test.inp","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i = 1 ; i <= n ; i++)   scanf("%d",&f[i]);
    for(int i = 1 ; i <= n ; i++)
        if(num[i] == 0) dfs(i);
    for(int i = 1 ; i <= nsvertice ; i++) mul = lcm(mul , sz[i]);
    for(int i = 1 ; i <= n ; i++)
        if(label[i] != label[f[i]]) g[label[i]] = label[f[i]];
    for(int i = 1 ; i <= nsvertice ; i++){
        int tmp = 1;
        int x = i;
        while(g[x] != 0){
            tmp++;
            x = g[x];
        }
        maximize(maxLen , tmp - 1);
    }
    while(ans  < maxLen + 1)    ans+=mul;
    cout<<ans;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#define ll long long
#define M 222
#include<stack>
#include<iostream>
using namespace std;
ll ans = 0;
int low[M] , num[M] , label[M] , sz[M];
int f[M] , g[M] , nsvertice = 0;
stack<int> st;
int cnt = 0;
int maxLen = 0;
void minimize(int &a,int b){
	if(a > b) a = b;
}
void maximize(int &a,int b){
	if(a < b) a = b;
}
void dfs(int u){
	st.push(u);
	cnt++;
	low[u] = cnt ; num[u] = cnt;
	if(label[f[u]] == 0){
		if(num[f[u]] == 0){
			dfs(f[u]);
			minimize(low[u] , low[f[u]]);
		}
		else minimize(low[u],num[f[u]]);
	}
	if(low[u] == num[u]){
		int v;
		nsvertice++;
		do{
			v = st.top();
			st.pop();
			sz[nsvertice]++;
			label[v] = u;
		}while(v != u);
	}
}
ll gcd(ll a,ll b){
	if(a < b) swap(a,b);
	while(a%b != 0){
		ll r = a%b;
		a = b;
		b = r;
	}
	return b;
}
ll lcm(ll a,ll b){
	ll x = gcd(a,b);
	a/=x;
	return a*b;
}
ll mul = 1;
main(){
	//freopen("test.inp","r",stdin);
	int n;
	scanf("%d",&n);
	for(int i = 1 ; i <= n ; i++)	scanf("%d",&f[i]);
	for(int i = 1 ; i <= n ; i++)
		if(num[i] == 0) dfs(i);
	for(int i = 1 ; i <= nsvertice ; i++) mul = lcm(mul , sz[i]);
	for(int i = 1 ; i <= n ; i++)
		if(label[i] != label[f[i]]) g[label[i]] = label[f[i]];
	for(int i = 1 ; i <= nsvertice ; i++){
		int tmp = 1;
		int x = i;
		while(g[x] != 0){
			tmp++;
			x = g[x];
		}
		maximize(maxLen , tmp - 2);
	}
	while(ans  < maxLen + 1) 	ans+=mul;
	cout<<ans;
}


#include <iostream>
#include <vector>

using namespace std;
int n,k,i,j;
bool s;
vector <int> f[10];
void xuly ()
{
    s=true;
    for (j=1;j<=n;j++)
    if (f[1][f[1][j]]!=f[1][j]) {s=false;break;}
    if (s) {k=1; return;}
    i=2;
    while (k!=i)
    {
        for (j=1;j<=n;j++) f[2][j]=f[1][f[2][j]];
        s=true;
        for (j=1;j<=n;j++)
        if (f[2][f[2][j]]!=f[2][j]) {s=false;break;}
        if (s) {k=i; return;}
        i+=1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    f[1].push_back(0);
    f[2].push_back(0);
    int u;
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f[1].push_back(u);
        f[2].push_back(u);
    }
    xuly();
    cout<<k;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int n,k,i,j;
vector <int> f[202];
void xuly ()
{
    for (j=1;j<=n;j++)
    {
            if (f[1][f[1][j]]!=f[1][j]) break;
            k=1; return;
    }
    for (i=2;i<=n;i++)
    {
        f[i].push_back(0);
        for (j=1;j<=n;j++)
        {
            f[i].push_back(f[1][f[i-1][j]]);
        }
        for (j=1;j<=n;j++)
        {
            if (f[i][f[i][j]]!=f[i][j]) break;
            k=i; return;
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    f[1].push_back(0);
    for (i=1;i<=n;i++)
    {
        int u;
        cin>>u;
        f[1].push_back(u);
    }

    xuly();
    cout<<k;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int n,k,i,j;
bool s;
vector <int> f[202];
void xuly ()
{
    s=true;
    for (j=1;j<=n;j++)
    if (f[1][f[1][j]]!=f[1][j]) {s=false;break;}
    if (s) {k=1; return;}
    i=2;
    while (k!=i)
    {
        f[i].push_back(0);
        for (j=1;j<=n;j++) f[i].push_back(f[1][f[i-1][j]]);
        s=true;
        for (j=1;j<=n;j++)
        if (f[i][f[i][j]]!=f[i][j]) {s=false;break;}
        if (s) {k=i; return;}
        i+=1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    f[1].push_back(0);
    for (i=1;i<=n;i++)
    {
        int u;
        cin>>u;
        f[1].push_back(u);
    }
    xuly();
    cout<<k;
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#include <numeric>

using namespace std;
int n,k,i,j,c,cs,so,maxf;
stack <int> st;
vector <int> f;
int number[202],low[202],cycle[202];
bool fre[202],stuck[202];

int gcd(int a, int b)
{
    for (;;)
    {
        if (a == 0) return b;
        b %= a;
        if (b == 0) return a;
        a %= b;
    }
}
int lcm(int a, int b)
{
    int temp = gcd(a, b);

    return temp ? (a / temp * b) : 0;
}
void visit (int u)
{
    int a;
    c+=1;number[u]=c;
    low[u]=number[u];
    st.push(u);
    if (not fre[f[u]])
    {
        if (number[f[u]]!=0)
            low[u]=min(low[u],number[f[u]]);
        else
        {
            if (u!=f[u])
            {
                visit(f[u]);
                low[u]=min(low[u],low[f[u]]);
            }
            else number[u]==low[u];
        }
    }
    if (number[u]==low[u])
    {
        int cy=0;
        do
        {
            a=st.top();
            st.pop();
            fre[a]=true;
            cy+=1;
        } while (a!=u);
        if (cy==1 and u!=f[u]) stuck[u]=true;
        if (cy>1)
        {
            cs+=1;
            cycle[cs]=cy;
        }
    }
    return;
}
void dfs (int u)
{
    so+=1;
    if (not stuck[u]) return;
    else dfs(f[u]);
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    int u;
    f.push_back(0);
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f.push_back(u);
    }
    cs=-1;
    for (i=1;i<=n;i++)
    if (number[i]==0) visit(i);
    int tmp=accumulate(cycle, cycle + cs+1,1, lcm);
    k=tmp;
    for (i=1;i<=n;i++)
    if (stuck[i])
    {
        so=-1;
        dfs(i);
        if (so>maxf) maxf=so;
    }
    cout<<max(k,(maxf-1)/k*k+k);
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int n,k,i,j;
bool s;
vector <int> f[202];
void xuly ()
{
    s=true;
    for (j=1;j<=n;j++)
    if (f[1][f[1][j]]!=f[1][j]) {s=false;break;}
    if (s) {k=1; return;}
    i=2;
    while (k!=i)
    {
        f[i].push_back(0);
        for (j=1;j<=n;j++) f[i].push_back(f[1][f[i-1][j]]);
        s=true;
        for (j=1;j<=n;j++)
        if (f[i][f[i][j]]!=f[i][j]) {s=false;break;}
        if (s) {k=i; return;}
        i+=1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    f[1].push_back(0);
    int u;
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f[1].push_back(u);
    }
    xuly();
    cout<<k;
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#include <numeric>

using namespace std;
int n,k,i,j,c,cs,maxf;
vector <int> f;
int l[202];
bool fre[202];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    int u;
    f.push_back(0);
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f.push_back(u);
    }
    for (i=1;i<=n;i++)
    {
        memset(fre,0,sizeof fre);
        cs=i;c=0;
        while (!fre[cs])
        {
            l[cs]=c++;
            fre[cs]=true;
            cs=f[cs];
        }
        if (k==0) k=c-l[cs];
        else k=k/__gcd(k,c-l[cs])*(c-l[cs]);
        maxf=max(maxf,l[cs]);
    }
    k=max(k,(maxf-1)/k*k+k);
    cout<<k;
}


#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#include <numeric>

using namespace std;
int n,k,i,j,c,cs,so,maxf;
stack <int> st;
vector <int> f;
int number[202],low[202],cycle[202];
bool fre[202],stuck[202];

int gcd(int a, int b)
{
    for (;;)
    {
        if (a == 0) return b;
        b %= a;
        if (b == 0) return a;
        a %= b;
    }
}
int lcm(int a, int b)
{
    int temp = gcd(a, b);

    return temp ? (a / temp * b) : 0;
}
void visit (int u)
{
    int a;
    c+=1;number[u]=c;
    low[u]=number[u];
    st.push(u);
    if (not fre[f[u]])
    {
        if (number[f[u]]!=0)
            low[u]=min(low[u],number[f[u]]);
        else
        {
            if (u!=f[u])
            {
                visit(f[u]);
                low[u]=min(low[u],low[f[u]]);
            }
            else number[u]==low[u];
        }
    }
    if (number[u]==low[u])
    {
        int cy=0;
        do
        {
            a=st.top();
            st.pop();
            fre[a]=true;
            cy+=1;
        } while (a!=u);
        if (cy==1 and u!=f[u]) stuck[u]=true;
        if (cy>1)
        {
            cs+=1;
            cycle[cs]=cy;
        }
    }
    return;
}
void dfs (int u)
{
    so+=1;
    if (not stuck[u]) return;
    else dfs(f[u]);
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    int u;
    f.push_back(0);
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f.push_back(u);
    }
    cs=-1;
    for (i=1;i<=n;i++)
    if (number[i]==0) visit(i);
    int tmp=accumulate(cycle, cycle + cs+1,1, lcm)*1ll;
    k=tmp*1ll;
    for (i=1;i<=n;i++)
    if (stuck[i])
    {
        so=-1;
        dfs(i);
        if (so>maxf) maxf=so;
    }
    cout<<max(k,(maxf-1)/k*k+k);
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int n,k,i,j;
bool s;
vector <int> f[1000000];
void xuly ()
{
    s=true;
    for (j=1;j<=n;j++)
    if (f[1][f[1][j]]!=f[1][j]) {s=false;break;}
    if (s) {k=1; return;}
    i=2;
    while (k!=i)
    {
        f[i].push_back(0);
        for (j=1;j<=n;j++) f[i].push_back(f[1][f[i-1][j]]);
        s=true;
        for (j=1;j<=n;j++)
        if (f[i][f[i][j]]!=f[i][j]) {s=false;break;}
        if (s) {k=i; return;}
        i+=1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    f[1].push_back(0);
    int u;
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f[1].push_back(u);
    }
    xuly();
    cout<<k;
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#include <numeric>

using namespace std;
int n,i,j,c,cs,maxf;
vector <int> f;
int l[202];
long long k;
bool fre[202];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    int u;
    f.push_back(0);
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f.push_back(u);
    }
    for (i=1;i<=n;i++)
    {
        memset(fre,0,sizeof fre);
        cs=i;c=0;
        while (!fre[cs])
        {
            l[cs]=c++;
            fre[cs]=true;
            cs=f[cs];
        }
        if (k==0) k=c-l[cs]*1ll;
        else k=k/__gcd(k,c-l[cs]*1ll)*(c-l[cs]);
        maxf=max(maxf,l[cs]);
    }
    k=max(k,(maxf-1)/k*k+k);
    cout<<k;
}


#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#include <numeric>

using namespace std;
int n,k,i,j,c,cs,maxf;
vector <int> f;
int l[202];
bool fre[202];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    int u;
    f.push_back(0);
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f.push_back(u);
    }
    for (i=1;i<=n;i++)
    {
        memset(fre,0,sizeof fre);
        cs=i;c=0;
        while (!fre[cs])
        {
            l[cs]=c++;
            fre[cs]=true;
            cs=f[cs];
        }
        if (k==0) k=c-l[cs]*1ll;
        else k=k/__gcd(k,c-l[cs]*1ll)*(c-l[cs]);
        maxf=max(maxf,l[cs]);
    }
    k=max(k,(maxf-1)/k*k+k);
    cout<<k;
}


#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#include <numeric>

using namespace std;
int n,i,j,c,cs,so;
long long k;
stack <int> st;
vector <int> f;
int number[202],low[202],cycle[202];
bool fre[202],stuck[202];

int gcd(int a, int b)
{
    for (;;)
    {
        if (a == 0) return b;
        b %= a;
        if (b == 0) return a;
        a %= b;
    }
}
int lcm(int a, int b)
{
    int temp = gcd(a, b);

    return temp ? (a / temp * b) : 0;
}
void visit (int u)
{
    int a;
    c+=1;number[u]=c;
    low[u]=number[u];
    st.push(u);
    if (not fre[f[u]])
    {
        if (number[f[u]]!=0)
            low[u]=min(low[u],number[f[u]]);
        else
        {
            if (u!=f[u])
            {
                visit(f[u]);
                low[u]=min(low[u],low[f[u]]);
            }
            else number[u]==low[u];
        }
    }
    if (number[u]==low[u])
    {
        int cy=0;
        do
        {
            a=st.top();
            st.pop();
            fre[a]=true;
            cy+=1;
        } while (a!=u);
        if (cy==1 and u!=f[u]) stuck[u]=true;
        if (cy>1)
        {
            cs+=1;
            cycle[cs]=cy;
        }
    }
    return;
}
void dfs (int u)
{
    so+=1;
    if (not stuck[u]) return;
    else dfs(f[u]);
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    int u;
    f.push_back(0);
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f.push_back(u);
    }
    cs=-1;
    for (i=1;i<=n;i++)
    if (number[i]==0) visit(i);
    int tmp=accumulate(cycle, cycle + cs+1,1, lcm);
    k=tmp;
    for (i=1;i<=n;i++)
    if (stuck[i])
    {
        so=-1;
        dfs(i);
        while (so>k) k+=tmp;
    }
    cout<<k;
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#include <numeric>

using namespace std;
int n,k,i,j,c,cs,so;
stack <int> st;
vector <int> f;
int number[202],low[202],cycle[202];
bool fre[202],stuck[202];

int gcd(int a, int b)
{
    for (;;)
    {
        if (a == 0) return b;
        b %= a;
        if (b == 0) return a;
        a %= b;
    }
}
int lcm(int a, int b)
{
    int temp = gcd(a, b);

    return temp ? (a / temp * b) : 0;
}
void visit (int u)
{
    int a;
    c+=1;number[u]=c;
    low[u]=number[u];
    st.push(u);
    if (not fre[f[u]])
    {
        if (number[f[u]]!=0)
            low[u]=min(low[u],number[f[u]]);
        else
        {
            if (u!=f[u])
            {
                visit(f[u]);
                low[u]=min(low[u],low[f[u]]);
            }
            else number[u]==low[u];
        }
    }
    if (number[u]==low[u])
    {
        int cy=0;
        do
        {
            a=st.top();
            st.pop();
            fre[a]=true;
            cy+=1;
        } while (a!=u);
        if (cy==1 and u!=f[u]) stuck[u]=true;
        if (cy>1)
        {
            cs+=1;
            cycle[cs]=cy;
        }
    }
    return;
}
void dfs (int u)
{
    so+=1;
    if (not stuck[u]) return;
    else dfs(f[u]);
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    int u;
    f.push_back(0);
    for (i=1;i<=n;i++)
    {
        cin>>u;
        f.push_back(u);
    }
    cs=-1;
    for (i=1;i<=n;i++)
    if (number[i]==0) visit(i);
    int tmp=accumulate(cycle, cycle + cs+1,1, lcm);
    k=tmp;
    for (i=1;i<=n;i++)
    if (stuck[i])
    {
        so=-1;
        dfs(i);
        while (so>k) k+=tmp;
    }
    cout<<k;
    return 0;
}


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

#define rep(i,a,b) for (int i=a;i<=b;++i)
#define dep(i,a,b) for (int i=a;i>=b;--i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define clr(x) memset(x,0,sizeof(x))
typedef long long ll;
ll power(ll x,ll y,ll mod){if (y<0)return power(power(x,mod-2,mod),-y,mod);ll res=1;for(;y;y>>=1,x=x*x%mod)if (y&1) res=res*x%mod;return res;}

int n;
int a[1000],p[1000],q[1000],v[1000];

int main() {
//	freopen("1.in", "r", stdin);
	scanf("%d",&n);
	rep(i,1,n) scanf("%d",&a[i]);
	rep(i,1,n) {
		rep(j,1,n) v[j]=0;
		int ti=0;
		int x=i;
		while (1) {
			x=a[x];
			if (!v[x]) v[x]=++ti;
			else {
				p[i]=v[x];
				q[i]=ti-v[x]+1;
			//	cout << p[i] << ' ' << q[i] << endl;
				break;
			}
		}
	}
	ll res=1;
	rep(i,1,n) res=res*q[i]/__gcd(res,(ll)q[i]);
	rep(i,1,n) res=max(res,(ll)p[i]);
	cout<<res<<endl;
}


#include<bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    vector<int> A;
    A.resize(n);

    for(int i = 0 ; i < n ; i++) {
        cin >> A[i];
    }
    for(int i = 0, k, l ; i < m ; i++) {
        cin >> k >> l;
        int cur = k;
        int dir = 1;
        int cnt = 0;
        int step = 0;
        while(true) {
            step++;
            if(dir) {
                int next = upper_bound(A.begin(), A.end(), A[cur] + l) - A.begin();
                next--;
                if(next == cur) {
                    cnt++;
                    dir ^= 1;
                    continue;
                } else {
                    cnt = 0;
                    int len = A[next] - A[cur];
                    if(step > 1) {
                        int c = (l/ len) % 2;
                        dir ^= c;
                        l %= len;
                        if(c) cur = next;

                    } else {
                        dir ^= 1;
                        l -= len;
                        cur = next;
                        continue;
                    }
                }
            } else {
                int next = lower_bound(A.begin(), A.end(), A[cur] - l) - A.begin();
                if(next == cur) {
                    cnt++;
                    dir ^= 1;
                    continue;
                } else {
                    cnt = 0;
                    int len = A[cur] - A[next];
                    if(step > 1) {
                        int c = (l/ len) % 2;
                        dir ^= c;
                        l %= len;
                        if(c) cur = next;

                    } else {
                        dir ^= 1;
                        l -= len;
                        cur = next;
                        continue;
                    }
                }
            }
        }
        cout << cur << '\n';
    }
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <climits>

using namespace std;

#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x) , end(x)
#define sz(x) (int)(x).size()
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;

ll modpow(ll b, ll e ,ll mod){
	ll ans = 1;
	for (; e; b = b * b % mod, e /= 2)
		if (e & 1) ans = ans * b % mod;
	return ans;
}
vi a , p;
int n , m;
map<int,int> ord;
map<int,int> rord;
int32_t main(){
	cin >> n >> m;
	a = vi(n);
	rep(i,0,n) cin >> a[i];
	p = a;
	sort(all(a));
	rep(i,0,n) ord[a[i]] = i ,rord[p[i]] = i+1;
	while(m--){
		int x , l;
		cin >> x >> l;
		x--;
		int pos = ord[p[x]];
		int z = prev(lower_bound(all(a) , a[pos] + l + 1)) - begin(a);
		l -= a[z] - a[pos];
		pos = z;
		bool ok = 1 ;
		while(l){
			int npos = pos;
			if(ok == 1){ // left of pos
				npos = (lower_bound(all(a) , a[pos] - l))  - begin(a);
			}
			else{
				npos = prev(lower_bound(all(a) , a[pos] + l + 1)) - begin(a);
			}
			if(npos == pos) break ;
			int len = abs(a[npos] - a[pos]);
			if(((l / len) &1)){
				pos = npos;
				ok ^= 1;
			}
			l %= len;
		}
		cout << rord[a[pos]] << endl;
	}
}

/* clever stuff:
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and STAY ORGANIZED
	* Keep it simple stupid
	* WRITE STUFF DOWN
	* math -> gcd / lcm / divisors?
*/


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<set>
using namespace std ;

#define MAXN 200007

int n , q ;
pair < int , int > a[ MAXN ] ;
int pos[ MAXN ] ;

int getl ( int sr ) {
    int l , r , mid ;
    l = 1 , r = n ;
    while ( r - l > 1 ) {
        mid = ( l + r ) / 2 ;
        if ( a[ mid ].first > sr ) { r = mid ; }
        else { l = mid ; }
    }
    while ( r > 1 && a[ r ].first > sr ) { --r ; }
    return r ;
}

int getr ( int sr ) {
    int l , r , mid ;
    l = 1 , r = n ;
    while ( r - l > 1 ) {
        mid = ( l + r ) / 2 ;
        if ( a[ mid ].first < sr ) { l = mid ; }
        else { r = mid ; }
    }
    while ( l < n && a[ l ].first < sr ) { ++ l ; }
    return l ;
}

void input ( ) {
    scanf ( "%d%d" , &n , &q ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        scanf ( "%d" , &a[ i ].first ) ;
        a[ i ].second = i ;
    }
    sort ( a + 1 , a + n + 1 ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        pos[ a[ i ].second ] = i ;
    }
}

void solve ( ) {
    while ( q -- ) {
        int st , len ;
        scanf ( "%d%d" , &st , &len ) ;
        st = pos[ st ] ;
        int dir = 1 ;
        bool fst = true ;
        while ( 1 ) {
            if ( dir == 1 ) {
                int ret = getl ( a[ st ].first + len ) ;
                if ( ret == st ) { printf ( "%d\n" , a[ st ].second ) ; break ; }
                int diff = ( a[ ret ].first - a[ st ].first ) ;
                if ( fst == false ) {
                    len %= ( diff << 1 ) ;
                }
                if ( len >= diff ) {
                    len -= diff ;
                    dir = -1 ;
                    st = ret ;
                }
            }
            else {
                int ret = getr ( a[ st ].first - len ) ;
                if ( ret == st ) { printf ( "%d\n" , a[ st ].second ) ; break ; }
                int diff = ( a[ st ].first - a[ ret ].first ) ;
                if ( fst == false ) {
                    len %= ( diff << 1 ) ;
                }
                if ( len >= diff ) {
                    len -= diff ;
                    dir = 1 ;
                    st = ret ;
                }
            }
            fst = false ;
        }
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    input ( ) ;
    solve ( ) ;
    return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<set>
using namespace std ;

#define MAXN 200007

int n , q ;
pair < int , int > a[ MAXN ] ;
int pos[ MAXN ] ;

int getl ( int sr ) {
    int l , r , mid ;
    l = 1 , r = n ;
    while ( r - l > 1 ) {
        mid = ( l + r ) / 2 ;
        if ( a[ mid ].first > sr ) { r = mid ; }
        else { l = mid ; }
    }
    while ( r > 1 && a[ r ].first > sr ) { --r ; }
    return r ;
}

int getr ( int sr ) {
    int l , r , mid ;
    l = 1 , r = n ;
    while ( r - l > 1 ) {
        mid = ( l + r ) / 2 ;
        if ( a[ mid ].first < sr ) { l = mid ; }
        else { r = mid ; }
    }
    while ( l < n && a[ l ].first < sr ) { ++ l ; }
    return l ;
}

void input ( ) {
    scanf ( "%d%d" , &n , &q ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        scanf ( "%d" , &a[ i ].first ) ;
        a[ i ].second = i ;
    }
    sort ( a + 1 , a + n + 1 ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        pos[ a[ i ].second ] = i ;
    }
}

void solve ( ) {
    while ( q -- ) {
        int st , len ;
        scanf ( "%d%d" , &st , &len ) ;
        st = pos[ st ] ;
        int dir = 1 ;
        bool fst = true ;
        int cons = 0 ;
        while ( 1 ) {
            if ( len == 0 || cons >= 2 ) { printf ( "%d\n" , a[ st ].second ) ; break ; }
            if ( dir == 1 ) {
                int ret = getl ( a[ st ].first + len ) ;
                if ( ret == st ) { dir = -1 ; ++ cons ; continue ; }
                cons = 0 ;
                int diff = ( a[ ret ].first - a[ st ].first ) ;
                if ( fst == false ) {
                    len %= ( diff << 1 ) ;
                }
                if ( len >= diff ) {
                    len -= diff ;
                    dir = -1 ;
                    st = ret ;
                }
            }
            else {
                int ret = getr ( a[ st ].first - len ) ;
                if ( ret == st ) { dir = 1 ; ++ cons ; continue ; }
                cons = 0 ;
                int diff = ( a[ st ].first - a[ ret ].first ) ;
                if ( fst == false ) {
                    len %= ( diff << 1 ) ;
                }
                if ( len >= diff ) {
                    len -= diff ;
                    dir = 1 ;
                    st = ret ;
                }
            }
            fst = false ;
        }
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    input ( ) ;
    solve ( ) ;
    return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<set>
using namespace std ;

#define MAXN 200007

int n , q ;
pair < int , int > a[ MAXN ] ;
int pos[ MAXN ] ;

int getl ( int sr ) {
    int l , r , mid ;
    l = 1 , r = n ;
    while ( r - l > 1 ) {
        mid = ( l + r ) / 2 ;
        if ( a[ mid ].first > sr ) { r = mid ; }
        else { l = mid ; }
    }
    while ( r > 1 && a[ r ].first > sr ) { --r ; }
    return r ;
}

int getr ( int sr ) {
    int l , r , mid ;
    l = 1 , r = n ;
    while ( r - l > 1 ) {
        mid = ( l + r ) / 2 ;
        if ( a[ mid ].first < sr ) { l = mid ; }
        else { r = mid ; }
    }
    while ( l < n && a[ l ].first < sr ) { ++ l ; }
    return l ;
}

void input ( ) {
    scanf ( "%d%d" , &n , &q ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        scanf ( "%d" , &a[ i ].first ) ;
        a[ i ].second = i ;
    }
    sort ( a + 1 , a + n + 1 ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        pos[ a[ i ].second ] = i ;
    }
}

void solve ( ) {
    while ( q -- ) {
        int st , len ;
        scanf ( "%d%d" , &st , &len ) ;
        st = pos[ st ] ;
        int dir = 1 ;
        while ( 1 ) {
            if ( dir == 1 ) {
                int ret = getl ( a[ st ].first + len ) ;
                if ( ret == st ) { printf ( "%d\n" , a[ st ].second ) ; break ; }
                int diff = ( a[ ret ].first - a[ st ].first ) ;
                len %= ( diff << 1 ) ;
                if ( len >= diff ) {
                    len -= diff ;
                    dir = -1 ;
                    st = ret ;
                }
            }
            else {
                int ret = getr ( a[ st ].first - len ) ;
                if ( ret == st ) { printf ( "%d\n" , a[ st ].second ) ; break ; }
                int diff = ( a[ st ].first - a[ ret ].first ) ;
                len %= ( diff << 1 ) ;
                if ( len >= diff ) {
                    len -= diff ;
                    dir = 1 ;
                    st = ret ;
                }
            }
        }
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    input ( ) ;
    solve ( ) ;
    return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<set>
using namespace std ;

#define MAXN 200007

int n , q ;
pair < int , int > a[ MAXN ] ;
int pos[ MAXN ] ;

int getl ( int sr ) {
    int l , r , mid ;
    l = 1 , r = n ;
    while ( r - l > 1 ) {
        mid = ( l + r ) / 2 ;
        if ( a[ mid ].first > sr ) { r = mid ; }
        else { l = mid ; }
    }
    while ( r > 1 && a[ r ].first > sr ) { --r ; }
    return r ;
}

int getr ( int sr ) {
    int l , r , mid ;
    l = 1 , r = n ;
    while ( r - l > 1 ) {
        mid = ( l + r ) / 2 ;
        if ( a[ mid ].first < sr ) { l = mid ; }
        else { r = mid ; }
    }
    while ( l < n && a[ l ].first < sr ) { ++ l ; }
    return l ;
}

void input ( ) {
    scanf ( "%d%d" , &n , &q ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        scanf ( "%d" , &a[ i ].first ) ;
        a[ i ].second = i ;
    }
    sort ( a + 1 , a + n + 1 ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        pos[ a[ i ].second ] = i ;
    }
}

void solve ( ) {
    while ( q -- ) {
        int st , len ;
        scanf ( "%d%d" , &st , &len ) ;
        st = pos[ st ] ;
        int dir = 1 ;
        bool fst = true ;
        int cons = 0 ;
        while ( 1 ) {
            if ( len == 0 || cons >= 2 ) { printf ( "%d\n" , st ) ; break ; }
            if ( dir == 1 ) {
                int ret = getl ( a[ st ].first + len ) ;
                if ( ret == st ) { dir = -1 ; ++ cons ; continue ; }
                cons = 0 ;
                int diff = ( a[ ret ].first - a[ st ].first ) ;
                if ( fst == false ) {
                    len %= ( diff << 1 ) ;
                }
                if ( len >= diff ) {
                    len -= diff ;
                    dir = -1 ;
                    st = ret ;
                }
            }
            else {
                int ret = getr ( a[ st ].first - len ) ;
                if ( ret == st ) { dir = 1 ; ++ cons ; continue ; }
                cons = 0 ;
                int diff = ( a[ st ].first - a[ ret ].first ) ;
                if ( fst == false ) {
                    len %= ( diff << 1 ) ;
                }
                if ( len >= diff ) {
                    len -= diff ;
                    dir = 1 ;
                    st = ret ;
                }
            }
            fst = false ;
        }
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    input ( ) ;
    solve ( ) ;
    return 0 ;
}


#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2 * 100 * 1000 + 4;

int n, m, ind, L;
int x[N], f[N], rev[N];

bool cmp(const int &a, const int &b) {
    return x[a] < x[b];
}

void input() {
    cin.tie(0);

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> x[i];
        f[i] = i;
    }

    sort(f, f + n, cmp);
    for (int i = 0; i < n; i++)
        rev[f[i]] = i;

    sort(x, x + n);
}

void nxt() {
    int keep = upper_bound(x, x + n, x[ind] + L) - x - 1;

    if (keep == ind) {
        keep = lower_bound(x, x + n, x[ind] - L) - x;
        L -= x[ind] - x[keep];
        ind = keep;
        return;
    }

    L -= x[keep] - x[ind];
    ind = keep;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    input();
    while (m--) {
        cin >> ind >> L;
        ind--;
        ind = rev[ind];

        int keep, y;
        do {
            keep = L;
            nxt();
            y = ind;
            nxt();

            if (L > keep / 2) {
                int d = abs(x[y] - x[ind]);

                if (d == 0)
                    break;

                if ((L / d) % 2)
                    ind = y;
                L %= d;
            }
        }   while (L < keep);

        cout << f[ind] + 1 << '\n';
    }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2 * 100 * 1000 + 4;

int n, m, ind, L;
int x[N];

void input() {
    cin.tie(0);

    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> x[i];

    sort(x, x + n);
}

void nxt() {
    int keep = upper_bound(x, x + n, x[ind] + L) - x - 1;

    if (keep == ind) {
        keep = lower_bound(x, x + n, x[ind] - L) - x;
        L -= x[ind] - x[keep];
        ind = keep;
        return;
    }

    L -= x[keep] - x[ind];
    ind = keep;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    input();
    while (m--) {
        cin >> ind >> L;
        ind--;

        int keep, y;
        do {
            keep = L;
            nxt();
            y = ind;
            nxt();

            if (L > keep / 2) {
                int d = abs(x[y] - x[ind]);

                if (d == 0)
                    break;

                if ((L / d) % 2)
                    ind = y;
                L %= d;
            }
        }   while (L < keep);

        cout << ind + 1 << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>

const int N = 100005;

int n, m, x[N], v[N], w[N];

int next(int a, int l, bool typ) {
	if (!typ)
		return std::upper_bound(x + 1, x + n + 1, x[a] + l) - x - 1;
	else
		return std::lower_bound(x + 1, x + n + 1, x[a] - l) - x;
}
int run(int a, int l, int typ) {
	int R = next(a, l, typ);
	int _l = l - std::abs(x[R] - x[a]);
	int L = next(R, _l, typ ^ 1);
	if (L == R) return L;
	int dis = std::abs(x[R] - x[L]);
	int __l = _l - dis;
	int P = next(L, __l, typ);
	if (P == R) {
		int time = _l / dis & 1;
		return run(time ? L : R, _l % dis, typ ^ time ^ 1);
	} else return run(L, __l, typ);
}

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n >> m;
	for (int i = 1; i <= n; ++i) std::cin >> x[i], v[i] = i;
	std::sort(v + 1, v + n + 1, [] (int a, int b) {return x[a] < x[b]; });
	std::sort(x + 1, x + n + 1);
	for (int i = 1; i <= n; ++i) w[v[i]] = i;
	for (int i = 1; i <= m; ++i) {
		int x, l; std::cin >> x >> l;
		std::cout << v[run(w[x], l, 0)] << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>

const int N = 100005;

int n, m, x[N], v[N], w[N];

int next(int a, int l, bool typ) {
	if (!typ)
		return std::upper_bound(x + 1, x + n + 1, x[a] + l) - x - 1;
	else
		return std::lower_bound(x + 1, x + n + 1, x[a] - l) - x;
}
int run(int a, int l, int typ) {
	int R = next(a, l, typ);
	int _l = l - std::abs(x[R] - x[a]);
	int L = next(R, _l, typ ^ 1);
	if (L == R) return L;
	int dis = std::abs(x[R] - x[L]);
	int __l = _l - dis;
	int P = next(L, __l, typ);
	if (P == R) {
		int time = _l / dis & 1;
		return run(time ? L : R, _l % dis, typ ^ time);
	} else return run(L, __l, typ);
}

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n >> m;
	for (int i = 1; i <= n; ++i) std::cin >> x[i], v[i] = i;
	std::sort(v + 1, v + n + 1, [] (int a, int b) {return x[a] < x[b]; });
	std::sort(x + 1, x + n + 1);
	for (int i = 1; i <= n; ++i) w[v[i]] = i;
	for (int i = 1; i <= m; ++i) {
		int x, l; std::cin >> x >> l;
		std::cout << v[run(w[x], l, 0)] << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>

const int N = 100005;

int n, m, x[N], v[N], w[N];

int next(int a, int l, bool typ) {
	if (!typ)
		return std::upper_bound(x + 1, x + n + 1, x[a] + l) - x - 1;
	else
		return std::lower_bound(x + 1, x + n + 1, x[a] - l) - x;
}
int run(int a, int l, int typ) {
	int R = next(a, l, typ);
	int dis = std::abs(x[R] - x[a]);
	int _l = l - dis;
	int L = next(R, _l, typ ^ 1);
	int __l = _l - std::abs(x[R] - x[L]);
	if (L == R) return L;
	int P = next(L, __l, 0);
	if (P == R) {
		int time = l / dis & 1;
		return run(time ? R : L, l % dis, typ ^ time);
	} else return run(L, __l, typ);
}

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n >> m;
	for (int i = 1; i <= n; ++i) std::cin >> x[i], v[i] = i;
	std::sort(v + 1, v + n + 1, [] (int a, int b) {return x[a] < x[b]; });
	std::sort(x + 1, x + n + 1);
	for (int i = 1; i <= n; ++i) w[v[i]] = i;
	for (int i = 1; i <= m; ++i) {
		int x, l; std::cin >> x >> l;
		std::cout << v[run(w[x], l, 0)] << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>

const int N = 200005;

int n, m, x[N], v[N], w[N];

int next(int a, int l, bool typ) {
	if (!typ)
		return std::upper_bound(x + 1, x + n + 1, x[a] + l) - x - 1;
	else
		return std::lower_bound(x + 1, x + n + 1, x[a] - l) - x;
}
int run(int a, int l, int typ) {
	int R = next(a, l, typ);
	int _l = l - std::abs(x[R] - x[a]);
	int L = next(R, _l, typ ^ 1);
	if (L == R) return L;
	int dis = std::abs(x[R] - x[L]);
	int __l = _l - dis;
	int P = next(L, __l, typ);
	if (P == R) {
		int time = _l / dis & 1;
		return run(time ? L : R, _l % dis, typ ^ time ^ 1);
	} else return run(L, __l, typ);
}

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n >> m;
	for (int i = 1; i <= n; ++i) std::cin >> x[i], v[i] = i;
	std::sort(v + 1, v + n + 1, [] (int a, int b) {return x[a] < x[b]; });
	std::sort(x + 1, x + n + 1);
	for (int i = 1; i <= n; ++i) w[v[i]] = i;
	for (int i = 1; i <= m; ++i) {
		int x, l; std::cin >> x >> l;
		std::cout << v[run(w[x], l, 0)] << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>

const int N = 100005;

int n, m, x[N], v[N], w[N];

int next(int a, int l, bool typ) {
	if (!typ)
		return std::upper_bound(x + 1, x + n + 1, x[a] + l) - x - 1;
	else
		return std::lower_bound(x + 1, x + n + 1, x[a] - l) - x;
}
int run(int a, int l, int typ) {
	int R = next(a, l, typ);
	int _l = l - std::abs(x[R] - x[a]);
	int L = next(R, _l, typ ^ 1);
	int dis = std::abs(x[R] - x[L]);
	int __l = _l - std::abs(x[R] - x[L]);
	if (L == R) return L;
	int P = next(L, __l, 0);
	if (P == R) {
		int time = _l / dis & 1;
		return run(time ? R : L, _l % dis, typ ^ time);
	} else return run(L, __l, typ);
}

int main() {
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	std::cin >> n >> m;
	for (int i = 1; i <= n; ++i) std::cin >> x[i], v[i] = i;
	std::sort(v + 1, v + n + 1, [] (int a, int b) {return x[a] < x[b]; });
	std::sort(x + 1, x + n + 1);
	for (int i = 1; i <= n; ++i) w[v[i]] = i;
	for (int i = 1; i <= m; ++i) {
		int x, l; std::cin >> x >> l;
		std::cout << v[run(w[x], l, 0)] << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,x[200005];
pair<pair<int,int>,int> simulate(pair<pair<int,int>,int> p)
{
	if (p.second==1)
	{
		int st=p.first.first,en=n;
		while (st!=en)
		{
			int mid=(st+en+1)/2;
			if (x[mid]-x[p.first.first]<=p.first.second)
			st=mid;
			else
			en=mid-1;
		}
		return {{st,p.first.second-(x[st]-x[p.first.first])},-p.second};
	}
	else
	{
		int st=1,en=p.first.first;
		while (st!=en)
		{
			int mid=(st+en)/2;
			if (x[p.first.first]-x[mid]<=p.first.second)
			en=mid;
			else
			st=mid+1;
		}
		return {{st,p.first.second-(x[p.first.first]-x[st])},-p.second};
	}
}
int main()
{
	int q;
	scanf("%d%d",&n,&q);
	for (int i=1;i<=n;i++)
	scanf("%d",&x[i]);
	while (q--)
	{
		int a,l;
		scanf("%d%d",&a,&l);
		pair<pair<int,int>,int> p({a,l},1);
		while (1)
		{
			auto p2=simulate(p),p3=simulate(p2);
			if (p.first.first==p2.first.first && p2.first.first==p3.first.first)
			break;
			p=p3;
			if (p2.first.first!=p3.first.first)
			p.first.second%=2*(x[p2.first.first]-x[p3.first.first]);
			p=simulate(p);
		}
		printf("%d\n",p.first);
	}
}


#include <bits/stdc++.h>
using namespace std;
int n,x[200005],s[200005],inv[200005];
pair<pair<int,int>,int> simulate(pair<pair<int,int>,int> p)
{
	if (p.second==1)
	{
		int st=inv[p.first.first],en=n;
		while (st!=en)
		{
			int mid=(st+en+1)/2;
			if (x[s[mid]]-x[p.first.first]<=p.first.second)
			st=mid;
			else
			en=mid-1;
		}
		return {{s[st],p.first.second-(x[s[st]]-x[p.first.first])},-p.second};
	}
	else
	{
		int st=1,en=inv[p.first.first];
		while (st!=en)
		{
			int mid=(st+en)/2;
			if (x[p.first.first]-x[s[mid]]<=p.first.second)
			en=mid;
			else
			st=mid+1;
		}
		return {{s[st],p.first.second-(x[p.first.first]-x[s[st]])},-p.second};
	}
}
bool cmp(int a,int b)
{
	return x[a]<x[b];
}
int main()
{
	int q;
	scanf("%d%d",&n,&q);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&x[i]);
		s[i]=i;
	}
	sort(s+1,s+n+1,cmp);
	for (int i=1;i<=n;i++)
	inv[s[i]]=i;
	while (q--)
	{
		int a,l;
		scanf("%d%d",&a,&l);
		pair<pair<int,int>,int> p({a,l},1);
		while (1)
		{
			auto p2=simulate(p),p3=simulate(p2);
			if (p.first.first==p2.first.first && p2.first.first==p3.first.first)
			break;
			p=p3;
			if (p2.first.first!=p3.first.first)
			p.first.second%=2*(x[p2.first.first]-x[p3.first.first]);
			p=simulate(p);
		}
		printf("%d\n",p.first);
	}
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

using namespace std;

//using namespace __gnu_pbds;
#define pb push_back
#define F first
#define S second
#define ll long long
#define ld double long
#define ull unsigned long long
#define endl '\n'

const ll N = 2e5 + 36;
const ll INF = 1e9 + 7;
const ll X[8] = {1, 1, -1, -1, 2, 2, -2, -2};
const ll Y[8] = {2, -2, 2, -2, 1, -1, 1, -1};

pair < int, int > a[N];
int b[N];

signed main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
//    freopen("sum.in", "r", stdin);
//    freopen("sum.out", "w", stdout);
#endif // LOCAL
    int n, m;
    cin >> n >> m;
    for (int i(0); i < n; ++i) {
        cin >> a[i].F;
        a[i].S = i;
    }
    sort(a, a + n);
    for (int i(0); i < n; ++i) {
        b[a[i].S] = i;
    }
    for (int i(0); i < m; ++i) {
        int x, sz, otv = x;
        cin >> x >> sz;
        --x;
        x = b[x];
        while (true) {
            int l = x, r = n - 1, ans = x;
            while (l <= r) {
                int mid = (l + r) >> 1;
                if (a[mid].F <= a[x].F + sz) {
                    ans = mid;
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
            int R = ans;
            sz -= (a[R].F - a[x].F);
            l = 0, r = R, ans = R;
            while (l <= r) {
                int mid = (l + r) >> 1;
                if (a[mid].F >= a[R].F - sz) {
                    ans = mid;
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }
            int L = ans;
            if (L == R){
                otv = a[L].S;
                break;
            }
            int dst = a[R].F - a[L].F;
            int k = sz / dst;
            sz %= dst;
            if (k % 2){
                x = L;
            } else {
                x = R;
            }
        }
        cout << otv + 1 << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	for(int i=0; i<n; i++) cin >> a[i];
	sort(a.begin(), a.end());
	int cur;

	auto right = [&](int l, int r, int len){
		int cd = l;
		int st = l;
		while(l <= r){
			int mid = (l + r) / 2;
			int ln = a[mid] - a[st];
			if(ln > len) r = mid - 1;
			else{
				cd = mid;
				l = mid + 1;
			}
		}
		return cd;
	};

	auto left = [&](int l, int r, int len){
		int cd = r;
		int st = r;
		while(l <= r){
			int mid = (l + r) / 2;
			int ln = a[st] - a[mid];
			if(ln > len) l = mid + 1;
			else{
				cd = mid;
				r = mid -1;
			}
		}
		return cd;
	};

	for(int i=0; i<m; i++){
		int id, len;
		cin >> id >> len;
		cur = id-1;
		while(true){
			int nid = right(cur, n-1, len);
			// cout << nid << " " << cur << endl;
			if(nid != cur){
				int ln = abs(a[cur] - a[nid]);
				int cnt = len / ln;
				len %= ln;
				if(cnt%2) cur = nid;
			}
			else{
				nid = left(0, cur, len);
				// cout << nid << "  " << cur << endl;
				if(nid == cur){
					cout << (cur+1) << endl;
					break;
				}
				int ln = abs(a[cur] - a[nid]);
				int cnt = len / ln;
				len %= ln;
				if(cnt%2) cur = nid;
			}
		}
	}
}


#include <bits/stdc++.h>
#define sc second
#define fs first

using namespace std;

const int MAXN = 200500;

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    pair<int, int> b[MAXN];
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &b[i].fs);
        b[i].sc = i;
    }
    sort(b, b + n);
    int u[MAXN];
    for (int i = 0; i < n; i++)
        u[b[i].sc] = i;
    int x[MAXN];
    for (int i = 0; i < n; i++)
        x[i] = b[i].fs;
    int k, k1, dis, d, p, l, dir;
    for (int j = 0; j < m; j++)
    {
        scanf("%d %d", &p, &d);
        k = u[p - 1];
        dir = 1;
        dis = x[k] + d * dir;
        k1 = upper_bound(x, x + n, dis) - x - 1;
        d -= abs(x[k] - x[k1]);
        k = k1;
        dir *= -1;
        for ( ; ; )
        {
            dis = x[k] + d * dir;
            if (dir == 1)
                k1 = upper_bound(x, x + n, dis) - x - 1;
            else
                k1 = lower_bound(x, x + n, dis) - x;
            if (k == k1)
                break;
            l = abs(x[k] - x[k1]);
            if ((d / l) % 2 == 1)
            {
                dir *= -1;
                k = k1;
            }
            d = d % l;
        }
        printf("%d\n", b[k].sc + 1);
    }
    return 0;
}


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

#define ff first
#define ss second
#define pb push_back

const long long MAXN = 2e5 + 21, inf = 1e9 + 21;

int n, m;
int x[MAXN], s[MAXN];
map<int, int> mp;

inline int gr(int p, int l){
    return *(upper_bound(s, s + n, p + l) - 1);
}

inline int gl(int p, int l){
    return *lower_bound(s, s + n, p - l);
}

inline int solve(int p, int l){
    int rc = gr(p, l);
    int lc = gl(rc, l - rc + p);
    int tmp = (2 * rc) - p - lc;

    if(rc == lc && p == rc)
        return rc;

    if(lc == p)
        return solve(lc, l % tmp);
    else
        return solve(lc, l - tmp);
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> x[i];
        s[i] = x[i];
        mp[x[i]] = i + 1;
    }

    sort(s, s + n);

    while (m--){
        int a, l, t;
        cin >> a >> l;
        t = solve(x[--a], l);
        cout << mp[t] << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 200005;

int n, m, a[N], b[N];
map <int, int> mp;

int solve(int x, int len) {
  if (len == 0) {
    return x;
  }
  int id = lower_bound(b + 1, b + n + 1, x + len + 1) - (b + 1);
  if (b[id] == x) {
    id = lower_bound(b + 1, b + n + 1, x - len) - (b + 1);
    id++;
    if (b[id] == x) {
      return x;
    }
    int dis = x - b[id], cnt = len / dis;
    len %= dis;
    if (cnt & 1) {
      return solve(b[id], len);
    } else {
      return solve(x, len);
    }
  }
  return solve(b[id], len - b[id] + x);
}

int main() {
  ios_base :: sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mp[a[i]] = i;
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  while(m--) {
    int x, l;
    cin >> x >> l;
    cout << mp[solve(a[x], l)] << endl;
  }
  return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

const int BUFFER_SIZE = 1 << 20;
char rb[BUFFER_SIZE], *rp = rb, *rt = rb;
inline char read_char() {
    return rp == rt ? (rt = rb + fread(rb, 1, BUFFER_SIZE, stdin), rp = rb, *rp ++) : *rp ++;
}
inline int read_int() {
    int x = 0;
    char ch = read_char(), flag = 0;
    while (ch != '-' && (ch < '0' || ch > '9')) {
        ch = read_char();
    }
    if (ch == '-') {
        flag = 1;
        ch = read_char();
    }
    for (x = 0; ch >= '0' && ch <= '9'; ch = read_char()) {
        x = x * 10 + (ch - '0');
    }
    return flag ? -x : x;
}

const int MAX_N = 200000 + 5;

int N, M, nid[MAX_N];
pair<int, int> x[MAX_N];
int f[MAX_N][2], s[MAX_N], sn;

int main() {
    N = read_int();
    M = read_int();
    for (int i = 1; i <= N; i ++) {
        x[i].first = read_int();
        x[i].second = i;
    }
    sort(x + 1, x + 1 + N);
    for (int i = 1; i <= N; i ++) {
        nid[x[i].second] = i;
    }
    for (int i = 1; i <= M; i ++) {
        int a = nid[read_int()], aa;
        int l = read_int();
        for (int d = sn = 0; l && l != f[a][d]; d ^= 1, a = aa) {
            l = f[a][d] ? l % (f[a][d] - l) : l;
            f[s[++ sn] = a][d] = l;
            aa = d ? lower_bound(x + 1, x + 1 + N, make_pair(x[a].first - l, 0)) - x :
                     upper_bound(x + 1, x + 1 + N, make_pair(x[a].first + l, N + 1)) - 1 - x;
            l -= abs(x[aa].first - x[a].first);
        }
        printf("%d\n", x[a].second);
        for (int i = 1; i <= sn; i ++) {
            f[s[i]][0] = f[s[i]][1] = 0;
        }
    }
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

const int BUFFER_SIZE = 1 << 20;
char rb[BUFFER_SIZE], *rp = rb, *rt = rb;
inline char read_char() {
    return rp == rt ? (rt = rb + fread(rb, 1, BUFFER_SIZE, stdin), rp = rb, *rp ++) : *rp ++;
}
inline int read_int() {
    int x = 0;
    char ch = read_char(), flag = 0;
    while (ch != '-' && (ch < '0' || ch > '9')) {
        ch = read_char();
    }
    if (ch == '-') {
        flag = 1;
        ch = read_char();
    }
    for (x = 0; ch >= '0' && ch <= '9'; ch = read_char()) {
        x = x * 10 + (ch - '0');
    }
    return flag ? -x : x;
}

const int MAX_N = 200000 + 5;

int N, M, nid[MAX_N];
pair<int, int> x[MAX_N];
f[MAX_N][2], s[MAX_N], sn;

int main() {
    N = read_int();
    M = read_int();
    for (int i = 1; i <= N; i ++) {
        x[i].first = read_int();
        x[i].second = i;
    }
    sort(x + 1, x + 1 + N);
    for (int i = 1; i <= N; i ++) {
        nid[x[i].second] = i;
    }
    for (int i = 1; i <= M; i ++) {
        int a = nid[read_int()], aa;
        int l = read_int();
        for (int d = sn = 0; l && l != f[a][d]; d ^= 1, a = aa) {
            l = f[a][d] ? l % (f[a][d] - l) : l;
            f[s[++ sn] = a][d] = l;
            aa = d ? lower_bound(x + 1, x + 1 + N, make_pair(x[a].first - l, 0)) - x :
                     upper_bound(x + 1, x + 1 + N, make_pair(x[a].first + l, N + 1)) - 1 - x;
            l -= abs(x[aa].first - x[a].first);
        }
        printf("%d\n", x[a].second);
        for (int i = 1; i <= sn; i ++) {
            f[s[i]][0] = f[s[i]][1] = 0;
        }
    }
    return 0;
}


// :)
// "Khodaya, be man "Tagwaye setiz" biamooz ta
//  dar anbuh masuliat nalaghzam ..." -Shariati
#include <bits/stdc++.h>

using namespace std;
typedef long long int64;
typedef pair<int,int> pii;
typedef pair<int64,int64> pii64;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()

const int maxn = 2e5+10;
const int64 MO = 1e9+7;
const int IN = 1e9;

pii p[maxn];
int mp[maxn];

int n, m;

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> p[i].F;
		p[i].S = i;
	}
	//
	sort(p, p + n);
	for (int i = 0; i < n; i++)
		mp[p[i].S] = i;
	for (int i = 0; i < m; i++) {
		int id, L, last = -1, f = 0, num = 0;
		cin >> id >> L;
		id--;
		id = mp[id];
		while (true) {
			if (f == 0) {
				int nxt = upper_bound(p, p + n, MP(p[id].F + L, IN)) - p - 1;
				f = 1 - f;
				if (nxt == id) {
					num++;
					if (num > 1) {
						cout << p[id].S + 1 << "\n";
						break;
					}
					continue;
				}
				num = 0;
				L -= p[nxt].F - p[id].F;
				if (nxt == last)
					L %= 2 * (p[nxt].F - p[id].F);
				last = id;
				id = nxt;
				continue;
			}
			if (f == 1) {
				int nxt = lower_bound(p, p + n, MP(p[id].F - L, -1)) - p;
				f = 1 - f;
				if (nxt == id) {
					num++;
					if (num > 1) {
						cout << p[id].S + 1 << "\n";
						break;
					}
					continue;
				}
				num = 0;
				L -= p[id].F - p[nxt].F;
				if (nxt == last)
					L %= 2 * (p[id].F - p[nxt].F);
				last = id;
				id = nxt;
				continue;
			}
		}
	}
	//
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200007;
map<int,int>pos;

int a[MAXN];
int solve(int i, int l);

int swingBetween(int L, int R, int l)
{
    l %= 2LL*(a[R]-a[L]);
    if (l < a[R]-a[L]) return solve(L, l);
    l -= a[R]-a[L];

    auto it = pos.lower_bound(a[R]-l);
    L = it->second;
    l -= a[R]-a[L];
    if (L==R) return L;
    return swingBetween(L, R, l);
}

int solve(int i, int l)
{
    int L, R;
    auto it = pos.lower_bound(a[i]+l);
    if (it==pos.end() || it->first > a[i]+l) it--;
    R = it->second;
    l -= a[R]-a[i];
    it = pos.lower_bound(a[R]-l);
    L = it->second;
    l -= a[R]-a[L];
    if (L==R) return L;
    return swingBetween(L, R, l);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        pos[a[i]] = i;
    }

    while (m--) {
        int i, l;
        cin >> i >> l;
        cout << solve(i, l) << "\n";
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int sz = 2e5+1;
pii x[sz];
int xmap[sz];
int n;

int findRight(int xi, int len) {
	int l = xi, r = n, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[mid].first - x[xi].first <= len) {
			bst = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return bst;
}

int findLeft(int xi, int len) {
	int l = 1, r = xi, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[xi].first - x[mid].first <= len) {
			bst = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return bst;
}

int solve() {
	int xi, len;
	scanf("%d %d", &xi, &len);
	xi = xmap[xi];
	len += (x[xi].first - x[1].first);
	xi = 1;
	int nxt;
	bool isRight = 1;
	while (len) {
		nxt = (isRight ? findRight(xi, len) : findLeft(xi, len));
		if (nxt == xi)return x[xi].second;
		int diff = abs(x[nxt].first - x[xi].first);
		int changes = len / diff;
		if (changes & 1) {
			isRight = !isRight;
			xi = nxt;
		}
		len %= diff;
	}
	return x[xi].second;
}

int main() {
	//fast;
	int q;
	scanf("%d %d", &n, &q);
	foru(i, 0, n) {
		scanf("%d", &x[i + 1].first);
		x[i + 1].second = i + 1;
	}
	sort(x + 1, x + n + 1);
	for (int i = 1; i <= n; i++) xmap[x[i].second] = i;
	while (q--)printf("%d\n", solve());
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int sz = 2e5+1;
pii x[sz];
int xmap[sz];
int n;

inline int findRight(int xi, int len, int rightBound) {
	int l = xi, r = rightBound, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[mid].first - x[xi].first <= len) {
			bst = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return bst;
}

inline int findLeft(int xi, int len, int leftBound) {
	int l = leftBound, r = xi, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[xi].first - x[mid].first <= len) {
			bst = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return bst;
}

int solve() {
	int xi, len;
	scanf("%d %d", &xi, &len);
	xi = xmap[xi];
	len += (x[xi].first - x[1].first);
	xi = 1;
	int nxt, rightBound = n, leftBound = 1;
	bool isRight = 1;
	while (len) {
		nxt = (isRight ? findRight(xi, len,rightBound) : findLeft(xi, len,leftBound));
		if (nxt == xi)return x[xi].second;
		int diff = abs(x[nxt].first - x[xi].first);
		int changes = len / diff;
		if (changes & 1) {
			if (isRight)leftBound = xi;
			else rightBound = xi;
			isRight = !isRight;
			xi = nxt;
		}
		len %= diff;
	}
	return x[xi].second;
}

int main() {
	//fast;
	int q;
	scanf("%d %d", &n, &q);
	foru(i, 0, n) {
		scanf("%d", &x[i + 1].first);
		x[i + 1].first += 1e9 + 1;
		x[i + 1].second = i + 1;
	}
	sort(x + 1, x + n + 1);
	for (int i = 1; i <= n; i++) xmap[x[i].second] = i;
	while (q--)printf("%d\n", solve());
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (long long i = k; i < n; i++)
#define ford(i, k, n) for (long long i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> pii;
typedef pair<ll, ll> pll;

const long long sz = 2e5+1;
pii x[sz];
long long xmap[sz];
long long n;

inline long long findRight(long long xi, long long len, long long rightBound) {
	long long l = xi, r = rightBound, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[mid].first - x[xi].first <= len) {
			bst = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return bst;
}

inline long long findLeft(long long xi, long long len, long long leftBound) {
	long long l = leftBound, r = xi, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[xi].first - x[mid].first <= len) {
			bst = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return bst;
}

long long solve() {
	long long xi, len;
	scanf("%d %d", &xi, &len);
	xi = xmap[xi];
	len += (x[xi].first - x[1].first);
	xi = 1;
	long long nxt, rightBound = n, leftBound = 1;
	bool isRight = 1;
	while (len) {
		nxt = (isRight ? findRight(xi, len,rightBound) : findLeft(xi, len,leftBound));
		if (nxt == xi)return x[xi].second;
		long long diff = abs(x[nxt].first - x[xi].first);
		long long changes = len / diff;
		if (changes & 1) {
			if (isRight)leftBound = xi;
			else rightBound = xi;
			isRight = !isRight;
			xi = nxt;
		}
		len %= diff;
	}
	return x[xi].second;
}

int main() {
	//fast;
	long long q;
	scanf("%d %d", &n, &q);
	foru(i, 0, n) {
		scanf("%d", &x[i + 1].first);
		x[i + 1].first += 1e9 + 1;
		x[i + 1].second = i + 1;
	}
	sort(x + 1, x + n + 1);
	for (long long i = 1; i <= n; i++) xmap[x[i].second] = i;
	while (q--)printf("%d\n", solve());
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int sz = 2e5+1;
pii x[sz];
int xmap[sz];
int n;

int findRight(int xi, int len) {
	int l = xi, r = n, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[mid].first - x[xi].first <= len) {
			bst = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return bst;
}

int findLeft(int xi, int len) {
	int l = 1, r = xi, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[xi].first - x[mid].first <= len) {
			bst = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return bst;
}

int solve() {
	int xi, len;
	scanf("%d %d", &xi, &len);
	xi = xmap[xi];
	len += (x[xi].first - x[1].first);
	xi = 1;
	int nxt;
	bool isRight = 1;
	while (len) {
		nxt = (isRight ? findRight(xi, len) : findLeft(xi, len));
		if (nxt == xi)return x[xi].second;
		int diff = abs(x[nxt].first - x[xi].first);
		int changes = len / diff;
		if (changes & 1) {
			isRight = !isRight;
			xi = nxt;
		}
		len %= diff;
	}
	return x[xi].second;
}

int main() {
	//fast;
	int q;
	scanf("%d %d", &n, &q);
	foru(i, 0, n) {
		scanf("%d", &x[i + 1].first);
		x[i + 1].first += 1e9 + 1;
		x[i + 1].second = i + 1;
	}
	sort(x + 1, x + n + 1);
	for (int i = 1; i <= n; i++) xmap[x[i].second] = i;
	while (q--)printf("%d\n", solve());
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (long long i = k; i < n; i++)
#define ford(i, k, n) for (long long i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> pii;
typedef pair<ll, ll> pll;

const long long sz = 2e5+1;
pii x[sz];
long long xmap[sz];
long long n;

inline long long findRight(long long xi, long long len, long long rightBound) {
	long long l = xi, r = rightBound, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[mid].first - x[xi].first <= len) {
			bst = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return bst;
}

inline long long findLeft(long long xi, long long len, long long leftBound) {
	long long l = leftBound, r = xi, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[xi].first - x[mid].first <= len) {
			bst = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return bst;
}

long long solve() {
	long long xi, len;
	scanf("%d %d", &xi, &len);
	xi = xmap[xi];
	len += (x[xi].first - x[1].first);
	xi = 1;
	long long nxt, rightBound = n, leftBound = 1;
	bool isRight = 1;
	while (len) {
		nxt = (isRight ? findRight(xi, len,rightBound) : findLeft(xi, len,leftBound));
		if (nxt == xi)return x[xi].second;
		long long diff = abs(x[nxt].first - x[xi].first);
		long long changes = len / diff;
		if (changes & 1) {
			if (isRight)leftBound = xi;
			else rightBound = xi;
			isRight = !isRight;
			xi = nxt;
		}
		len %= diff;
	}
	return x[xi].second;
}

long long main() {
	//fast;
	long long q;
	scanf("%d %d", &n, &q);
	foru(i, 0, n) {
		scanf("%d", &x[i + 1].first);
		x[i + 1].first += 1e9 + 1;
		x[i + 1].second = i + 1;
	}
	sort(x + 1, x + n + 1);
	for (long long i = 1; i <= n; i++) xmap[x[i].second] = i;
	while (q--)prlong longf("%d\n", solve());
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int sz = 2e5+1;
int x[sz];
int n;

int findRight(int xi, int len) {
	int l = xi, r = n, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[mid] - x[xi] <= len) {
			bst = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return bst;
}

int findLeft(int xi, int len) {
	int l = 1, r = xi, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[xi] - x[mid] <= len) {
			bst = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return bst;
}

int solve() {
	int xi, len;
	scanf("%d %d", &xi, &len);
	len += (x[xi] - x[1]);
	xi = 1;
	int nxt;
	bool isRight = 1;
	while (len) {
		nxt = (isRight ? findRight(xi, len) : findLeft(xi, len));
		if (nxt == xi)return xi;
		int diff = abs(x[nxt] - x[xi]);
		int changes = len / diff;
		if (changes & 1) {
			isRight = !isRight;
			xi = nxt;
		}
		len %= diff;
	}
	return xi;
}

int main() {
	//fast;
	int q;
	scanf("%d %d", &n, &q);
	foru(i, 0, n) scanf("%d", &x[i+1]);
	while (q--)printf("%d\n", solve());
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (long long i = k; i < n; i++)
#define ford(i, k, n) for (long long i = k; i >= n; i--)
#define pb push_back
#define mp make_pair

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> pii;
typedef pair<ll, ll> pll;

const long long sz = 2e5 + 1;
pii x[sz];
long long xmap[sz];
long long n;

long long findRight(long long xi, long long len) {
	long long l = xi, r = n, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[mid].first - x[xi].first <= len) {
			bst = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return bst;
}

long long findLeft(long long xi, long long len) {
	long long l = 1, r = xi, mid, bst = -1;
	while (l <= r) {
		mid = (l + r + 1) / 2;
		if (x[xi].first - x[mid].first <= len) {
			bst = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return bst;
}

long long solve() {
	long long xi, len;
	scanf("%lld %lld", &xi, &len);
	xi = xmap[xi];
	len += (x[xi].first - x[1].first);
	xi = 1;
	long long nxt;
	bool isRight = 1;
	while (len) {
		nxt = (isRight ? findRight(xi, len) : findLeft(xi, len));
		if (nxt == xi)return x[xi].second;
		long long diff = abs(x[nxt].first - x[xi].first);
		long long changes = len / diff;
		if (changes & 1) {
			isRight = !isRight;
			xi = nxt;
		}
		len %= diff;
	}
	return x[xi].second;
}

int main() {
	//fast;
	long long q;
	scanf("%lld %lld", &n, &q);
	foru(i, 0, n) {
		scanf("%lld", &x[i + 1].first);
		x[i + 1].second = i + 1;
	}
	sort(x + 1, x + n + 1);
	for (long long i = 1; i <= n; i++) xmap[x[i].second] = i;
	while (q--)printf("%lld\n", solve());
	return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
//#define int ll
#define F first
#define S second

signed main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  unordered_map<int, int> num;
  int ptr = 0;
  for (auto &it : a) {
    cin >> it;
    num[it] = ptr++;
  }
  sort(all(a));
  vector<int> pos(n);
  for (int i = 0 ; i < n; i++) {
    pos[num[a[i]]] = i;
  }
  for (int i = 0; i < m; i++) {
    int cur, len;
    cin >> cur >> len;
    cur--;
    cur = pos[cur];
    int prv = -1, prv2 = -1;
    for (;;) {
      int to = upper_bound(all(a), a[cur] + len) - a.begin() - 1;
      if (to == cur) {
        int to2 = lower_bound(all(a), a[cur] - len) - a.begin();
        if (to2 == cur) {
          break;
        }
        len -= a[cur] - a[to2];
        if (prv == to2 && prv2 == cur) {
          len %= 2ll * (a[cur] - a[to2]);
        }
        prv2 = prv;
        prv = cur;
        cur = to2;
      }
      else {
        len -= a[to] - a[cur];
        if (prv == to && prv2 == cur) {
          len %= 2ll * (a[to] - a[cur]);
        }
        prv2 = prv;
        prv = cur;
        cur = to;
      }
    }
    cout << num[a[cur]] + 1 << '\n';
  }
  //cerr << "TIME :: " << clock() * 1.0 / CLOCKS_PER_SEC << '\n';
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define int ll
#define F first
#define S second

signed main() {
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
  unordered_map<int, int> num;
	int ptr = 0;
  for (auto &it : a) {
		cin >> it;
    num[it] = ptr++;
	}
	sort(all(a));
  vector<int> pos(n);
  for (int i =0 ; i < n; i++) {
    pos[num[a[i]]] = i;
  }
	for (int i = 0; i < m; i++) {
		int cur, len;
		cin >> cur >> len;
		cur--;
    cur = pos[cur];
		for (;;) {
			int to = upper_bound(all(a), a[cur] + len) - a.begin() - 1;
			if (to == cur) {
				int to2 = lower_bound(all(a), a[cur] - len) - a.begin();
				if (to2 == cur) {
					break;
				}
				len -= a[cur] - a[to2];
				cur = to2;
			}
			else {
				len -= a[to] - a[cur];
				cur = to;
			}
		}
		cout << num[a[cur]] + 1 << '\n';
	}
	//cerr << "TIME :: " << clock() * 1.0 / CLOCKS_PER_SEC << '\n';
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define int ll
#define F first
#define S second

signed main() {
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	for (auto &it : a) {
		cin >> it;
	}
	sort(all(a));
	for (int i = 0; i < m; i++) {
		int cur, len;
		cin >> cur >> len;
		cur--;
		for (;;) {
			int to = upper_bound(all(a), a[cur] + len) - a.begin() - 1;
			if (to == cur) {
				int to2 = lower_bound(all(a), a[cur] - len) - a.begin();
				if (to2 == cur) {
					break;
				}
				len -= a[cur] - a[to2];
				cur = to2;
			}
			else {
				len -= a[to] - a[cur];
				cur = to;
			}
		}
		cout << cur + 1 << '\n';
	}
	//cerr << "TIME :: " << clock() * 1.0 / CLOCKS_PER_SEC << '\n';
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define int ll
#define F first
#define S second

signed main() {
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	for (auto &it : a) {
		cin >> it;
	}
	sort(all(a));
	for (int i = 0; i < m; i++) {
		int cur, len;
		cin >> cur >> len;
		cur--;
		for (;;) {
			int to = upper_bound(all(a), a[cur] + len) - a.begin() - 1;
			if (to == cur) {
				int to2 = lower_bound(all(a), a[cur] - len) - a.begin();
				if (to2 == cur) {
					break;
				}
				len = min(a[cur] - a[to2], len - a[cur] + a[to2]);
				cur = to2;
			}
			else {
				len = min(a[to] - a[cur], len - a[to] + a[cur]);
				cur = to;
			}
		}
		cout << cur + 1 << '\n';
	}
	//cerr << "TIME :: " << clock() * 1.0 / CLOCKS_PER_SEC << '\n';
}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
//#define int ll
#define F first
#define S second

signed main() {
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
  unordered_map<int, int> num;
	int ptr = 0;
  for (auto &it : a) {
		cin >> it;
    num[it] = ptr++;
	}
	sort(all(a));
  vector<int> pos(n);
  for (int i =0 ; i < n; i++) {
    pos[num[a[i]]] = i;
  }
	for (int i = 0; i < m; i++) {
		int cur, len;
		cin >> cur >> len;
		cur--;
    cur = pos[cur];
		for (;;) {
			int to = upper_bound(all(a), a[cur] + len) - a.begin() - 1;
			if (to == cur) {
				int to2 = lower_bound(all(a), a[cur] - len) - a.begin();
				if (to2 == cur) {
					break;
				}
				len -= a[cur] - a[to2];
				cur = to2;
			}
			else {
				len -= a[to] - a[cur];
				cur = to;
			}
		}
		cout << num[a[cur]] + 1 << '\n';
	}
	//cerr << "TIME :: " << clock() * 1.0 / CLOCKS_PER_SEC << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
#define int ll
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2100);
const int N = 200005;

int n;
int a[N];
pair<int, int> b[N];
int u[N];

void solv()
{
    int qq;
    scanf("%lld%lld", &n, &qq);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%lld", &a[i]);
        b[i] = m_p(a[i], i);
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; ++i)
        u[b[i].se] = i;

    while (qq--)
    {
        int x, d;
        scanf("%lld%lld", &x, &d);
        x = u[x];
        bool z = false;

        int y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

        if (x == y)
        {
            printf("%lld\n", b[x].se);
            continue;
        }

        d -= abs(a[x] - a[y]);
        x = y;
        z ^= true;

        if (x == n)
        {
            int q = d / (a[n] - a[1]);
            d %= (a[n] - a[1]);
            if (q % 2 == 1)
            {
                x = 1;
                z ^= true;
            }
        }

        while (1)
        {
            if (!z)
            {
                y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

                if (x == y)
                {
                    printf("%lld\n", b[x].se);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
            else
            {
                int y = lower_bound(a + 1, a + n + 1, a[x] - d) - a;

                if (x == y)
                {
                    printf("%lld\n", b[x].se);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
        }
    }
}

int32_t main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2100);
const int N = 200005;

int n;
int a[N];
pair<int, int> b[N];
int u[N];

void solv()
{
    int qq;
    scanf("%d%d", &n, &qq);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        b[i] = m_p(a[i], i);
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; ++i)
        u[b[i].se] = i;

    while (qq--)
    {
        int x, d;
        scanf("%d%d", &x, &d);
        x = u[x];
        bool z = false;

        int y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

        if (x == y)
        {
            printf("%d\n", b[x].se);
            continue;
        }

        d -= abs(a[x] - a[y]);
        x = y;
        z ^= true;

        if (x == n)
        {
            int q = d / (a[n] - a[1]);
            d %= (a[n] - a[1]);
            if (q % 2 == 1)
            {
                x = 1;
                z ^= true;
            }
        }

        while (1)
        {
            if (!z)
            {
                y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

                if (x == y)
                {
                    printf("%d\n", b[x].se);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
            else
            {
                int y = lower_bound(a + 1, a + n + 1, a[x] - d) - a;

                if (x == y)
                {
                    printf("%d\n", b[x].se);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
        }
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2100);
const int N = 200005;

int n;
int a[N];
pair<int, int> b[N];
int u[N];

void solv()
{
    int qq;
    scanf("%d%d", &n, &qq);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        b[i] = m_p(a[i], i);
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; ++i)
        u[b[i].se] = i;

    while (qq--)
    {
        int x, d;
        scanf("%d%d", &x, &d);
        x = u[x];
        bool z = false;

        if (n == 1)
        {
            printf("1\n");
            continue;
        }

        int qq = 0;

        while (1)
        {
            ++qq;
            assert(qq <= 40);
            if (x == n)
            {
                int q = d / (a[n] - a[1]);
                d %= (a[n] - a[1]);
                if (q % 2 == 1)
                    x = 1;
            }
            if (x == 1)
            {
                int q = d / (a[n] - a[1]);
                d %= (a[n] - a[1]);
                if (q % 2 == 1)
                    x = n;
            }

            if (!z)
            {
                int y;
                if (x < n && a[x] + d >= a[x + 1])
                    y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;
                else
                    y = lower_bound(a + 1, a + n + 1, a[x] - d) - a;

                if (x == y)
                    break;
                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
            else
            {
                int y;
                if (x > 1 && a[x] - d <= a[x - 1])
                    y = lower_bound(a + 1, a + n + 1, a[x] - d) - a;
                else
                    y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

                if (x == y)
                    break;

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
            assert(1 <= x && x <= n);
        }
        printf("%d\n", b[x].se);
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2100);
const int N = 200005;

int n;
int a[N];

void solv()
{
    int qq;
    scanf("%d%d", &n, &qq);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    while (qq--)
    {
        int x, d;
        scanf("%d%d", &x, &d);
        bool z = false;

        int y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

        if (x == y)
        {
            printf("%d\n", x);
            continue;
        }

        d -= abs(a[x] - a[y]);
        x = y;
        z ^= true;

        if (x == n)
        {
            int q = d / (a[n] - a[1]);
            d %= (a[n] - a[1]);
            if (q % 2 == 1)
            {
                x = 1;
                z ^= true;
            }
        }

        while (1)
        {
            if (!z)
            {
                y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

                if (x == y)
                {
                    printf("%d\n", x);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
            else
            {
                int y = lower_bound(a + 1, a + n + 1, a[x] - d) - a;

                if (x == y)
                {
                    printf("%d\n", x);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
        }
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2100);
const int N = 200005;

int n;
int a[N];
pair<int, int> b[N];
int u[N];

void solv()
{
    int qq;
    scanf("%d%d", &n, &qq);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        b[i] = m_p(a[i], i);
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; ++i)
        u[b[i].se] = i;

    while (qq--)
    {
        int x, d;
        scanf("%d%d", &x, &d);
        x = u[x];
        bool z = false;

        if (n == 1)
        {
            printf("1\n");
            continue;
        }

        int qq = 0;

        while (1)
        {
            if (!z)
            {
                if (!(x < n && a[x] + d >= a[x + 1]))
                    z ^= true;
            }
            else
            {
                if (!(x > 1 && a[x] - d <= a[x - 1]))
                    z ^= true;
            }
            if (!z)
            {
                int y;
                y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

                if (x == y)
                    break;

                int q = (d / abs(a[x] - a[y]));
                d %= abs(a[x] - a[y]);

                if (q % 2 == 1)
                {
                    x = y;
                }
                else
                {
                    z ^= true;
                }
            }
            else
            {
                int y;
                y = lower_bound(a + 1, a + n + 1, a[x] - d) - a;

                if (x == y)
                    break;

                int q = (d / abs(a[x] - a[y]));
                d %= abs(a[x] - a[y]);
                if (q % 2 == 1)
                {
                    x = y;
                }
                else
                {
                    z ^= true;
                }
            }
            assert(1 <= x && x <= n);
        }
        printf("%d\n", b[x].se);
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2100);
const int N = 200005;

int n;
int a[N];
pair<int, int> b[N];
int u[N];

void solv()
{
    int qq;
    scanf("%d%d", &n, &qq);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        b[i] = m_p(a[i], i);
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; ++i)
        u[b[i].se] = i;

    while (qq--)
    {
        int x, d;
        scanf("%d%d", &x, &d);
        x = u[x];
        bool z = false;

        int y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

        if (x == y)
        {
            printf("%d\n", b[x].se);
            continue;
        }

        d -= abs(a[x] - a[y]);
        x = y;
        z ^= true;

        /*if (x == n)
        {
            int q = d / (a[n] - a[1]);
            d %= (a[n] - a[1]);
            if (q % 2 == 1)
            {
                x = 1;
                z ^= true;
            }
        }*/

        while (1)
        {
            if (!z)
            {
                y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

                if (x == y)
                {
                    printf("%d\n", b[x].se);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
            else
            {
                int y = lower_bound(a + 1, a + n + 1, a[x] - d) - a;

                if (x == y)
                {
                    printf("%d\n", b[x].se);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
        }
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2100);
const int N = 200005;

int n;
int a[N];
pair<int, int> b[N];
int u[N];

void solv()
{
    int qq;
    scanf("%d%d", &n, &qq);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        b[i] = m_p(a[i], i);
    }
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; ++i)
        u[b[i].se] = i;

    while (qq--)
    {
        int x, d;
        scanf("%d%d", &x, &d);
        x = u[x];
        bool z = false;

        /*int y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

        if (x == y)
        {
            printf("%d\n", b[x].se);
            continue;
        }

        d -= abs(a[x] - a[y]);
        x = y;
        z ^= true;*/

        /*if (x == n)
        {
            int q = d / (a[n] - a[1]);
            d %= (a[n] - a[1]);
            if (q % 2 == 1)
            {
                x = 1;
                z ^= true;
            }
        }*/

        while (1)
        {
            if (!z)
            {
                int y = upper_bound(a + 1, a + n + 1, a[x] + d) - a - 1;

                if (x == y)
                {
                    printf("%d\n", b[x].se);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
            else
            {
                int y = lower_bound(a + 1, a + n + 1, a[x] - d) - a;

                if (x == y)
                {
                    printf("%d\n", b[x].se);
                    break;
                }

                d -= abs(a[x] - a[y]);
                x = y;
                z ^= true;
            }
            assert(1 <= x && x <= n);
        }
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}