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.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class UVA245H {

	private static String s;
	private static int q;
	private static int[][] dp1 = new int[5001][5001];
	private static int[][] dp2 = new int[5001][5001];
	
	public static void main(String[] args) {
		InputReader in = new InputReader(System.in);
		PrintWriter w = new PrintWriter(System.out);
		s = in.readString();
		q = in.nextInt();
		int n = s.length();
		
//		dp1 = new int[5001][5001];;
//		dp2 = new int[5001][5001];;
		
		for(int i=0; i<n; i++) {
			Arrays.fill(dp1[i], -1);
			Arrays.fill(dp2[i], -1);
		}
		
		for(int i=0; i<n; i++) {
			for(int j=i; j<n; j++) {
				isPalindrome(i, j);
			}
		}
		
		for(int i=0; i<q; i++) {
			int l = in.nextInt()-1;
			int r = in.nextInt()-1;
			w.println(count(l, r));
		}
		
		w.flush();
	}
	
	private static int count(int i, int j) {
		if (i>j) return 0;
		if (i==j) {
			return 1;
		}
		if (dp2[i][j]!=-1) return dp2[i][j];
		
		//returns palindromes in substring (i, j)
		return dp2[i][j]=dp1[i][j]+count(i+1, j)+count(i, j-1)-count(i+1, j-1);
	}
	
	
	private static int isPalindrome(int i, int j) {
		if (i==j) {
			return dp1[i][j]=1;
		} else if (j==i+1) {
			return dp1[i][j]=s.charAt(i)==s.charAt(j)?1:0;
		}
		
		if (dp1[i][j]!=-1) return dp1[i][j];
		
		int ans=0;
		if (s.charAt(i)!=s.charAt(j)) {
			ans = 0;
		} else {
			ans = isPalindrome(i+1, j-1);
		}
		return dp1[i][j] = ans;
	}

	static class InputReader {
		 
		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
 
		public InputReader(InputStream st) {
			this.stream = st;
		}
 
		public int read() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}
 
		public int nextInt() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
 
		public long nextLong() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
 
		public int[] nextIntArray(int n) {
			int a[] = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextInt();
			}
			return a;
		}
 
		public String readString() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}
 
		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}
 
		public boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
 
		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}
 
	}
}
