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.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
import java.util.Map.Entry;

public class Main {
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;
	private PrintWriter pw;
	private long mod = 998244353;

	private StringBuilder ans_sb;
	private int sqrt;
	private void soln() {
		int n = nextInt();
		int k = nextInt();
		int[] arr = nextIntArray(n);
		ArrayList<Long> l1 = new ArrayList<>();
		l1.add(0L);
		long[] a = new long[n+1];
		for(int i=1;i<=n;i++) {
			long x = nextInt();
			if(arr[i-1] == 2)
				x = -x;
			a[i] = x;
			if(i != 0)
				a[i] += a[i-1];
			l1.add(a[i]);
			l1.add(a[i]-k);
			l1.add(a[i]+k);
		}
		l1.add(0L);
		l1.add(0L+k);
		l1.add(0L-k);
		Collections.sort(l1);
		HashMap<Long, Integer> map = new HashMap<>();
		int c = 0;
		int len = l1.size();
		for(int i=0;i<len;i++) {
			long x = l1.get(i);
			if(!map.containsKey(x)) {
				map.put(x, c++);
			}
		}
		int q = nextInt();
		sqrt = (int) Math.sqrt(q)+1;
		//debug(map);
		Pair[] qs = new Pair[q];
		for(int i=0;i<q;i++) {
			qs[i] = new Pair(nextInt() - 1, nextInt(), i);
		}
		int[][] arrs = new int[n+1][3];
		for(int i=0;i<=n;i++) {
			arrs[i][0] = map.get(a[i]);
			arrs[i][1] = map.get(a[i]-k);
			arrs[i][2] = map.get(a[i]+k);
		}
		//debug(arrs);
		Arrays.parallelSort(qs);
		long ans = 0;
		int size = 300000;
		long[] cnt = new long[size];
		int ll = qs[0].l;
		int rr = ll-1;
		long[] anss = new long[q];
		for(int i=0;i<q;i++) {
			Pair pp = qs[i];
			int l = pp.l;
			int r = pp.r;
			while(ll < l) {
				cnt[arrs[ll][0]]--;
				ans -= cnt[arrs[ll][2]];
				ll++;
			}
			while(ll > l) {
				ll--;
				ans += cnt[arrs[ll][2]];
				cnt[arrs[ll][0]]++;
			}
			while(rr < r) {
				rr++;
				ans += cnt[arrs[rr][1]];
				cnt[arrs[rr][0]]++;
			}
			while(rr > r) {
				cnt[arrs[rr][0]]--;
				ans -= cnt[arrs[rr][1]];
				rr--;
			}
			anss[pp.i] = ans;
		}
		for(int i=0;i<q;i++)
			pw.println(anss[i]);
	}
	private class Pair implements Comparable<Pair>{
		int l,r,i;
		public Pair(int b,int z,int c) {
			l = b;
			r = z;
			i=c;
		}
		@Override
		public int compareTo(
				Pair o)
		{
			if(o.l/sqrt != l/sqrt)
				return l - o.l;
			else
				return this.r - o.r;
		}
	}
	private void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
	private long pow(long a, long b, long c) {
		if (b == 0)
			return 1;
		long p = pow(a, b / 2, c);
		p = (p * p) % c;
		return (b % 2 == 0) ? p : (a * p) % c;
	}

	private long gcd(long n, long l) {
		if (l == 0)
			return n;
		return gcd(l, n % l);
	}

	public static void main(String[] args) throws Exception {
		new Thread(null, new Runnable() {
			@Override
			public void run() {
				new Main().solve();
			}
		}, "1", 1 << 26).start();
		//new Main().solve();
	}

	public StringBuilder solve() {
		InputReader(System.in);
		/*
		 * try { InputReader(new FileInputStream("C:\\Users\\hardik\\Desktop\\in.txt"));
		 * } catch(FileNotFoundException e) {}
		 */
		pw = new PrintWriter(System.out);
		// ans_sb = new StringBuilder();
		soln();

		pw.close();
		// System.out.println(ans_sb);
		return ans_sb;
	}

	public void InputReader(InputStream stream1) {
		stream = stream1;
	}

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

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

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

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

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

	private String nextToken() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

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

	private int[] nextIntArray(int n) {
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = nextInt();
		}
		return arr;
	}

	private long[] nextLongArray(int n) {
		long[] arr = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = nextLong();
		}
		return arr;
	}

	private void pArray(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		return;
	}

	private void pArray(long[] arr) {
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		return;
	}

	private boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return isWhitespace(c);
	}

	private char nextChar() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		char c1 = (char) c;
		while (!isSpaceChar(c))
			c = read();
		return c1;
	}

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