#include <bits/stdc++.h>
using namespace std;
const int N = 400050, inf = 1e9;
int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9') q = ch == '-' ? 1 : q, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return q ? -x : x;
}
struct date {
  int x, s;
  bool operator<(const date &c) const { return s < c.s; }
} b[N];
struct Blo {
  int a[500], fr[500], laz[500];
} B[500];
int n, m, a[N], c[500][500], bl[N], L[N], R[N], w[N], mn[500], laz[500],
    fr[500], las, P;
int f[N][2];
int find(int s) {
  int l = 1, r = n, ans = 0, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (b[mid].s <= s)
      l = mid + 1, ans = mid;
    else
      r = mid - 1;
  }
  return ans;
}
void work(int l, int r, int x) {
  int ans = inf, p = find(x - 1);
  if (p == 0) {
    printf("%d\n", las = 0);
    return;
  }
  for (int i = 1; i <= P; ++i)
    laz[i] = 0, mn[i] = B[bl[p] - 1].a[i], fr[i] = B[bl[p] - 1].fr[i];
  for (int i = L[bl[p]]; i <= p; ++i) {
    int zl = max(1, b[i].x - m + 1), zr = b[i].x;
    if (bl[zl] != bl[zr]) ++laz[bl[zl] + 1], --laz[bl[zr]];
    mn[bl[zl]] = f[i][0], mn[bl[zr]] = f[i][1];
    if (zl == L[bl[zl]]) ++fr[bl[zl]];
    if (bl[zl] != bl[zr]) ++fr[bl[zr]];
  }
  for (int i = 1; i <= P; ++i) laz[i] += laz[i - 1];
  for (int i = 1; i <= P; ++i) laz[i] += B[bl[p] - 1].laz[i];
  int sum = fr[bl[l]] + laz[bl[l]];
  for (int i = L[bl[l]]; i <= R[bl[l]]; ++i) {
    if (i >= l && i <= r) ans = (ans < sum ? ans : sum);
    if (a[i] < x) --sum;
    if (a[i + m] < x) ++sum;
  }
  for (int i = bl[l] + 1; i < bl[r]; ++i)
    ans = (ans < laz[i] + mn[i] ? ans : laz[i] + mn[i]);
  sum = fr[bl[r]] + laz[bl[r]];
  for (int i = L[bl[r]]; i <= R[bl[r]]; ++i) {
    if (i >= l && i <= r) ans = (ans < sum ? ans : sum);
    if (a[i] < x) --sum;
    if (a[i + m] < x) ++sum;
  }
  las = ans;
  printf("%d\n", ans);
  return;
}
void rebuild(int x) {
  mn[x] = inf, fr[x] = w[L[x]];
  for (int i = L[x]; i <= R[x]; ++i) mn[x] = (mn[x] < w[i] ? mn[x] : w[i]);
  return;
}
int main() {
  n = gi(), m = gi();
  for (int i = 1; i <= n; ++i) b[i] = (date){i, a[i] = gi()};
  sort(b + 1, b + n + 1);
  int blo = sqrt(n);
  P = (n - 1) / blo + 1;
  for (int i = 1; i <= n; ++i) {
    bl[i] = (i - 1) / blo + 1, R[bl[i]] = i;
    if (!L[bl[i]]) L[bl[i]] = i;
  }
  for (int i = 1; i <= n; ++i) {
    int l = max(1, b[i].x - m + 1), r = b[i].x;
    if (bl[l] == bl[r]) {
      for (int j = l; j <= r; ++j) ++w[j];
      rebuild(bl[l]);
    } else {
      for (int j = bl[l] + 1; j < bl[r]; ++j) ++laz[j];
      for (int j = l; j <= R[bl[l]]; ++j) ++w[j];
      for (int j = L[bl[r]]; j <= r; ++j) ++w[j];
      rebuild(bl[l]), rebuild(bl[r]);
    }
    if (bl[i + 1] != bl[i])
      for (int j = 1; j <= P; ++j)
        B[bl[i]].a[j] = mn[j], B[bl[i]].fr[j] = fr[j], B[bl[i]].laz[j] = laz[j];
    f[i][0] = mn[bl[l]], f[i][1] = mn[bl[r]];
  }
  int Q = gi();
  for (int i = 1; i <= Q; ++i) {
    int l = gi(), r = gi(), x = gi();
    x ^= las, work(l, r, x);
  }
  return 0;
}
