Please note that the memory limit differs from the standard.

You really love to listen to music. During the each of next s days you will listen to exactly m songs from the playlist that consists of exactly n songs. Let's number the songs from the playlist with numbers from 1 to n, inclusive. The quality of song number i is ai.

On the i-th day you choose some integer v (li ≤ v ≤ ri) and listen to songs number v, v + 1, ..., v + m - 1. On the i-th day listening to one song with quality less than qi increases your displeasure by exactly one.

Determine what minimum displeasure you can get on each of the s next days. 

Input

The first line contains two positive integers n, m (1 ≤ m ≤ n ≤ 2·105). The second line contains n positive integers a1, a2, ..., an (0 ≤ ai < 230) — the description of songs from the playlist. 

The next line contains a single number s (1 ≤ s ≤ 2·105) — the number of days that you consider.

The next s lines contain three integers each li, ri, xi (1 ≤ li ≤ ri ≤ n - m + 1; 0 ≤ xi < 230) — the description of the parameters for the i-th day. In order to calculate value qi, you need to use formula: <image>, where ansi is the answer to the problem for day i. Assume that ans0 = 0. 

Output

Print exactly s integers ans1, ans2, ..., anss, where ansi is the minimum displeasure that you can get on day i.

Examples

Input

5 3
1 2 1 2 3
5
1 1 2
1 3 2
1 3 3
1 3 5
1 3 1


Output

2
0
2
3
1
#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;
}
