#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int __OneWan_2024 = [](){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 0;
}();
int belong[40005], st[40005], ed[40005];
int a[40005];
int cnt[40][40][40005], mode[40][40], tcnt[40005];
vector<int> ind(1);
void add(int L, int R, int idx) {
cnt[L][R][a[idx]]++;
if (cnt[L][R][a[idx]] > cnt[L][R][0] || (cnt[L][R][a[idx]] == cnt[L][R][0] && ind[a[idx]] < mode[L][R])) {
mode[L][R] = ind[a[idx]];
cnt[L][R][0] = cnt[L][R][a[idx]];
}
}
void del(int L, int R, int idx) {
cnt[L][R][a[idx]]--;
}
int main() {
int n, m;
cin >> n >> m;
int B = pow(n, 0.3333333333);
{
int cnt = n / B;
for (int i = 1 ; i <= B ; i++) {
st[i] = cnt * (i - 1) + 1;
ed[i] = cnt * i;
}
ed[B] = n;
for (int i = 1 ; i <= B ; i++) {
for (int j = st[i] ; j <= ed[i] ; j++) {
belong[j] = i;
}
}
}
for (int i = 1 ; i <= n ; i++) {
cin >> a[i];
ind.push_back(a[i]);
}
sort(begin(ind), end(ind));
ind.resize(unique(begin(ind), end(ind)) - begin(ind));
for (int i = 1 ; i <= n ; i++) {
a[i] = lower_bound(begin(ind), end(ind), a[i]) - begin(ind);
}
for (int i = 1 ; i <= B ; i++) {
for (int j = i ; j <= B ; j++) {
for (int k = st[i] ; k <= ed[j] ; k++) {
add(i, j, k);
}
}
}
int ans = 0;
for (int i = 1 ; i <= m ; i++) {
int L, R;
cin >> L >> R;
L = (L + ans - 1) % n + 1;
R = (R + ans - 1) % n + 1;
if (L > R) {
swap(L, R);
}
int x = belong[L], y = belong[R];
if (abs(x - y) <= 2) { // 如果查询在一个块内, 暴力查询
ans = 0;
for (int j = L ; j <= R ; j++) {
tcnt[a[j]]++;
if (tcnt[a[j]] > tcnt[0] || (tcnt[a[j]] == tcnt[0] && ind[a[j]] < ans)) {
ans = ind[a[j]];
tcnt[0] = tcnt[a[j]];
}
}
cout << ans << "\n";
for (int j = L ; j <= R ; j++) {
tcnt[a[j]]--;
}
tcnt[0] = 0;
continue;
}
int tx = x + 1, ty = y - 1;
int tmode = mode[tx][ty];
int tcnt = cnt[tx][ty][0];
for (int j = L ; j <= ed[x] ; j++) { // 把左边多余部分加进来
add(tx, ty, j);
}
for (int j = st[y] ; j <= R ; j++) { // 把右边多余部分加进来
add(tx, ty, j);
}
ans = mode[tx][ty];
cout << ans << "\n";
for (int j = L ; j <= ed[x] ; j++) { // 删去左边多余部分
del(tx, ty, j);
}
for (int j = st[y] ; j <= R ; j++) { // 删去右边多余部分
del(tx, ty, j);
}
mode[tx][ty] = tmode; // 复原众数
cnt[tx][ty][0] = tcnt; // 复原数量
}
return 0;
}