Problem: John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the right: the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has width 1 and height hi. We will assume that the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has index i.

A piece of the fence from l to r (1 ≤ l ≤ r ≤ n) is a sequence of planks of wood with indices from l to r inclusive, that is, planks with indices l, l + 1, ..., r. The width of the piece of the fence from l to r is value r - l + 1.

Two pieces of the fence from l1 to r1 and from l2 to r2 are called matching, if the following conditions hold:

  * the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence; 
  * the pieces are of the same width; 
  * for all i (0 ≤ i ≤ r1 - l1) the following condition holds: hl1 + i + hl2 + i = hl1 + hl2. 



John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of wood planks in the fence. The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — the heights of fence planks.

The third line contains integer q (1 ≤ q ≤ 105) — the number of queries. Next q lines contain two space-separated integers li and ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the i-th piece of the fence.

Output

For each query on a single line print a single integer — the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input.

Examples

Input

10
1 2 2 1 100 99 99 100 100 100
6
1 4
1 2
3 4
1 5
9 10
10 10


Output

1
2
2
0
2
9

Solution (in JAVA):

Solution:
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author AlexFetisov
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] H = Utils.readIntArray(in, n);
        int[] h = new int[2 * (n-1) + 2];
        N = 2 * n;
        for (int i = 1; i < n; ++i) {
            h[i-1] = H[i] - H[i-1];
        }
        h[n-1] = Integer.MIN_VALUE + 5;
        for (int i = n; i < 2 * n; ++i) {
            h[i] = -h[i - n];
        }
        h[N-1] = h[n-1] - 1;
        int[] hh = h.clone();
        hh = ArrayUtils.sortAndUnique(hh);
        for (int i = 0; i < N; ++i) h[i] = Arrays.binarySearch(hh, h[i]);
        array = new SuffixArray(h, N + 5);

        int[] begins = new int[N];
        Arrays.fill(begins, -1);
        for (int i = 2; i < N; ++i) {
            if (array.p[i] >= n) {
                begins[i] = array.p[i] - n;
            }
        }

        treeMin = new SegmentTreeMin(array.lcp);

        VersionSegmentTree tree = new VersionSegmentTree(begins);

        int[] sum = new int[begins.length+1];
        sum[0] = 0;
        for (int i = 0; i < begins.length; ++i) {
            sum[i+1] = sum[i];
            if (begins[i] == -1) {
                ++sum[i+1];
            }
        }

        int q = in.nextInt();
        while (q-- > 0) {
            int left = in.nextInt()-1, right = in.nextInt()-1;
            if (right == left) {
                out.println(n-1);
                continue;
            }
            --right;
            int height = right - left + 1;
            int pos = array.p2[left];

            int rangeLeft = extendLeft(pos, height);
            int rangeRight = extendRight(pos, height);

            int val = rangeRight - rangeLeft + 1 - sum[rangeRight+1] + sum[rangeLeft];
            if (val == 0) {
                out.println(0);
                continue;
            }
            out.println(tree.getLessOrEqual(rangeLeft, rangeRight, left - height - 1) + val - tree.getLessOrEqual(rangeLeft, rangeRight, right + 1));
        }
    }

    private int extendLeft(int pos, int height) {
        int left = 0, right = pos - 1, res = pos, e;
        while (left <= right) {
            e = (left + right) >> 1;
            if (treeMin.getMin(e, pos-1) >= height) {
                res = e;
                right = e - 1;
            } else {
                left = e + 1;
            }
        }
        return res;
    }

    private int extendRight(int pos, int height) {
        int left = pos + 1, right = N-1, res = pos, e;
        while (left <= right) {
            e = (left + right) >> 1;
            if (treeMin.getMin(pos, e-1) >= height) {
                res = e;
                left = e + 1;
            } else {
                right = e - 1;
            }
        }
        return res;
    }

    int N;
    SuffixArray array;
    SegmentTreeMin treeMin;

    class VersionSegmentTree {
        public int getLessOrEqual(int rangeLeft, int rangeRight, int value) {
//            int l;
//            if (levels.containsKey(value)) {
//                l = levels.get(value);
//            } else {
//                Integer key = levels.lowerKey(value);
//                if (key == null) return 0;
//                l = levels.get(key.intValue());
//            }
            if (value < 0) return 0;
            int l = expLevels[value];
            if (l == -1) return 0;
            Vertex cur = root[l];
            return getSum(cur, 0, maxRight, rangeLeft, rangeRight);
        }

        private int getSum(Vertex v, int rangeLeft, int rangeRight, int left, int right) {
            if (left == rangeLeft && right == rangeRight) {
                return v.sum;
            }
            int middle = (rangeLeft + rangeRight) >> 1;
            int sum = 0;
            if (left <= middle) {
                sum += getSum(v.left, rangeLeft, middle, left, Math.min(right, middle));
            }
            if (right > middle) {
                sum += getSum(v.right, middle + 1, rangeRight, Math.max(left, middle + 1), right);
            }
            return sum;
        }

        class Vertex {
            Vertex left;
            Vertex right;
            int sum = 0;

            Vertex() {
            }

            public Vertex(Vertex l, Vertex r) {
                left = l;
                right = r;
            }

            public Vertex(int x) {
                sum = x;
            }

            public Vertex(int s, Vertex l, Vertex r) {
                sum = s;
                left = l;
                right = r;
            }
        }

        Vertex[] root;
        TreeMap<Integer, Integer> levels;
        int maxRight;
        int[] expLevels;

        VersionSegmentTree(int[] a) {
            expLevels = new int[300001];
            Arrays.fill(expLevels, -1);
            maxRight = a.length-1;
            List<Pair<Integer, Integer>> pairs = new ArrayList<Pair<Integer, Integer>>(a.length);
            levels = new TreeMap<Integer, Integer>();
            for (int i = 0; i < a.length; ++i) {
                if (a[i] != -1) {
                    pairs.add(Pair.makePair(a[i], i));
                }
            }
            Collections.sort(pairs);

            root = new Vertex[a.length + 1];
            root[0] = createTree(0, maxRight);
            int cnt = 1;
            for (Pair<Integer, Integer> p : pairs) {
                root[cnt] = addValue(root[cnt-1], p.second, 0, maxRight);
                expLevels[p.first] = cnt;
                //levels.put(p.first, cnt);
                ++cnt;
            }
            int cur = expLevels[0];
            for (int i = 1; i < expLevels.length; ++i) {
                if (expLevels[i] == -1) expLevels[i] = cur;
                cur = expLevels[i];
            }
        }

        private Vertex createTree(int left, int right) {
            if (left == right) {
                return new Vertex();
            } else {
                int e = (left + right) / 2;
                Vertex l = createTree(left, e);
                Vertex r = createTree(e + 1, right);
                return new Vertex(l, r);
            }
        }

        private Vertex addValue(Vertex root, int x, int left, int right) {
            if (left == right) {
                return new Vertex(1);
            } else {
                int e = (left + right) / 2;
                if (x <= e) {
                    Vertex r = root.right;
                    Vertex l = addValue(root.left, x, left, e);
                    return new Vertex(r.sum + l.sum, l, r);
                } else {
                    Vertex l = root.left;
                    Vertex r = addValue(root.right, x, e + 1, right);
                    return new Vertex(r.sum + l.sum, l, r);
                }
            }
        }
    }

    class SegmentTreeMin {

        int[][] table;
        int n;
        int[] log;

        SegmentTreeMin(int[] a) {
            n = a.length;
            table = new int[18][n];
            table[0] = a.clone();
            for (int i = 1, size = 2; i < 18; ++i, size <<= 1) {
                for (int start = 0; start + size - 1 < n; ++start) {
                    int mi = table[i-1][start];
                    int mi2 = table[i-1][start + size / 2];
                    table[i][start] = Math.min(mi, mi2);
                }
            }
            log = new int[a.length+1];
            log[0] = 0;
            log[1] = 0;
            log[2] = 1;
            int nxt = 4;
            int cur = 1;
            for (int i = 3; i <= a.length; ++i) {
                if (i == nxt) {
                    nxt *= 2;
                    ++cur;
                }
                log[i] = cur;
            }
        }

        int getMin(int from, int to) {
            int len = to - from + 1;
            int lg = log[len];
            int res = table[lg][from];
            res = Math.min(table[lg][to - (1 << lg) + 1], res);
            return res;
        }
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer stt;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
    }

    public String nextLine() {
        try {
            return reader.readLine().trim();
        } catch (IOException e) {
            return null;
        }
    }

    public String nextString() {
        while (stt == null || !stt.hasMoreTokens()) {
            stt = new StringTokenizer(nextLine());
        }
        return stt.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

}

