In Ann's favorite book shop are as many as n books on math and economics. Books are numbered from 1 to n. Each of them contains non-negative number of problems.

Today there is a sale: any subsegment of a segment from l to r can be bought at a fixed price. 

Ann decided that she wants to buy such non-empty subsegment that the sale operates on it and the number of math problems is greater than the number of economics problems exactly by k. Note that k may be positive, negative or zero.

Unfortunately, Ann is not sure on which segment the sale operates, but she has q assumptions. For each of them she wants to know the number of options to buy a subsegment satisfying the condition (because the time she spends on choosing depends on that).

Currently Ann is too busy solving other problems, she asks you for help. For each her assumption determine the number of subsegments of the given segment such that the number of math problems is greaten than the number of economics problems on that subsegment exactly by k.

Input

The first line contains two integers n and k (1 ≤ n ≤ 100 000,  - 109 ≤ k ≤ 109) — the number of books and the needed difference between the number of math problems and the number of economics problems.

The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 2), where ti is 1 if the i-th book is on math or 2 if the i-th is on economics.

The third line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109), where ai is the number of problems in the i-th book.

The fourth line contains a single integer q (1 ≤ q ≤ 100 000) — the number of assumptions.

Each of the next q lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n) describing the i-th Ann's assumption.

Output

Print q lines, in the i-th of them print the number of subsegments for the i-th Ann's assumption.

Examples

Input

4 1
1 1 1 2
1 1 1 1
4
1 2
1 3
1 4
3 4


Output

2
3
4
1


Input

4 0
1 2 1 2
0 0 0 0
1
1 4


Output

10

Note

In the first sample Ann can buy subsegments [1;1], [2;2], [3;3], [2;4] if they fall into the sales segment, because the number of math problems is greater by 1 on them that the number of economics problems. So we should count for each assumption the number of these subsegments that are subsegments of the given segment.

Segments [1;1] and [2;2] are subsegments of [1;2].

Segments [1;1], [2;2] and [3;3] are subsegments of [1;3].

Segments [1;1], [2;2], [3;3], [2;4] are subsegments of [1;4].

Segment [3;3] is subsegment of [3;4].
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename S, typename T>
inline bool Min(S &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <typename S, typename T>
inline bool Max(S &a, const T &b) {
  return a < b ? a = b, true : false;
}
template <typename S, typename T>
inline void Adm(S &a, const T &b) {
  a = (a + b) % MOD;
  if (a < 0) a += MOD;
}
template <typename S, typename T>
inline void Mum(S &a, const T &b) {
  a = 1LL * a * b % MOD;
}
template <typename T>
inline bool IsPri(T x) {
  if (x < 2) return false;
  for (T i = 2; i * i <= x; ++i)
    if (x % i == 0) return false;
  return true;
}
template <typename T>
inline T Gcd(T a, T b) {
  while (b) {
    T t = b;
    b = a % b;
    a = t;
  }
  return a;
}
template <typename T>
inline int BitCnt(T x) {
  int cnt = 0;
  while (x) ++cnt, x &= x - 1;
  return cnt;
}
template <typename T>
inline T MDis(T x1, T y1, T x2, T y2) {
  return abs(x1 - x2) + abs(y1 - y2);
}
template <typename T>
inline T MDis(T x1, T y1, T z1, T x2, T y2, T z2) {
  return abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2);
}
inline double EDis(double x1, double y1, double x2, double y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
inline double EDis(double x1, double y1, double z1, double x2, double y2,
                   double z2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) +
              (z1 - z2) * (z1 - z2));
}
inline long long Pow(long long a, long long n) {
  long long t = 1;
  a %= MOD;
  while (n > 0) {
    if (n & 1) t = t * a % MOD;
    a = a * a % MOD, n >>= 1;
  }
  return t % MOD;
}
inline int read() {
  static char buf[1000000], *p1 = buf, *p2 = buf;
  register int x = false;
  register char ch =
      p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
          ? EOF
          : *p1++;
  ;
  register bool sgn = false;
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
             ? EOF
             : *p1++;
  ;
  if (ch == '-')
    sgn = true,
    ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
             ? EOF
             : *p1++;
  ;
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ 48),
    ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
             ? EOF
             : *p1++;
  ;
  return sgn ? -x : x;
}
const int N = 1e5 + 100;
int n, k, Q;
long long s[N], ans[N];
int b[N], B;
vector<long long> dz;
int dd[N], da[N], ds[N];
struct query {
  int l, r, id;
  bool operator<(const query &o) const {
    if (b[l] != b[o.l]) return l < o.l;
    return b[l] & 1 ? r < o.r : r > o.r;
  }
} q[N];
int c[N];
int main() {
  cin >> n >> k;
  B = sqrt(n) + 1;
  for (int i = 1; i <= n; ++i) scanf("%d", &s[i]), b[i] = i / B + 1;
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    s[i] = s[i - 1] + (s[i] == 1 ? x : -x);
    dz.push_back(s[i]);
  }
  dz.push_back(0), (dz).push_back(INT_MIN), sort((dz).begin(), (dz).end()),
      (dz).erase(unique((dz).begin(), (dz).end()), (dz).end());
  for (int i = 0; i <= n; ++i) {
    dd[i] = (lower_bound(dz.begin(), dz.end(), (s[i])) - dz.begin());
    int j = (lower_bound(dz.begin(), dz.end(), (s[i] + k)) - dz.begin());
    if (j < ((int)(dz).size()) && dz[j] == s[i] + k) da[i] = j;
    j = (lower_bound(dz.begin(), dz.end(), (s[i] - k)) - dz.begin());
    if (j < ((int)(dz).size()) && dz[j] == s[i] - k) ds[i] = j;
  }
  cin >> Q;
  for (int i = 1; i <= Q; ++i) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;
  sort(q + 1, q + Q + 1);
  int l = 1, r = 0;
  long long res = 0;
  for (int i = 1; i <= Q; ++i) {
    while (l < q[i].l) {
      --c[dd[l - 1]];
      res -= c[da[l - 1]] + (s[r] == s[l - 1] + k);
      ++l;
    }
    while (l > q[i].l) {
      --l;
      res += c[da[l - 1]] + (s[r] == s[l - 1] + k);
      ++c[dd[l - 1]];
    }
    while (r < q[i].r) {
      ++r;
      ++c[dd[r - 1]];
      res += c[ds[r]];
    }
    while (r > q[i].r) {
      res -= c[ds[r]];
      --c[dd[r - 1]];
      --r;
    }
    ans[q[i].id] = res;
  }
  for (int i = 1; i <= Q; ++i) printf("%lld\n", ans[i]);
  return 0;
}
