#include <bits/stdc++.h>
const int N = 2e5 + 100;
const int SQRT = 512;
int getBlock(int idx) { return idx / SQRT; }
int getTop(int l) { return ((l + SQRT - 1) / SQRT) * SQRT; }
int n, m;
std::vector<int> a;
void read() {
  scanf("%d%d", &n, &m);
  a.resize(n);
  for (int& x : a) scanf("%d", &x);
}
struct T2 {
  int val;
  int new_sum;
  int new_max;
};
struct BlockBase {
  int task1_sum;
  int task2_sum;
  int task2_max;
};
struct Block {
  BlockBase base;
  std::vector<int> task1_changes;
  std::vector<T2> task2;
  BlockBase get(int val) const {
    auto res = base;
    for (auto x1 : task1_changes) {
      if (x1 <= val) break;
      res.task1_sum++;
    }
    for (auto x1 : task2) {
      {};
      {};
      if (x1.val <= val) break;
      {};
      {};
      {};
      {};
      res.task2_sum = x1.new_sum;
      res.task2_max = x1.new_max;
    }
    return res;
  }
};
Block d[SQRT][SQRT];
using Pair = std::pair<int, int>;
std::vector<int> times;
std::vector<Pair> tidxs;
void task1_prep() {
  for (int i = 0; i < n; ++i) {
    Pair p = tidxs[i];
    if (i != 0 && getBlock(i - 1) != getBlock(i)) {
      int tb = getBlock(i);
      for (int j = 0; j < SQRT; ++j) d[j][tb].base = d[j][tb - 1].get(p.first);
    }
    int idx = p.second;
    int pos = getBlock(idx);
    int tb = getBlock(i);
    d[pos][tb].task1_changes.push_back(p.first);
  }
}
int curState[N] = {};
T2 stupidGetBlock(int pos) {
  T2 r;
  r.new_max = 0;
  int res = 0;
  for (int i = pos * SQRT; i < (pos + 1) * SQRT; ++i) {
    res += curState[i];
    {};
    {};
    r.new_max = std::max(r.new_max, res);
  }
  r.new_sum = res;
  return r;
}
void task2_prep() {
  for (int i = 0; i < n; ++i) {
    Pair p = tidxs[i];
    if (i != 0 && getBlock(i - 1) != getBlock(i)) {
      {};
      int tb = getBlock(i);
      for (int j = 0; j < SQRT; ++j) {
        d[j][tb].base = d[j][tb - 1].get(p.first - 1);
        if (j == 2) {
          {};
          {};
        }
      }
    }
    int idx = p.second;
    int pos = getBlock(idx);
    int tb = getBlock(i);
    {
      curState[idx]--;
      T2 x = stupidGetBlock(pos);
      x.val = p.first;
      d[pos][tb].task2.push_back(x);
      if (idx == 10) {
        {};
        {};
      }
    }
    if (idx - m >= 0) {
      curState[idx - m]++;
      int pp = getBlock(idx - m);
      T2 x = stupidGetBlock(pp);
      x.val = p.first;
      d[pp][tb].task2.push_back(x);
    }
  }
}
void prep() {
  times = a;
  std::sort(times.rbegin(), times.rend());
  for (int i = 0; i < n; ++i) tidxs.push_back(Pair(a[i], i));
  std::sort(tidxs.rbegin(), tidxs.rend());
  task1_prep();
  task2_prep();
}
int f(int v) {
  if (times[0] <= v) return 0;
  int j = 0;
  for (int i = 0; i < n; i += SQRT, ++j)
    if (times[i] <= v) break;
  return j - 1;
}
int task1_get(int l, int r, int v, int fv) {
  int res = 0;
  int L = getBlock(l);
  int R = getBlock(r);
  if (L == R) {
    for (int i = l; i <= r; ++i) res += (a[i] > v);
    return res;
  }
  for (int i = l; i < (L + 1) * SQRT; ++i) res += (a[i] > v);
  for (int i = R * SQRT; i <= r; ++i) res += (a[i] > v);
  for (int b = L + 1; b < R; ++b) res += d[b][fv].get(v).task1_sum;
  return res;
}
void shift1(int pos, int v, int& res, int& cur) {
  if (a[pos] > v) --cur;
  if (a[pos + m] > v) ++cur;
  res = std::max(res, cur);
}
void shift2(int pos, int v, int fv, int& res, int& cur) {
  {};
  int cc = cur;
  auto r = d[pos][fv].get(v);
  {};
  {};
  cur += r.task2_sum;
  cc += r.task2_max;
  res = std::max(res, cc);
  res = std::max(res, cur);
}
int task2_get(int l, int r, int v) {
  int res;
  int fv = f(v);
  int cur = task1_get(l, l + m - 1, v, fv);
  {};
  {};
  res = cur;
  int L = getBlock(l);
  int R = getBlock(r);
  if (L == R) {
    for (int i = l; i < r; ++i) shift1(i, v, res, cur);
    return res;
  }
  {};
  for (int i = l; i < (L + 1) * SQRT; ++i) shift1(i, v, res, cur);
  {};
  {};
  {};
  for (int b = L + 1; b < R; ++b) {
    shift2(b, v, fv, res, cur);
    {};
    {};
    {};
  }
  {};
  for (int i = R * SQRT; i < r; ++i) shift1(i, v, res, cur);
  {};
  return res;
}
int main() {
  read();
  prep();
  int s;
  scanf("%d", &s);
  int ans = 0;
  for (int i = 0; i < s; ++i) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    --l;
    --r;
    int q = x ^ ans;
    int gg = task2_get(l, r, q - 1);
    ans = m - gg;
    printf("%d\n", ans);
  }
}
