Problem: 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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 400050, inf = 1e9;
int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9') q = ch == '-' ? 1 : q, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return q ? -x : x;
}
struct date {
  int x, s;
  bool operator<(const date &c) const { return s < c.s; }
} b[N];
struct Blo {
  int a[500], fr[500], laz[500];
} B[500];
int n, m, a[N], c[500][500], bl[N], L[N], R[N], w[N], mn[500], laz[500],
    fr[500], las, P;
int f[N][2];
int find(int s) {
  int l = 1, r = n, ans = 0, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (b[mid].s <= s)
      l = mid + 1, ans = mid;
    else
      r = mid - 1;
  }
  return ans;
}
void work(int l, int r, int x) {
  int ans = inf, p = find(x - 1);
  if (p == 0) {
    printf("%d\n", las = 0);
    return;
  }
  for (int i = 1; i <= P; ++i)
    laz[i] = 0, mn[i] = B[bl[p] - 1].a[i], fr[i] = B[bl[p] - 1].fr[i];
  for (int i = L[bl[p]]; i <= p; ++i) {
    int zl = max(1, b[i].x - m + 1), zr = b[i].x;
    if (bl[zl] != bl[zr]) ++laz[bl[zl] + 1], --laz[bl[zr]];
    mn[bl[zl]] = f[i][0], mn[bl[zr]] = f[i][1];
    if (zl == L[bl[zl]]) ++fr[bl[zl]];
    if (bl[zl] != bl[zr]) ++fr[bl[zr]];
  }
  for (int i = 1; i <= P; ++i) laz[i] += laz[i - 1];
  for (int i = 1; i <= P; ++i) laz[i] += B[bl[p] - 1].laz[i];
  int sum = fr[bl[l]] + laz[bl[l]];
  for (int i = L[bl[l]]; i <= R[bl[l]]; ++i) {
    if (i >= l && i <= r) ans = (ans < sum ? ans : sum);
    if (a[i] < x) --sum;
    if (a[i + m] < x) ++sum;
  }
  for (int i = bl[l] + 1; i < bl[r]; ++i)
    ans = (ans < laz[i] + mn[i] ? ans : laz[i] + mn[i]);
  sum = fr[bl[r]] + laz[bl[r]];
  for (int i = L[bl[r]]; i <= R[bl[r]]; ++i) {
    if (i >= l && i <= r) ans = (ans < sum ? ans : sum);
    if (a[i] < x) --sum;
    if (a[i + m] < x) ++sum;
  }
  las = ans;
  printf("%d\n", ans);
  return;
}
void rebuild(int x) {
  mn[x] = inf, fr[x] = w[L[x]];
  for (int i = L[x]; i <= R[x]; ++i) mn[x] = (mn[x] < w[i] ? mn[x] : w[i]);
  return;
}
int main() {
  n = gi(), m = gi();
  for (int i = 1; i <= n; ++i) b[i] = (date){i, a[i] = gi()};
  sort(b + 1, b + n + 1);
  int blo = sqrt(n);
  P = (n - 1) / blo + 1;
  for (int i = 1; i <= n; ++i) {
    bl[i] = (i - 1) / blo + 1, R[bl[i]] = i;
    if (!L[bl[i]]) L[bl[i]] = i;
  }
  for (int i = 1; i <= n; ++i) {
    int l = max(1, b[i].x - m + 1), r = b[i].x;
    if (bl[l] == bl[r]) {
      for (int j = l; j <= r; ++j) ++w[j];
      rebuild(bl[l]);
    } else {
      for (int j = bl[l] + 1; j < bl[r]; ++j) ++laz[j];
      for (int j = l; j <= R[bl[l]]; ++j) ++w[j];
      for (int j = L[bl[r]]; j <= r; ++j) ++w[j];
      rebuild(bl[l]), rebuild(bl[r]);
    }
    if (bl[i + 1] != bl[i])
      for (int j = 1; j <= P; ++j)
        B[bl[i]].a[j] = mn[j], B[bl[i]].fr[j] = fr[j], B[bl[i]].laz[j] = laz[j];
    f[i][0] = mn[bl[l]], f[i][1] = mn[bl[r]];
  }
  int Q = gi();
  for (int i = 1; i <= Q; ++i) {
    int l = gi(), r = gi(), x = gi();
    x ^= las, work(l, r, x);
  }
  return 0;
}