#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 333;
const long long linf = 1e18 + 333;
const int N = 2e5 + 5;
const int SIZE = 8000000;
class tree {
 public:
  unsigned long long min : 18, l : 23, r : 23;
  tree() { min = l = r = 0; }
};
int n, m, q, cnt, x1, x2, t[N];
pair<int, int> a[N];
tree p[SIZE];
int insert(int t, int l, int r) {
  if (l == r) return t + 1;
  int x = ++cnt;
  if (x1 <= l and r <= x2) {
    p[x] = p[t];
    p[x].min++;
    return x;
  }
  int m = l + r >> 1;
  p[x] = p[t];
  int add = p[x].min - min((l == m ? p[x].l : p[p[x].l].min),
                           (m + 1 == r ? p[x].r : p[p[x].r].min));
  if (x1 <= m) p[x].l = insert(p[t].l, l, m);
  if (x2 > m) p[x].r = insert(p[t].r, m + 1, r);
  p[x].min = min((l == m ? p[x].l : p[p[x].l].min),
                 (m + 1 == r ? p[x].r : p[p[x].r].min)) +
             add;
  return x;
}
int query(int x, int l, int r) {
  if (x2 < l or r < x1) return inf;
  if (l == r) return x;
  if (x1 <= l and r <= x2) return p[x].min;
  int m = l + r >> 1;
  int add = p[x].min - min((l == m ? p[x].l : p[p[x].l].min),
                           (m + 1 == r ? p[x].r : p[p[x].r].min));
  return min(query(p[x].l, l, m), query(p[x].r, m + 1, r)) + add;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i].first);
    a[i].second = i;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    x1 = max(1, a[i].second - m + 1);
    x2 = min(a[i].second, n - m + 1);
    t[i] = insert(t[i - 1], 1, n - m + 1);
  }
  scanf("%d", &q);
  int ans = 0;
  for (int qq = 0; qq < q; qq++) {
    int x;
    scanf("%d %d %d", &x1, &x2, &x);
    x = lower_bound(a + 1, a + n + 1, pair<int, int>(x ^ ans, 0)) - a - 1;
    ans = query(t[x], 1, n - m + 1);
    printf("%d\n", ans);
  }
  return 0;
}
