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 Template implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine(), " :");
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    int[] readIntArray(int size) throws IOException {
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = readInt();
        }
        return res;
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    <T> List<T>[] createGraphList(int size) {
        List<T>[] list = new List[size];
        for (int i = 0; i < size; i++) {
            list[i] = new ArrayList<>();
        }
        return list;
    }

    public static void main(String[] args) {
        new Template().run();
//        new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            if (System.getProperty("ONLINE_JUDGE") == null) {
                time();
                memory();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    final int BASKET = 350;
    final int MAX_BASKET_NUM = 100_000 / BASKET + 4;

    class Query implements Comparable<Query> {
        int l, r, index;

        public Query(int l, int r, int index) {
            this.l = l;
            this.r = r;
            this.index = index;
        }

        @Override
        public int compareTo(Query o) {
            int b1 = l / BASKET;
            int b2 = o.l / BASKET;
            if (b1 != b2) return b1 - b2;
            return r - o.r;
        }
    }

    void solve() throws IOException {
        int n = readInt();
        int k = readInt();
        int[] t = readIntArray(n);
        int[] a = readIntArray(n);
        for (int i = 0; i < n; i++) {
            if (t[i] == 2) {
                a[i] *= -1;
            }
        }

        long[] p = new long[n + 1];
        TreeSet<Long> set = new TreeSet<>();
        set.add(0L);
        set.add(-k * 1L);
        set.add(k * 1L);
        for (int i = 1; i < p.length; i++) {
            p[i] = p[i - 1] + a[i - 1];
            set.add(p[i]);
            set.add(p[i] - k);
            set.add(p[i] + k);
        }
        HashMap<Long, Integer> cord = new HashMap<>();
        for (long x : set) {
            cord.put(x, cord.size());
        }

        int[] count = new int[cord.size()];
        int[] piplus = new int[p.length];
        int[] piminus = new int[p.length];
        int[] pi = new int[p.length];
        for (int i = 0; i < p.length; i++) {
            pi[i] = cord.get(p[i]);
            piplus[i] = cord.get(p[i] + k);
            piminus[i] = cord.get(p[i] - k);
        }


        int q = readInt();
        Query[] queries = new Query[q];
        for (int i = 0; i < q; i++) {
            queries[i] = new Query(readInt() - 1, readInt(), i);
        }
        Arrays.sort(queries);

        long[] answer = new long[q];
        int l = 0, r = -1;
        long curAns = 0;

        for (Query query : queries) {
            while (r < query.r) {
                r++;
                curAns += count[piminus[r]];
                count[pi[r]]++;
            }
            while (r > query.r) {
                count[pi[r]]--;
                curAns -= count[piminus[r]];
                r--;
            }

            while (l < query.l) {
                count[pi[l]]--;
                curAns -= count[piplus[l]];
                l++;
            }
            while (l > query.l) {
                l--;
                curAns += count[piplus[l]];
                count[pi[l]]++;
            }
            answer[query.index] = curAns;
        }
        for (long x : answer) {
            out.println(x);
        }
    }
}