You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes.

String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|.

String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1.

Input

The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query.

It is guaranteed that the given string consists only of lowercase English letters.

Output

Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces.

Examples

Input

caaaba
5
1 1
1 4
2 3
4 6
4 5


Output

1
7
3
4
2

Note

Consider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba».
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
	InputReader scn;
	PrintWriter out;
	String INPUT = "caaaba\r\n" + "5\r\n" + "1 1\r\n" + "1 4\r\n" + "2 3\r\n" + "4 6\r\n" + "4 5\r\n" + "";

	void solve() {
		char[] arr = (scn.next()).toCharArray();
		int n = arr.length;
		long[] frw = new long[n], rev = new long[n];

		long mod = BigInteger.probablePrime(30, new Random()).longValue();
		long mul = BigInteger.probablePrime(30, new Random()).longValue();

		long[] power = new long[n], invPow = new long[n];

		power[0] = 1;
		for (int i = 1; i < n; i++) {
			power[i] = (mul * power[i - 1]) % mod;
		}

		for (int i = 0; i < n; i++) {
			invPow[i] = pow(power[i], mod - 2, mod);
		}

		frw[0] = arr[0];
		for (int i = 1; i < n; i++) {
			frw[i] = ((arr[i] * power[i]) % mod + frw[i - 1]) % mod;
		}

		rev[n - 1] = arr[n - 1];
		for (int i = n - 2; i >= 0; i--) {
			rev[i] = ((arr[i] * power[n - 1 - i]) % mod + rev[i + 1]) % mod;
		}

		int[][] dp = new int[n][n];
		for (int l = n - 1; l >= 0; l--) {
			for (int r = l; r < n; r++) {
				if (l == r) {
					dp[l][r] = 1;
				} else {
					dp[l][r] = (fh(frw, l, r, invPow, mod) == rh(rev, l, r, invPow, mod, n) ? 1 : 0);
					dp[l][r] += dp[l + 1][r] + dp[l][r - 1] - dp[l + 1][r - 1];
				}
			}
		}

		int q = scn.nextInt();
		while (q-- > 0) {
			int l = scn.nextInt() - 1, r = scn.nextInt() - 1;
			out.println(dp[l][r]);
		}
	}

	long pow(long a, long x, long m) {
		if (x == 0) {
			return 1;
		}

		long p = pow(a, x / 2, m);
		p *= p;
		p %= m;
		if (x % 2 == 1) {
			p *= a;
			p %= m;
		}

		return p;
	}

	long fh(long[] f, int i, int j, long[] invPow, Long M) {
		long jh = f[j]; // hash for S[0..j]
		long ih = (i > 0) ? f[i - 1] : 0; // hash for S[0..i-1]
		long subhash = ((jh + M - ih) * invPow[i]) % M;
		return subhash;
	}

	long rh(long[] r, int i, int j, long[] invPow, Long M, int n) {
		long ih = r[i]; // hash for reverse S[i..n-1]
		long jh = (j < n - 1) ? r[j + 1] : 0; // hash for reverse S[j+1..n-1]
		long subhash = ((ih + M - jh) * invPow[n - 1 - j]) % M;
		return subhash;
	}

	void run() throws Exception {
		boolean onlineJudge = System.getProperty("ONLINE_JUDGE") != null;
		out = new PrintWriter(System.out);
		scn = new InputReader(onlineJudge);
		long time = System.currentTimeMillis();
		solve();
		out.flush();
		if (!onlineJudge) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

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

	class InputReader {
		InputStream is;

		public InputReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

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

		char nextChar() {
			return (char) skip();
		}

		String next() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		char[] next(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		int nextInt() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		long nextLong() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

		int[] nextArray(int n, boolean isOneInd) {
			int k = isOneInd ? 1 : 0;
			int[] a = new int[n + k];
			for (int i = k; i < n + k; i++)
				a[i] = nextInt();
			return a;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				arr[i] = arr[i] ^ arr[j];
				arr[j] = arr[i] ^ arr[j];
				arr[i] = arr[i] ^ arr[j];
			}
			return arr;
		}
	}
}