class Utils {
    public static int[] readIntArray(InputReader in, int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; ++i) {
            a[i] = in.nextInt();
        }
        return a;
    }

}

class ArrayUtils {

    public static int[] sortAndUnique(int[] a) {
        int[] b = a.clone();
        Arrays.sort(b);
        ArrayList<Integer> temp = new ArrayList<Integer>();
        temp.add(b[0]);
        int size = 1;
        for (int i = 1; i < b.length; ++i) {
            if (temp.get(size-1) != b[i]) {
                temp.add(b[i]);
                ++size;
            }
        }
        return CollectionUtils.toArray(temp);
    }

}

class SuffixArray {
    public int[] s;
    public int n;
    int[] c;
    int[] c2;
    public int[] p;
    public int[] p2;
    public int[] lcp;
    int[] num;
    int[] pos;

    public SuffixArray(String s) {
        char[] cc = s.toCharArray();
        this.s = new int[cc.length];
        for (int i = 0; i < cc.length; ++i) {
            this.s[i] = cc[i];
        }
        n = s.length();
        c = new int[n];
        c2 = new int[n];
        p = new int[n];
        p2 = new int[n];
        lcp = new int[n];
        num = new int[Math.max(n+1, 500)];
        pos = new int[Math.max(1+n, 500)];
        build();
    }

