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 f implements Runnable{
	public static void main (String[] args) {new Thread(null, new f(), "_cf", 1 << 28).start();}

	int size;
	
	public void run() {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("Go!");

		int n = fs.nextInt();
		size = (int)Math.sqrt(n);
		int k = fs.nextInt();
		long[] pref = new long[n+1];
		boolean[] id = new boolean[n];
		for(int i = 0; i < n; i++) {
			id[i] = fs.nextInt() == 1 ? false : true;
		}
		for(int i = 1; i <= n; i++) {
			int x = fs.nextInt();
			if(id[i-1]) x = -x;
			pref[i] += pref[i-1];
			pref[i] += x;
		}
		int q = fs.nextInt();
		Query[] qs = new Query[q];
		for(int i = 0; i < q; i++) {
			qs[i] = new Query(fs.nextInt()-1, fs.nextInt(), i);
		}
		int[] p = new int[n+1];
		int[] rightPair = new int[n+1];
		int[] leftPair = new int[n+1];
		HashMap<Long, Integer> map = new HashMap<>();
		for(int i = 0; i <= n; i++) {
			if(!map.containsKey(pref[i])) map.put(pref[i], map.size());
			if(!map.containsKey(pref[i]-k)) map.put(pref[i]-k, map.size());
			if(!map.containsKey(pref[i]+k)) map.put(pref[i]+k, map.size());
			p[i] = map.get(pref[i]);
			rightPair[i] = map.get(pref[i]-k);
			leftPair[i] = map.get(pref[i]+k);
		}
		
		long[] res = new long[q];
		int[] freq = new int[map.size()];
		Arrays.sort(qs);
		int left = 0, right = -1;
		long cnt = 0;
		for(Query qq : qs) {
			while(right < qq.r) {
				right++;
				cnt += freq[rightPair[right]];
				freq[p[right]]++;
			}
			while(right > qq.r) {
				freq[p[right]]--;
				cnt -= freq[rightPair[right]];
				right--;
			}
			while(left < qq.l) {
				freq[p[left]]--;
				cnt -= freq[leftPair[left]];
				left++;
			}
			while(left > qq.l) {
				left--;
				cnt += freq[leftPair[left]];
				freq[p[left]]++;
			}
			res[qq.id] = cnt;
		}
		for(long ll : res)
			out.println(ll);
		
		out.close();
	}
	
	class Query implements Comparable<Query> {
		int l, r, id;
		Query(int a, int b, int i) {
			l = a; r = b; id = i;
		}
		public int compareTo(Query q) {
			if(l / size == q.l / size)
				return Integer.compare(r, q.r);
			return Integer.compare(l/size, q.l/size);
		}
	}

	class FastScanner {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public FastScanner() {
			in = new BufferedInputStream(System.in, BS);
		}

		public FastScanner(String s) throws FileNotFoundException {
			in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}                
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
		
		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}
		
	}

}