inlinevoidInput(){ read(n, Q); for(int i = 1; i <= n; i++) { read(a[i]); } }
inlinevoidInit(){ for(int i = 1; i <= n; i++) c[i] = a[i]; sort(c + 1, c + n + 1); int m = unique(c + 1, c + n + 1) - c; for(int i = 1; i <= n; i++) { a[i] = lower_bound(c + 1, c + m, a[i]) - c; idx[a[i]].push_back(i); } B = sqrt(n); for(int l = 0; l <= n; l += B) { if(l == 0) continue; for(int i = 1; i <= m; i++) cnt[i] = 0; int ans = 0; for(int r = l; r <= n; r++) { cnt[a[r]]++; if(cnt[a[r]] > cnt[ans] || cnt[a[r]] == cnt[ans] && a[r] < ans) ans = a[r]; if((r + 1) % B == 0 || r == n) ds[l / B][r / B] = ans; } } }
inlineintCalc(int l, int r, int x){ returnupper_bound(idx[x].begin(), idx[x].end(), r) - lower_bound(idx[x].begin(), idx[x].end(), l); }
inlineintQuery(int l, int r){ int idl = l / B, idr = r / B; int ans = 0; if(idl -1 < idr) ans = ds[idl + 1][idr - 1]; int lst = Calc(l, r, ans), now = 0; if(idl == idr) { for(int i = l; i <= r; i++) { now = Calc(l, r, a[i]); if(lst < now || lst == now && a[i] < ans) { ans = a[i], lst = now; } } return c[ans]; } for(int i = l; i < (idl + 1) * B; i++) { now = Calc(l, r, a[i]); if(lst < now || now == lst && a[i] < ans) { ans = a[i], lst = now; } } for(int i = idr * B; i <= r; i++) { now = Calc(l, r, a[i]); if(lst < now || now == lst && a[i] < ans) { ans = a[i], lst = now; } } return c[ans]; }
inlinevoidWork(){ Init(); int ans = 0, l, r; while(Q--) { scanf("%d%d", &l, &r); l = (l + ans - 1) % n + 1; r = (r + ans - 1) % n + 1; if(l > r) swap(l, r); ans = Query(l, r); printf("%d\n", ans); } }
intmain(){ int T = 1; while(T--) { Input(); Work(); } return0; }