    public SuffixArray(int[] s, int alphabet) {
        this.s = s;
        n = s.length;
        c = new int[n];
        c2 = new int[n];
        p = new int[n];
        p2 = new int[n];
        lcp = new int[n];
        num = new int[Math.max(n+1, alphabet)];
        pos = new int[Math.max(1+n, alphabet)];
        build();
    }

    private void build() {
        for (int i = 0; i < n; ++i) {
            c[i] = s[i];
            p[i] = i;
        }
        for (int k = 0, k1 = 1; k < n; k = k1, k1 <<= 1) {
            Arrays.fill(num, 0);
            for (int i = 0; i < n; ++i) ++num[c[i]];
            pos[0] = 0;
            for (int i = 0; i < num.length-1; ++i) pos[i+1] = pos[i] + num[i];   /// ???
            for (int i = 0; i < n; ++i) {
                int j = (p[i] - k + n) % n;
                p2[pos[c[j]]++] = j;
            }
            int cc = 0;
            for (int i = 0; i < n; ++i) {
                if (i > 0 && (c[p2[i-1]] != c[p2[i]] || c[(p2[i-1]+k)%n] != c[(p2[i]+k)%n])) ++cc;
                c2[p2[i]] = cc;
            }
            for (int i = 0; i < n; ++i) {
                c[i] = c2[i];
                p[i] = p2[i];
            }
        }
        for (int i = 0; i < n; ++i) p2[p[i]] = i;
        int k = 0;
        for (int i = 0; i < n; ++i) {
            if (p2[i] == n-1) {
                lcp[p2[i]] = k = 0;
                continue;
            }
            if (k > 0) --k;
            int j = p[p2[i]+1];
            while (k < n && s[(i + k) % n] == s[(j + k) % n]) ++k;
            lcp[p2[i]] = k;
            if (p2[(i + 1) % n] > p2[(j + 1) % n]) k = 0;
        }
    }
}

class Pair<U, V> implements Comparable<Pair<U, V>> {
    public final U first;
    public final V second;

    public static<U, V> Pair<U, V> makePair(U first, V second) {
        return new Pair<U, V>(first, second);
    }

    public Pair(U first, V second) {
        this.first = first;
        this.second = second;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Pair pair = (Pair) o;

        return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null);

    }

    public int hashCode() {
        int result = first != null ? first.hashCode() : 0;
        result = 31 * result + (second != null ? second.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "(" + first + "," + second + ")";
    }

    public int compareTo(Pair<U, V> o) {
        int value = ((Comparable<U>)first).compareTo(o.first);
        if (value != 0)
            return value;
        return ((Comparable<V>)second).compareTo(o.second);
    }
}

class CollectionUtils {
    public static int[] toArray(Collection<Integer> collection) {
        int[] result = new int[collection.size()];
        int index = 0;
        for (int element : collection) {
            result[index++] = element;
        }
        return result;
    }

    //    public static List<Double> unique(List<Double> a) {
//        List<Double> result = new ArrayList<Double>();
//        result.add(a.get(0));
//        int last = 0;
//        for (int i = 1; i < a.size(); ++i) {
//            if (DoubleUtils.compareTo(a.get(i), result.get(last)) != 0) {
//                result.add(a.get(i));
//                ++last;
//            }
//        }
//        return result;
//    }

    //    public static void unique(List<Long> a) {
//        Collections.sort(a);
//        int size = 1;
//        for (int i = 1; i < a.size(); i++) {
//            if (!a.get(i).equals(a.get(size - 1))) {
//                a.set(size++, a.get(i));
//            }
//        }
//        for (int i = a.size() - 1; i >= size; i--) {
//            a.remove(i);
//        }
//    }

}