#include <bits/stdc++.h>
using namespace std;
const int maxn = 200050, maxns = 8000050;
int n, m, q, ans;
pair<int, int> a[maxn];
int ts, root[maxn];
unsigned long long tr[maxns];
bool cmp(const int &lhs, const int &rhs) { return a[lhs] < a[rhs]; }
inline void Get(bool lt, bool rt, unsigned long long x, int &ls, int &rs,
                int &s, int &t) {
  s = x % maxn, ls = x / maxn / maxns, rs = x / maxn % maxns;
  t = s - min(lt ? ls : tr[ls] % maxn, rt ? rs : tr[rs] % maxn);
}
inline unsigned long long Chg(int ls, int rs, int s) {
  return ((unsigned long long)ls * maxns + rs) * maxn + s;
}
inline void add(int x0, int &x, int ll, int rr, int li, int ri, int k) {
  if (ll == rr) {
    x = x0 + 1;
    return;
  }
  tr[x = ++ts] = tr[x0];
  if (li <= ll && rr <= ri) {
    tr[x] += k;
    return;
  }
  int mid = (ll + rr) >> 1, lt = (ll == mid), rt = (rr == mid + 1), ls0, rs0,
      s0, ls = 0, rs = 0, s, t;
  Get(lt, rt, tr[x0], ls0, rs0, s0, t);
  if (li <= mid) add(ls0, ls, ll, mid, li, ri, k);
  if (ri > mid) add(rs0, rs, mid + 1, rr, li, ri, k);
  if (!ls) ls = ls0;
  if (!rs) rs = rs0;
  s = min(lt ? ls : tr[ls] % maxn, rt ? rs : tr[rs] % maxn) + t;
  tr[x] = Chg(ls, rs, s);
}
inline int query(int x, int ll, int rr, int li, int ri) {
  if (ll == rr) return x;
  if (!x) return 0;
  if (li <= ll && rr <= ri) return tr[x] % maxn;
  int mid = (ll + rr) >> 1, ls, rs, s, t;
  Get(ll == mid, rr == mid + 1, tr[x], ls, rs, s, t);
  s = maxn;
  if (li <= mid) s = min(s, query(ls, ll, mid, li, ri));
  if (ri > mid) s = min(s, query(rs, mid + 1, rr, li, ri));
  return s + t;
}
int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%d", &a[i].first), a[i].second = i;
  sort(a + 1, a + 1 + n);
  for (i = 1; i <= n; i++)
    add(root[i - 1], root[i], 1, n - m + 1, a[i].second - m + 1, a[i].second,
        1);
  scanf("%d", &q);
  while (q--) {
    int li, ri, xi, pos;
    scanf("%d%d%d", &li, &ri, &xi);
    xi ^= ans;
    pos = lower_bound(a + 1, a + 1 + n, make_pair(xi, 0)) - a - 1;
    ans = query(root[pos], 1, n - m + 1, li, ri);
    printf("%d\n", ans);
  }
  return 0;
}
