#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 200000, max0 = 300, max1 = 700, max2 = 700, maxabs = 1 << 30;
int n, m;
vector<int> X;
int a[maxn + 5];
vector<int> id[max1 + 5], Min[max1 + 5], cnt[max1 + 5], to_add[max1 + 5];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0), _end_ = (n); i < _end_; ++i)
    scanf("%d", a + i), X.push_back(a[i]);
  X.push_back(maxabs);
  sort((X).begin(), (X).end());
  X.resize(unique((X).begin(), (X).end()) - X.begin());
  int tot = 0;
  for (int i = (0), _end_ = ((int((X).size()))); i < _end_; ++i) {
    if (!(i % max0)) {
      static int sum[maxn + 5];
      static bool imp[maxn + 5];
      memset(sum, 0, sizeof sum);
      memset(imp, 0, sizeof imp);
      for (int j = (0), _end_ = (n); j < _end_; ++j) {
        if (!(j % max2)) imp[j] = 1;
        if (a[j] < X[i]) {
          --sum[j + 1];
          ++sum[max(0, j - m + 1)];
        } else if (a[j] >= X[i] &&
                   a[j] < X[min((int((X).size())) - 1, i + max0)]) {
          to_add[tot].push_back(j);
          imp[j + 1] = 1;
          imp[max(0, j - m + 1)] = 1;
        }
      }
      int now = -1, pre = 0;
      for (int j = (0), _end_ = (n); j < _end_; ++j) {
        pre += sum[j];
        if (imp[j]) {
          ++now;
          id[tot].push_back(j);
          Min[tot].push_back(pre);
          cnt[tot].push_back(pre);
        }
        chkmin(Min[tot][now], pre);
      }
      id[tot].push_back(n);
      ++tot;
    }
  }
  int qn, lastans = 0;
  scanf("%d", &qn);
  static int lst[maxn + 5], sum[maxn + 5];
  memset(lst, -1, sizeof lst);
  for (int i = (0), _end_ = (qn); i < _end_; ++i) {
    int l, r, z;
    scanf("%d%d%d", &l, &r, &z), --l;
    z ^= lastans;
    int tmp = lower_bound((X).begin(), (X).end(), z) - X.begin();
    int p = tmp / max0;
    for (auto first : to_add[p])
      if (a[first] < z) {
        if (lst[first + 1] != i) sum[first + 1] = 0, lst[first + 1] = i;
        --sum[first + 1];
        if (lst[max(0, first + 1 - m)] != i)
          sum[max(0, first + 1 - m)] = 0, lst[max(0, first + 1 - m)] = i;
        ++sum[max(0, first + 1 - m)];
      }
    int pre = 0;
    int ans = oo;
    for (int j = (0), _end_ = ((int((id[p]).size())) - 1); j < _end_; ++j) {
      int u = id[p][j], v = id[p][j + 1];
      if (lst[id[p][j]] == i) pre += sum[id[p][j]];
      if (u >= l && v < r)
        chkmin(ans, pre + Min[p][j]);
      else {
        if (v <= l || u >= r) continue;
        int now = cnt[p][j] + pre;
        for (int k = (u), _end_ = (v); k < _end_; ++k) {
          if (k >= l && k < r) chkmin(ans, now);
          if (a[k] < X[p * max0]) --now;
          if (k + m < n && a[k + m] < X[p * max0]) ++now;
        }
      }
    }
    printf("%d\n", ans);
    lastans = ans;
  }
  return 0;
}
