#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
constexpr long long mod = 1000000007;
const long long INF = mod * mod;
const double eps = 1e-12;
const double pi = acosl(-1.0);
long long mod_pow(long long x, long long n, long long m = mod) {
  if (n < 0) {
    long long res = mod_pow(x, -n, m);
    return mod_pow(res, m - 2, m);
  }
  if (abs(x) >= m) x %= m;
  if (x < 0) x += m;
  long long res = 1;
  while (n) {
    if (n & 1) res = res * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return res;
}
struct modint {
  long long n;
  modint() : n(0) { ; }
  modint(long long m) : n(m) {
    if (n >= mod)
      n %= mod;
    else if (n < 0)
      n = (n % mod + mod) % mod;
  }
  operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) {
  a.n += b.n;
  if (a.n >= mod) a.n -= mod;
  return a;
}
modint operator-=(modint& a, modint b) {
  a.n -= b.n;
  if (a.n < 0) a.n += mod;
  return a;
}
modint operator*=(modint& a, modint b) {
  a.n = ((long long)a.n * b.n) % mod;
  return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, long long n) {
  if (n == 0) return modint(1);
  modint res = (a * a) ^ (n / 2);
  if (n % 2) res = res * a;
  return res;
}
long long inv(long long a, long long p) {
  return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) {
  a = a / b;
  return a;
}
const int max_n = 1 << 10;
modint fact[max_n], factinv[max_n];
void init_f() {
  fact[0] = modint(1);
  for (int i = 0; i < max_n - 1; i++) {
    fact[i + 1] = fact[i] * modint(i + 1);
  }
  factinv[max_n - 1] = modint(1) / fact[max_n - 1];
  for (int i = max_n - 2; i >= 0; i--) {
    factinv[i] = factinv[i + 1] * modint(i + 1);
  }
}
modint comb(int a, int b) {
  if (a < 0 || b < 0 || a < b) return 0;
  return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
  if (a < 0 || b < 0 || a < b) return 0;
  return fact[a] * factinv[a - b];
}
const int b = 450;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<int> l(n), r(n);
  vector<int> xs;
  vector<pair<int, int>> vs;
  for (int i = 0; i < n; i++) {
    vs.push_back({a[i], i});
  }
  sort((vs).begin(), (vs).end());
  for (int i = 0; i < vs.size(); i++) {
    xs.push_back(vs[i].first);
    int le = vs[i].second - m + 1;
    int ri = vs[i].second;
    le = max(le, 0);
    ri = min(ri, n - m);
    l[i] = le, r[i] = ri;
  }
  int q;
  cin >> q;
  vector<int> ql(q), qr(q), x(q);
  for (int i = 0; i < q; i++) {
    cin >> ql[i] >> qr[i] >> x[i];
    ql[i]--;
    qr[i]--;
  }
  int d = (n + b - 1) / b;
  int dq = (q + b - 1) / b;
  vector<vector<int>> ads;
  vector<vector<int>> vals;
  vector<vector<pair<int, int>>> updval(n);
  vector<int> ad(d);
  vector<int> val(d);
  ads.push_back(ad);
  vals.push_back(val);
  vector<int> cur(n);
  for (int i = 0; i < n; i++) {
    int bl = l[i] / b;
    int br = r[i] / b;
    if (bl == br) {
      for (int j = l[i]; j <= r[i]; j++) {
        cur[j]++;
      }
      val[bl] = mod;
      for (int j = bl * b; j < min(bl * b + b, n); j++) {
        val[bl] = min(val[bl], cur[j]);
      }
      updval[i].push_back({bl, val[bl]});
    } else {
      for (int j = bl + 1; j < br; j++) {
        ad[j]++;
      }
      for (int j = l[i]; j < min(n, bl * b + b); j++) {
        cur[j]++;
      }
      val[bl] = mod;
      for (int j = bl * b; j < min(n, bl * b + b); j++) {
        val[bl] = min(val[bl], cur[j]);
      }
      updval[i].push_back({bl, val[bl]});
      for (int j = br * b; j <= r[i]; j++) {
        cur[j]++;
      }
      val[br] = mod;
      for (int j = br * b; j < min(n, br * b + b); j++) {
        val[br] = min(val[br], cur[j]);
      }
      updval[i].push_back({br, val[br]});
    }
    if ((i + 1) % b == 0) {
      ads.push_back(ad);
      vals.push_back(val);
    }
  }
  vector<vector<int>> adnode(n), decnode(n + 1);
  for (int i = 0; i < n; i++) {
    int rl = l[i] % b;
    adnode[l[i]].push_back(i);
    decnode[r[i] + 1].push_back(i);
  }
  vector<vector<int>> levals;
  vector<int> leval(d);
  levals.push_back(leval);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < d; j++) {
      if (l[i] <= j * b && j * b <= r[i]) leval[j]++;
    }
    if ((i + 1) % b == 0) {
      levals.push_back(leval);
    }
  }
  vector<int> ans(q, mod);
  vector<int> imos(d + 1);
  vector<int> memo(b);
  int las = 0;
  for (int id = 0; id < q; id++) {
    x[id] ^= las;
    int num = lower_bound((xs).begin(), (xs).end(), x[id]) - xs.begin();
    int bid = num / b;
    for (int j = 0; j < d; j++) {
      ad[j] = ads[bid][j];
      val[j] = vals[bid][j];
    }
    fill((imos).begin(), (imos).end(), 0);
    for (int i = bid * b; i < num; i++) {
      for (pair<int, int> p : updval[i]) {
        val[p.first] = p.second;
      }
      int bl = l[i] / b, br = r[i] / b;
      if (bl != br) {
        imos[bl + 1]++;
        imos[br]--;
      }
    }
    for (int j = 0; j < d; j++) {
      imos[j + 1] += imos[j];
      ad[j] += imos[j];
    }
    int qbl = ql[id] / b;
    int qbr = qr[id] / b;
    if (qbl == qbr) {
      fill((memo).begin(), (memo).end(), 0);
      memo[0] = levals[bid][qbl];
      for (int i = bid * b; i < num; i++)
        if (l[i] <= qbl * b && qbl * b <= r[i]) memo[0]++;
      for (int i = 1; i < b; i++) {
        int loc = qbl * b + i;
        if (loc >= n) continue;
        memo[i] = memo[i - 1];
        memo[i] +=
            lower_bound((adnode[loc]).begin(), (adnode[loc]).end(), num) -
            adnode[loc].begin();
        memo[i] -=
            lower_bound((decnode[loc]).begin(), (decnode[loc]).end(), num) -
            decnode[loc].begin();
      }
      for (int j = ql[id] % b; j <= qr[id] % b; j++)
        ans[id] = min(ans[id], memo[j]);
    } else {
      for (int j = qbl + 1; j < qbr; j++) {
        ans[id] = min(ans[id], val[j] + ad[j]);
      }
      fill((memo).begin(), (memo).end(), 0);
      memo[0] = levals[bid][qbl];
      for (int i = bid * b; i < num; i++)
        if (l[i] <= qbl * b && qbl * b <= r[i]) memo[0]++;
      for (int i = 1; i < b; i++) {
        int loc = qbl * b + i;
        if (loc >= n) continue;
        memo[i] = memo[i - 1];
        memo[i] +=
            lower_bound((adnode[loc]).begin(), (adnode[loc]).end(), num) -
            adnode[loc].begin();
        memo[i] -=
            lower_bound((decnode[loc]).begin(), (decnode[loc]).end(), num) -
            decnode[loc].begin();
      }
      for (int j = ql[id] % b; j < b && qbl * b + j < n; j++)
        ans[id] = min(ans[id], memo[j]);
      fill((memo).begin(), (memo).end(), 0);
      memo[0] = levals[bid][qbr];
      for (int i = bid * b; i < num; i++)
        if (l[i] <= qbr * b && qbr * b <= r[i]) memo[0]++;
      for (int i = 1; i < b; i++) {
        int loc = qbr * b + i;
        if (loc >= n) continue;
        memo[i] = memo[i - 1];
        memo[i] +=
            lower_bound((adnode[loc]).begin(), (adnode[loc]).end(), num) -
            adnode[loc].begin();
        memo[i] -=
            lower_bound((decnode[loc]).begin(), (decnode[loc]).end(), num) -
            decnode[loc].begin();
      }
      for (int j = 0; j <= qr[id] % b; j++) ans[id] = min(ans[id], memo[j]);
    }
    las = ans[id];
  }
  for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
