#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 5;
const int mult = 9;
const int blsize = (1 << mult);
const int INF = (1 << 30);
pair<int, int> music[maxn];
int Maxval[blsize][blsize], front[blsize][blsize], update[blsize][2 * blsize];
int arr[maxn], curval[maxn];
int curMax[blsize], curtag[blsize];
int n, m;
inline bool is_first(int idx) { return (idx & (blsize - 1)) == 0; }
inline int bl_idx(int idx) { return (idx >> mult) + (idx >= n ? 1 : 0); }
inline int first_idx(int blid) { return blid << mult; }
inline int last_idx(int blid) {
  return min(n - 1, first_idx(blid) + blsize - 1);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", arr + i);
    music[i].first = -arr[i];
    music[i].second = i;
  }
  sort(music, music + n);
  int totbl = (n + blsize - 1) / blsize;
  for (int i = 0; i < n; i++) {
    if (is_first(i)) {
      int curtime = bl_idx(i);
      for (int j = 0; j < n; j++) curval[j] += curtag[bl_idx(j)];
      for (int j = 0; j < totbl; j++) {
        curMax[j] += curtag[j];
        Maxval[curtime][j] = curMax[j];
        front[curtime][j] = curval[first_idx(j)];
        curtag[j] = 0;
      }
    }
    int R = music[i].second, L = max(0, R - m + 1), sizeb = 0,
        tochange[] = {totbl, totbl};
    for (int j = L; j <= R; j++) {
      int curbl = bl_idx(j);
      if (is_first(j) && R >= last_idx(curbl))
        curtag[curbl]++, j = last_idx(curbl);
      else {
        curval[j]++;
        curMax[curbl] = max(curMax[curbl], curval[j]);
        if (sizeb == 0 || tochange[sizeb - 1] != curbl)
          tochange[sizeb++] = curbl;
      }
    }
    int curtime = bl_idx(i), idx = i - first_idx(curtime);
    for (int j = 0; j < 2; j++)
      update[curtime][2 * idx + j] =
          (curMax[tochange[j]] << mult) + tochange[j];
  }
  int query, ans = 0;
  scanf("%d", &query);
  while (query--) {
    int qL, qR, qv;
    scanf("%d%d%d", &qL, &qR, &qv);
    qL--, qR--, qv ^= ans;
    int idx = upper_bound(music, music + n, make_pair(-qv, INF)) - music - 1;
    if (idx < 0) {
      printf("%d\n", ans = m);
      continue;
    }
    int curtime = bl_idx(idx);
    for (int i = 0; i < totbl; i++)
      curMax[i] = Maxval[curtime][i], curtag[i] = 0;
    int st = first_idx(curtime);
    for (int i = st; i <= idx; i++) {
      int R = music[i].second, L = max(0, R - m + 1);
      if (L != first_idx(bl_idx(L))) L = last_idx(bl_idx(L)) + 1;
      if (R == last_idx(bl_idx(R))) R++;
      if (bl_idx(L) <= bl_idx(R)) curtag[bl_idx(L)]++, curtag[bl_idx(R)]--;
      for (int j = 0; j < 2; j++) {
        int xx = update[curtime][2 * (i - st) + j];
        curMax[xx & (blsize - 1)] = xx >> mult;
      }
    }
    for (int i = 1; i < totbl; i++) curtag[i] += curtag[i - 1];
    ans = 0;
    int val = -1;
    for (int i = qL; i <= qR; i++) {
      int curbl = bl_idx(i);
      if (is_first(i) && qR >= last_idx(curbl))
        ans = max(ans, curMax[curbl] + curtag[curbl]), i = last_idx(curbl),
        val = -1;
      else {
        if (val == -1) {
          val = front[curtime][curbl];
          int ftp = first_idx(curbl);
          for (int j = st; j <= idx; j++)
            if (ftp <= music[j].second &&
                ftp >= max(0, music[j].second - m + 1))
              val++;
          while (ftp != i) {
            val -= (arr[ftp] >= qv), val += (arr[ftp + m] >= qv);
            ftp++;
          }
        }
        ans = max(ans, val);
        val -= (arr[i] >= qv);
        val += (arr[i + m] >= qv);
      }
    }
    printf("%d\n", ans = m - ans);
  }
}
