#include <bits/stdc++.h>
using namespace std;
const int N = 200010, M = 510;
int n, m, lis[N], a[N], bc, b[N], bl[N], br[N], p[N], cnt, v[N];
int f[M][M], la[N], w[N], st[M][M], pre[N], suf[N], ed[N];
inline bool comp(int i, int j) { return a[i] > a[j]; }
inline void Modify(int k, int l, int r) {
  if (b[l] == b[r]) {
    for (int i = l; i <= r; i++) w[i]++;
    for (int i = bl[b[l]]; i <= br[b[l]]; i++) pre[k] = max(pre[k], w[i]);
    return;
  }
  for (int i = l; i <= br[b[l]]; i++) w[i]++;
  for (int i = bl[b[l]]; i <= br[b[l]]; i++) pre[k] = max(pre[k], w[i]);
  for (int i = b[l] + 1; i < b[r]; i++) la[i]++;
  for (int i = bl[b[r]]; i <= r; i++) w[i]++;
  for (int i = bl[b[r]]; i <= br[b[r]]; i++) suf[k] = max(suf[k], w[i]);
}
inline void rec(int k) {
  for (int i = 1; i <= cnt; i++) {
    for (int j = bl[i]; j <= br[i]; j++) f[k][i] = max(f[k][i], w[j] += la[i]);
    st[k][i] = w[bl[i]];
    la[i] = 0;
  }
}
inline int qry(int l, int r, int k, int x) {
  int now = v[k], ret = 0;
  for (int i = bl[k]; i < l; i++) now += (a[i + m] >= x) - (a[i] >= x);
  for (int i = l; i <= r; i++)
    ret = max(ret, now), now += (a[i + m] >= x) - (a[i] >= x);
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  bc = sqrt(n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), lis[i] = a[i], p[i] = i;
  sort(lis + 1, lis + n + 1);
  int tp = unique(lis + 1, lis + n + 1) - lis - 1, Q, ans = 0;
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(lis + 1, lis + tp + 1, a[i]) - lis;
  for (int i = 1, t; i <= n; i++)
    t = b[i] = (i - 1) / bc + 1, bl[t] = bl[t] ? bl[t] : i, br[t] = i;
  sort(p + 1, p + n + 1, comp);
  cnt = b[n];
  for (int i = 1; i <= n; i++) {
    Modify(i, max(1, p[i] - m + 1), p[i]);
    if (b[i] != b[i + 1]) rec(b[i]), ed[b[i]] = a[p[i]];
  }
  cin >> Q;
  while (Q--) {
    int l, r, x, t = 1;
    scanf("%d%d%d", &l, &r, &x);
    x = lower_bound(lis + 1, lis + tp + 1, x ^ ans) - lis;
    while (t < cnt && ed[t] >= x) t++;
    for (int i = 1; i <= cnt; i++)
      w[i] = f[t - 1][i], v[i] = st[t - 1][i], la[i] = 0;
    for (int i = bl[t]; i <= br[t] && a[p[i]] >= x; i++) {
      int L = max(1, p[i] - m + 1), R = p[i], A = b[L], B = b[R];
      if (A == B)
        w[A] = pre[i];
      else {
        w[A] = pre[i];
        w[B] = suf[i];
        la[A + 1]++;
        la[B]--;
      }
      if (L <= bl[b[l]] && bl[b[l]] <= R) v[b[l]]++;
      if (b[l] != b[r] && L <= bl[b[r]] && bl[b[r]] <= R) v[b[r]]++;
    }
    for (int i = 1; i <= cnt; i++) la[i] += la[i - 1];
    int A = b[l], B = b[r];
    if (A == B)
      ans = qry(l, r, A, x);
    else {
      ans = max(qry(l, br[A], A, x), qry(bl[B], r, B, x));
      for (int i = A + 1; i < B; i++) ans = max(ans, la[i] + w[i]);
    }
    ans = m - ans;
    printf("%d\n", ans);
  }
  return 0;
}
