#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int N = 2e5 + 100;   int t[20 * N], ls[20 * N], rs[20 * N], a[N], root[N];   int cur_size = 1;   void upd(int &v, int u, int l, int r, int pos, int x) {     v = cur_size++;     t[v] = t[u] + x, ls[v] = ls[u], rs[v] = rs[u];     if (l + 1 == r) {       return;     }     int mid = (l + r) / 2;     if (pos < mid)       upd(ls[v], ls[u], l, mid, pos, x);     else       upd(rs[v], rs[u], mid, r, pos, x);     t[v] = t[ls[v]] + t[rs[v]];   }   int get(int v, int l, int r, int ql, int qr) {     if (qr <= l || r <= ql) return 0;     if (ql <= l && r <= qr) return t[v];     int mid = (l + r) / 2;     return get(ls[v], l, mid, ql, qr) + get(rs[v], mid, r, ql, qr);   }   signed main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n, k;     cin >> n >> k;     vector<int> pos[N];     for (int i = 1; i <= n; i++) {       cin >> a[i];       pos[a[i]].push_back(i);     }     for (int i = 1; i <= n; i++) {       int p =           lower_bound(pos[a[i]].begin(), pos[a[i]].end(), i) - pos[a[i]].begin();       upd(root[i], root[i - 1], 1, n + 1, i, 1);       if (p >= k) {         p -= k;         upd(root[i], root[i], 1, n + 1, pos[a[i]][p], -1);       }     }     int q;     cin >> q;     int ans = 0;     while (q--) {       int l, r;       cin >> l >> r;       l = (l + ans) % n + 1;       r = (r + ans) % n + 1;       if (l > r) swap(l, r);       ans = get(root[r], 1, n + 1, l, r + 1);       cout << ans <<   n ;     }   }   