#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  x = 0;
  int ch = getchar();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') sign = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  x = x * sign;
}
const int INF = 0x66CCFF;
const int maxn = 200010;
const int maxm = 480;
int A[maxn];
pair<int, int> B[maxn];
int n, len;
int m, k;
int Pre[maxn];
int Add[maxm];
int Max[maxm], BMax[maxm][maxm];
int V[maxn], BVal[maxm][maxm];
int ResMax[maxm][maxm << 1];
inline void Pretreat() {
  static int V[maxn];
  m = (int)sqrt(n + 0.5);
  k = n / m + (n % m != 0);
  register int i, j;
  for (i = 1; i <= n; i++) Pre[i] = (i - 1) / m + 1;
  for (i = 1; i <= n; i++) {
    if (i == ((Pre[i] - 1) * m + 1)) {
      for (j = 1; j <= n; j++) V[j] += Add[Pre[j]];
      for (j = 1; j <= k; j++) {
        Max[j] += Add[j];
        BMax[Pre[i]][j] = Max[j];
        BVal[Pre[i]][j] = V[(j - 1) * m + 1];
        Add[j] = 0;
      }
    }
    int r = B[i].second, l = (1 > r - len + 1 ? 1 : r - len + 1);
    int L = ((Pre[l] * m) < r ? (Pre[l] * m) : r),
        R = (L + 1 > ((Pre[r] - 1) * m + 1) ? L + 1 : ((Pre[r] - 1) * m + 1));
    int &ML = Max[Pre[l]], &MR = Max[Pre[r]];
    for (j = l; j <= L; j++) {
      V[j]++;
      if (ML < V[j]) ML = V[j];
    }
    for (j = R; j <= r; j++) {
      V[j]++;
      if (MR < V[j]) MR = V[j];
    }
    for (j = Pre[l] + 1; j < Pre[r]; j++) Add[j]++;
    int id = i - ((Pre[i] - 1) * m + 1);
    for (j = 0; j < 2; j++) {
      int Now = Pre[j ? r : l];
      ResMax[Pre[i]][id + j * m] = (Max[Now]) * k + Now - 1;
    }
  }
}
inline int find(int v) {
  int L = 1, R = n;
  while (L < R) {
    int mid = (L + R) >> 1;
    if (B[mid].first <= v)
      L = mid + 1;
    else
      R = mid;
  }
  return B[L].first <= v ? L + 1 : L;
}
inline int Query(int l, int r, int ST, int pos, int qv) {
  register int i, cnt;
  int STVal = ((Pre[l] - 1) * m + 1);
  cnt = BVal[Pre[ST]][Pre[l]];
  for (i = ST; i <= pos; i++)
    if (STVal <= B[i].second &&
        STVal >= (1 > B[i].second - len + 1 ? 1 : B[i].second - len + 1))
      cnt++;
  for (; STVal < l; STVal++) {
    cnt -= (A[STVal] >= qv);
    cnt += (A[STVal + len] >= qv);
  }
  int Ans = 0;
  for (i = l; i <= r; i++) {
    if (cnt > Ans) Ans = cnt;
    cnt -= (A[i] >= qv);
    cnt += (A[i + len] >= qv);
  }
  return Ans;
}
int main() {
  read(n), read(len);
  for (int i = 1; i <= n; i++) read(A[i]), B[i] = make_pair(-A[i], i);
  sort(B + 1, B + 1 + n);
  Pretreat();
  int q, Ans = 0;
  read(q);
  while (q--) {
    int ql, qr, qv;
    read(ql), read(qr), read(qv);
    qv ^= Ans;
    if (-qv >= B[n].first)
      Ans = len;
    else if (-qv < B[1].first)
      Ans = 0;
    else {
      register int i;
      int pos = find(-qv) - 1;
      int BLC = Pre[pos];
      for (i = 1; i <= k; i++) Max[i] = BMax[BLC][i], Add[i] = 0;
      int ST = ((Pre[pos] - 1) * m + 1);
      for (i = ST; i <= pos; i++) {
        int r = B[i].second, l = (1 > r - len + 1 ? 1 : r - len + 1);
        if (Pre[l] != Pre[r]) Add[Pre[l] + 1]++, Add[Pre[r]]--;
        for (int j = 0; j < 2; j++)
          Max[ResMax[BLC][i - ST + j * m] % k + 1] =
              ResMax[BLC][i - ST + j * m] / k;
      }
      for (i = 1; i <= k; i++) Add[i] += Add[i - 1];
      Ans = 0;
      int L = ((Pre[ql] * m) < qr ? (Pre[ql] * m) : qr),
          R = (L + 1 > ((Pre[qr] - 1) * m + 1) ? L + 1
                                               : ((Pre[qr] - 1) * m + 1));
      for (i = Pre[ql] + 1; i < Pre[qr]; i++)
        Ans = (Ans > Max[i] + Add[i] ? Ans : Max[i] + Add[i]);
      Ans = (Ans > Query(ql, L, ST, pos, qv) ? Ans : Query(ql, L, ST, pos, qv));
      Ans = (Ans > Query(R, qr, ST, pos, qv) ? Ans : Query(R, qr, ST, pos, qv));
    }
    Ans = len - Ans;
    printf("%d\n", Ans);
  }
  return 0;
}
