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].
import java.io.*;
import java.util.*;

public class CODEFORCES
{
	@SuppressWarnings("rawtypes")
	static InputReader in;
	static PrintWriter out;

	static int sq;

	static class query implements Comparable<query>
	{

		int l, r, block, ind;

		query(int a, int b, int c)
		{
			l = a;
			r = b;
			block = l / sq;
			ind = c;
		}

		@Override
		public int compareTo(query o)
		{
			return Integer.compare(block, o.block) != 0 ? Integer.compare(block, o.block) : Integer.compare(r, o.r);
		}

	}

	static long ans = 0;

	static void solve()
	{
		int n = in.ni(), k = in.ni();
		int t[] = new int[n + 1];
		for (int i = 1; i <= n; i++)
			t[i] = in.ni();
		long pre[] = new long[n + 1];
		TreeSet<Long> set = new TreeSet<Long>();
		for (int i = 1; i <= n; i++)
		{
			pre[i] = pre[i - 1];
			int p = in.ni();
			if (t[i] == 2)
				p *= -1;
			pre[i] += p;
			set.add(pre[i]);
		}
		set.add((long) 0);
		sq = (int) (n / Math.sqrt(n));
		int cnt = 1;
		HashMap<Long, Integer> map = new HashMap<Long, Integer>();
		for (long i : set)
			map.put(i, cnt++);
		int ii[] = new int[n + 1];
		for (int i = 0; i <= n; i++)
			ii[i] = map.get(pre[i]);
		long lcnt[] = new long[cnt + 10];
		long rcnt[] = new long[cnt + 10];
		int pr[] = new int[n + 1];
		int su[] = new int[n + 1];
		for (int i = 0; i <= n; i++)
		{
			if (map.containsKey(pre[i] + k))
				pr[i] = map.get(pre[i] + k);
			else
				pr[i] = -1;
		}
		for (int i = 0; i <= n; i++)
		{
			if (map.containsKey(pre[i] - k))
				su[i] = map.get(pre[i] - k);
			else
				su[i] = -1;
		}
		int qi = in.ni();
		query q[] = new query[qi];
		for (int i = 0; i < qi; i++)
			q[i] = new query(in.ni(), in.ni(), i);
		Arrays.sort(q);
		int l = 1, r = 0;
		long hh[] = new long[qi];
		for (int i = 0; i < qi; i++)
		{
			int li = q[i].l, ri = q[i].r;
			int ind = q[i].ind;
			while (r < ri)
			{
				r++;
				lcnt[ii[r - 1]]++;
				if (su[r] != -1)
					ans += lcnt[su[r]];
				rcnt[ii[r]]++;
			}
			while (l > li)
			{
				l--;
				rcnt[ii[l]]++;
				if (pr[l - 1] != -1)
					ans += rcnt[pr[l - 1]];
				lcnt[ii[l - 1]]++;
			}
			while (l < li)
			{
				lcnt[ii[l - 1]]--;
				if (pr[l - 1] != -1)
					ans -= rcnt[pr[l - 1]];
				rcnt[ii[l]]--;
				l++;
			}
			while (r > ri)
			{
				rcnt[ii[r]]--;
				if (su[r] != -1)
					ans -= lcnt[su[r]];
				lcnt[ii[r - 1]]--;
				r--;
			}
			hh[ind] = ans;
		}
		for (int i = 0; i < qi; i++)
			out.println(hh[i]);
	}

	@SuppressWarnings("rawtypes")
	static void soln()
	{
		in = new InputReader(System.in);
		out = new PrintWriter(System.out);
		solve();
		out.flush();
	}

	static void debug(Object... o)
	{
		System.out.println(Arrays.deepToString(o));
	}

	public static void main(String[] args)
	{
		new Thread(null, new Runnable()
		{
			public void run()
			{
				try
				{
					soln();
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}, "1", 1 << 26).start();
	}

	// To Get Input
	// Some Buffer Methods
	static class InputReader<SpaceCharFilter>
	{

		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream)
		{
			this.stream = stream;
		}

		public int snext()
		{
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars)
			{
				curChar = 0;
				try
				{
					snumChars = stream.read(buf);
				} catch (IOException e)
				{
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int ni()
		{
			int c = snext();
			while (isSpaceChar(c))
			{
				c = snext();
			}
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nl()
		{
			int c = snext();
			while (isSpaceChar(c))
			{
				c = snext();
			}
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int[] nextIntArray(int n)
		{
			int a[] = new int[n];
			for (int i = 0; i < n; i++)
			{
				a[i] = ni();
			}
			return a;
		}

		public long[] nextLongArray(int n)
		{
			long a[] = new long[n];
			for (int i = 0; i < n; i++)
			{
				a[i] = nl();
			}
			return a;
		}

		public String readString()
		{
			int c = snext();
			while (isSpaceChar(c))
			{
				c = snext();
			}
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine()
		{
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = snext();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		private boolean isEndOfLine(int c)
		{
			return c == '\n' || c == '\r' || c == -1;
		}

		public interface SpaceCharFilter
		{
			public boolean isSpaceChar(int ch);
		}

	}
}