#include <bits/stdc++.h>
using namespace std;
const int N = 200002;
const int L = 1000;
int LAZY[4 * L];
int MIN[4 * L];
void push(int x) {
  LAZY[x + x] += LAZY[x];
  LAZY[x + x + 1] += LAZY[x];
}
void gather(int x) {
  MIN[x] += LAZY[x];
  LAZY[x] = 0;
}
int findRank(int* C, int num) {
  int low = 0;
  int high = L;
  while (low < high) {
    int mid = (low + high) >> 1;
    if (C[mid] < num)
      low = mid + 1;
    else
      high = mid;
  }
  return L - high;
}
int query() { return MIN[1]; }
void update(int x, int l, int r, int ll, int rr) {
  if (l >= ll && rr >= r) ++LAZY[x];
  if (l < r) push(x);
  gather(x);
  if (l >= ll && rr >= r) return;
  if (l < r && l <= rr && ll <= r) {
    int y = (l + r) >> 1;
    update(x + x, l, y, ll, rr);
    update(x + x + 1, y + 1, r, ll, rr);
    MIN[x] = MIN[x + x] < MIN[x + x + 1] ? MIN[x + x] : MIN[x + x + 1];
  }
}
struct Move {
  int M[2 * L + 1];
  int C[2 * L];
  void solve() {
    memset(MIN, 0, sizeof(MIN));
    memset(LAZY, 0, sizeof(LAZY));
    pair<int, int> D[2 * L];
    for (int i = 0; i < 2 * L; ++i) D[i] = {C[i], i};
    sort(D, D + 2 * L);
    for (int i = 2 * L - 1; i >= 0;) {
      int j = i;
      while (j >= 0 && D[j].first == D[i].first) {
        update(1, 0, L, 0 < D[j].second + 1 - L ? D[j].second + 1 - L : 0,
               L < D[j].second ? L : D[j].second);
        --j;
      }
      M[2 * L - j - 1] = query();
      i = j;
    }
    M[0] = 0;
    sort(C, C + L);
    sort(C + L, C + L + L);
  }
  void set(int* A, int* B) {
    memcpy(C + L, A, sizeof(C) / 2);
    memcpy(C, B, sizeof(C) / 2);
  }
};
void run() {
  int B[N / L + 1][L];
  int A[N];
  Move MV[N / L + 1];
  int n, m, s;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
    B[i / L][i % L] = A[i];
  }
  for (int i = 0; i < N / L; ++i) {
    int bgin = i * L;
    sort(B[i], B[i] + L);
    if (bgin + m + L <= n) {
      MV[i].set(A + bgin, A + bgin + m);
      MV[i].solve();
    }
  }
  cin >> s;
  int ans = m;
  for (int qr = 0; qr < s; ++qr) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x ^= m - ans;
    --l;
    --r;
    int val = 0;
    int p = l;
    int q = l + m - 1;
    int bgin = (p + L - 1) / L * L;
    while (p <= q && p < bgin) {
      val += A[p] >= x;
      ++p;
    }
    if (p <= q) {
      int j = p / L;
      while (p + L - 1 <= q) {
        val += findRank(B[j], x);
        p += L;
        ++j;
      }
    }
    while (p <= q) {
      val += A[p] >= x;
      ++p;
    }
    ans = val;
    p = l;
    while (p < bgin && p < r) {
      val += A[p + m] >= x;
      val -= A[p] >= x;
      ans = ans < val ? val : ans;
      ++p;
    }
    if (p < r) {
      int j = p / L;
      while (p + L <= r) {
        int u = findRank(MV[j].C, x);
        int ttl = val + u;
        int v = findRank(MV[j].C + L, x);
        val = ttl - v;
        ans = ans < ttl - MV[j].M[u + v] ? ttl - MV[j].M[u + v] : ans;
        p += L;
        ++j;
      }
    }
    while (p < r) {
      val += A[p + m] >= x;
      val -= A[p] >= x;
      ans = ans < val ? val : ans;
      ++p;
    }
    printf("%d\n", m - ans);
  }
}
int main() {
  run();
  return 0;
}
