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 static java.lang.Math.*; 

import java.io.*; 
import java.util.*; 
import java.math.*;

public class Template {
	BufferedReader in; 
	PrintWriter out;
	StringTokenizer st; 

	String next() {
		while (st==null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(in.readLine());
			} catch (Exception e) {}
		}
		return st.nextToken(); 
	}

	int nextInt() {
		return Integer.parseInt(next()); 
	}

	long nextLong() {
		return Long.parseLong(next());
	}

	double nextDouble() {
		return Double.parseDouble(next());
	}

	int inf = 1100000000; 

	int[][] P, rmq; 
	int n, m, M, N; 
	int[] h, lg, aib, ans; 
	Suffix[] L;
	int lev = 1;

	class Suffix implements Comparable<Suffix> {
		int left=-1, right=-1, pos=-1;
		public Suffix() {}
		public int compareTo(Suffix o) {
			if (left != o.left) return left - o.left;
			if (right != o.right) return right - o.right; 
			return pos - o.pos;
		}

		public boolean equals(Suffix o) {
			return left == o.left && right == o.right;
		}
	}

	public void sa() {
		P = new int[23][m]; 
		L = new Suffix[m];

		for (int i=0; i<m; i++) {
			L[i] = new Suffix();
			L[i].pos = i;
			P[0][i] = h[i];
		}

		for (int step = 1; step <= m; step<<=1, lev++) {
			for (int i=0; i<m; i++) {
				L[i].left = P[lev - 1][L[i].pos];
				L[i].right = L[i].pos + step < m ? P[lev - 1][L[i].pos + step] : -inf;
			}

			Arrays.sort(L);

			for (int i=0; i<m; i++) 
				if (i > 0 && L[i].equals(L[i-1])) P[lev][L[i].pos] = P[lev][L[i-1].pos]; 
				else P[lev][L[i].pos] = i;
		}
		lev--;
	}

	public void sparse() {
		rmq = new int[23][m - 1];
		for (int i=0; i<m-1; i++) rmq[0][i] = lcp(L[i].pos, L[i+1].pos); 
		for(int i = 1; (1 << i) <= m - 1; i++)
            for(int j = 0; j + (1 << i) - 1 < m - 1; j++)
                rmq[i][j] = Math.min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);


	}

	public int lcp(int i, int j) {
		int ret = 0;
		for (int k=lev+2; k>=0 && i < m && j < m; k--) {
			if (i + (1 << k) <= m && j + (1<<k) <= m && P[k][i] == P[k][j]) {
				i+=(1<<k);
				j+=(1<<k);
				ret+=(1<<k);
			}
		}
		return ret;
	}

	public void run() throws Exception {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		Long start = System.currentTimeMillis();

		n = nextInt();
		m = 2*n - 1; 
		h = new int[m];
		for (int i=0; i<n; i++) h[i] = nextInt();

		for (int i=0; i<n-1; i++) h[i] -= h[i+1];

		h[n - 1] = -inf;

		for (int i=n; i<m; i++) h[i] = -h[i-n];

		sa();
		sparse();

		M = m;  
		N = n;

		lg = new int[M + 1];
        lg[1] = 0;
        for(int i = 2; i <= M; i++)
            lg[i] = lg[i / 2] + 1;

        int Q = nextInt();
        ans = new int[Q];
        Query[] queries = new Query[2 * Q];
        int cnt = 0;
        int[] ans = new int[Q];
        for(int i = 0; i < Q; i++) {
            int a = nextInt() - 1;
            int b = nextInt() - 1;
            int length = b - a;

            if(a == b) {
                ans[i] = N - 1;
                continue;
            }

            b--;

            int sa = P[lev][a];

            int lower = sa;
            int st = 0;
            int dr = sa - 1;
            int m;
            while(st <= dr) {
                m = (st + dr) / 2;
                if(rmq(sa, m) >= length) {
                    dr = m - 1;
                    lower = m;
                }
                else
                    st = m + 1;
            }

            int upper = sa;
            st = sa + 1;
            dr = M - 1;
            while(st <= dr) {
                m = (st + dr) / 2;
                if(rmq(sa, m) >= length) {
                    st = m + 1;
                    upper = m;
                }
                else
                    dr = m - 1;
            }
            //System.err.println(lower + " " + upper); 
            queries[cnt++] = new Query(N, N + a - 1 - length, N + b + 2, M - 1, lower, i, true);
            queries[cnt++] = new Query(N, N + a - 1 - length, N + b + 2, M - 1, upper + 1, i, false);
        }

        Arrays.sort(queries, 0, cnt);

        aib = new int[M + 1];
        int j = 0;
        for(int i = 0; j < cnt; i++) {
            while(j < cnt && queries[j].index == i) {
                Query q = queries[j];
                if(q.start)
                    ans[q.queryIdx] -= getSum(q.L1, q.R1) + getSum(q.L2, q.R2);
                else
                    ans[q.queryIdx] += getSum(q.L1, q.R1) + getSum(q.L2, q.R2);
                j++;
            }

            if(i < M)
                update(L[i].pos);
        }

        for(int i = 0; i < Q; i++)
            out.println(ans[i]);

		out.close(); 
 	}
	
	public static void main(String[] args) throws Exception {
		new Template().run(); 
	}

	private class Query implements Comparable<Query> {
        public int L1;
        public int R1;
        public int L2;
        public int R2;
        public int index;
        public int queryIdx;
        public boolean start;

        private Query(int l1, int r1, int l2, int r2, int index, int queryIdx, boolean start) {
            L1 = l1;
            R1 = r1;
            L2 = l2;
            R2 = r2;
            this.index = index;
            this.queryIdx = queryIdx;
            this.start = start;
        }

        public int compareTo(Query o) {
            return index - o.index;
        }
    }

    private int rmq(int x, int y) {
        if(x > y) {
            int aux = x; x = y; y = aux;
        }
        y--;
        int k = lg[y - x + 1];
        return Math.min(rmq[k][x], rmq[k][y - (1 << k) + 1]);
    }    
    private int getSum(int left, int right) {
        if(left > right)
            return 0;
        return query(right) - query(left - 1);
    }

    private int query(int pos) {
        pos++;
        int ret = 0;
        while(pos > 0) {
            ret += aib[pos];
            pos -= (pos & (-pos));
        }
        return ret;
    }

    private void update(int pos) {
        pos++;
        while(pos < aib.length) {
            aib[pos]++;
            pos += (pos & (-pos));
        }
    }    
}