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.*;
import java.util.*;

public class Main implements Runnable {

	public void _main() throws IOException {
		int n = nextInt();		
		int[] h = new int[n];		
		for (int i = 0; i < n; i++) {
			h[i] = nextInt();			
		}
		int[] d1 = new int[n - 1];
		int[] d2 = new int[n - 1];
		int minChar = Integer.MAX_VALUE;
		for (int i = 0; i < n - 1; i++) {
			d1[i] = h[i] - h[i + 1];
			d2[i] = h[i + 1] - h[i];
			minChar = Math.min(minChar, d1[i]);
			minChar = Math.min(minChar, d2[i]);
		}
		int[] s = new int[2 * n];
		System.arraycopy(d1, 0, s, 0, n - 1);
		s[n - 1] = minChar - 1;
		System.arraycopy(d2, 0, s, n, n - 1);
		s[2 * n - 1] = minChar - 2;

		int[] sa = buildSuffixArray(s);
		int[] lcp = buildLCP(sa, s);
		int[][] rmq = buildSparseTable(lcp);
		int[] invSA = new int[sa.length];
		for (int i = 0; i < sa.length; i++) {
			invSA[sa[i]] = i;
		}

		int numQueries = nextInt();		
		int[] ans = new int[numQueries];
		List<Point> points = new ArrayList<Point>();
		int[] ql = new int[numQueries];
		int[] qr = new int[numQueries];
		for (int i = 0; i < numQueries; i++) {			
			int queryL = nextInt() - 1;
			int queryR = nextInt() - 1;
			ql[i] = queryL;
			qr[i] = queryR;

			if (queryL == queryR) {
				ans[i] = n - 1;
				continue;
			}
			--queryR;

			int pos = invSA[queryL];
			int needLCP = queryR - queryL + 1;

			int l = -1, r = pos;
			while (r - l > 1) {
				int m = (l + r) / 2;
				if (answerRMQ(rmq, m, pos - 1) >= needLCP) {
					r = m;
				} else {
					l = m;
				}
			}
			int left = r;

			l = pos - 1;
			r = s.length;
			while (r - l > 1) {
				int m = (l + r) / 2;
				if (answerRMQ(rmq, pos, m) >= needLCP) {
					l = m;
				} else {
					r = m;
				}
			}
			int right = l + 1;

			int L1 = 0;
			int R1 = queryL - 1 - needLCP;
			int L2 = queryR + 2;
			int R2 = n - 1 - needLCP;
			L1 += n;
			R1 += n;
			L2 += n;
			R2 += n;
			points.add(new Point(left, L1, R1, L2, R2, true, i));
			points.add(new Point(right + 1, L1, R1, L2, R2, false, i));			
		}

		FenwickTree f = new FenwickTree(s.length);
		int pointId = 0;
		Collections.sort(points);
		for (int i = 0; i <= s.length + 10; i++) {
			while (pointId < points.size() && points.get(pointId).x == i) {
				Point p = points.get(pointId);
				if (p.opens) {
					ans[p.id] -= f.getSum(p.l1, p.r1) + f.getSum(p.l2, p.r2);
				} else {
					ans[p.id] += f.getSum(p.l1, p.r1) + f.getSum(p.l2, p.r2);
				}
				++pointId;
			}
			if (i < s.length) {
				f.add(sa[i], +1);
			}
		}

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

	class Point implements Comparable<Point> {
		int x;
		int l1, r1, l2, r2;
		boolean opens;
		int id;
		Point(int x, int l1, int r1, int l2, int r2, boolean opens, int id) {
			this.x = x;
			this.l1 = l1;
			this.r1 = r1;
			this.l2 = l2;
			this.r2 = r2;
			this.opens = opens;
			this.id = id;
		}
		public int compareTo(Point o) {
			return x - o.x;
		}
	}

	final int infinity = 1000000000;

	private int[][] buildSparseTable(int[] a) {
		int k = 0;
		int n = a.length;
		while ((1 << k) < n) {
			++k;
		}
		++k;
		int[][] table = new int[k][1 << k];		
		System.arraycopy(a, 0, table[0], 0, n);
		Arrays.fill(table[0], n, 1 << k, infinity);
		for (int log = 0; log + 1 < k; log++) {
			int len = 1 << log;
			for (int i = 0; i + len <= n; i++) {
				table[log + 1][i] = Math.min(table[log][i], table[log][i + len]);
			}
		}
		return table;
	}

	private int answerRMQ(int[][] rmq, int l, int r) {
		if (l > r) {
			return infinity;
		}
		int k = 0;
		while ((1 << k) <= r - l + 1) {
			++k;
		}
		--k;
		if ((1 << k) == r - l + 1) {
			return rmq[k][l];
		}
		return Math.min(rmq[k][l], rmq[k][r - (1 << k) + 1]);
	}

	private int[] buildLCP(int[] sa, int[] s) {
		int n = s.length; 				
		int[] lcp = new int[n - 1];
		int k = 0;
		int[] invSA = new int[n];
		for (int i = 0; i < n; i++) {
			invSA[sa[i]] = i;
		}
		for (int i = 0; i < n; i++) {
			if (k > 0) {
				--k;
			}
			if (invSA[i] == n - 1) {
				k = 0;
				continue;
			}			
			int j = sa[invSA[i] + 1];			
			while (i + k < n && j + k < n && s[i + k] == s[j + k]) {
				++k;
			}
			lcp[invSA[i]] = k;
		}
		return lcp;
	}

	private int[] buildSuffixArray(int[] _s) {
		int[] s = compress(_s);
		int n = s.length;
		int[] am = new int[n];
		for (int x : s) {
			++am[x];
		}
		int[] start = new int[n];
		int numCols = 0;
		int total = 0;
		for (int i = 0; i < n; i++) {
			if (am[i] > 0) {
				start[numCols] = total;
				total += am[i];
				++numCols;
			}
		}
		int[] sa = new int[n];
		int[] tmp = start.clone();
		int[] col = new int[n];
		for (int i = 0; i < n; i++) {
			col[i] = s[i];
			sa[tmp[col[i]]++] = i;
		}
		int len = 1;
		while (len < n) {
			int[] ncol = new int[n];
			int[] nsa = new int[n];
			for (int i = 0; i < n; i++) {
				int j = sa[i] - len;
				if (j < 0) {
					j += n;
				}
				nsa[start[col[j]]++] = j;
			}
			numCols = 1;
			start[0] = 0;
			for (int i = 0; i < n; i++) {
				if (i > 0) {
					int u = nsa[i];
					int v = nsa[i - 1];
					if (col[u] != col[v] || col[(u + len) % n] != col[(v + len) % n]) {						
						start[numCols] = i;
						++numCols;
					}
				}
				ncol[nsa[i]] = numCols - 1;
			}
			col = ncol;
			sa = nsa;
			len *= 2;
		}
		return sa;
	}

	private int[] compress(int[] s) {
		int[] sorted = s.clone();
		Arrays.sort(sorted);
		int k = 0;
		for (int i = 0; i < sorted.length; i++) {
			if (i == 0 || sorted[i] != sorted[i - 1]) {
				sorted[k++] = sorted[i];
			}
		}
		int[] res = new int[s.length];
		for (int i = 0; i < s.length; i++) {
			res[i] = Arrays.binarySearch(sorted, 0, k, s[i]);
		}
		return res;
	}

	class FenwickTree {
		int[] a;
		public FenwickTree(int size) {
			a = new int[size];
		}
		void add(int pos, int val) {		
			while (pos < a.length) {
				a[pos] += val;
				pos |= pos + 1;
			}
		}
		int getSum(int l, int r) {
			if (l > r) {
				return 0;
			}
			return getSum(r) - getSum(l - 1);
		}
		int getSum(int r) {
			int res = 0;
			while (r >= 0) {
				res += a[r];
				r = (r & (r + 1)) - 1;
			}
			return res;
		}
	}


	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer st;

	private String next() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String rl = in.readLine();
			if (rl == null)
				return null;
			st = new StringTokenizer(rl);
		}
		return st.nextToken();
	}

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

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

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

	public static void main(String[] args) {
		Locale.setDefault(Locale.UK);
		new Thread(new Main()).start();
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);

			_main();

			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(202);
		}
	}

}