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».
//package ;
import java.io.*;
import java.util.*;

public class Problem_245H {
	static long[]pow_31,pow_iv;
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter pw = new PrintWriter(System.out);
		long m=(long)1e9+9;
		char[]s=sc.nextLine().toCharArray();
		pow_31=new long[s.length+1];
		pow_iv=new long[s.length+1];
		char[]rev=new char[s.length];
		
		for(int i=1,k=0;i<=s.length;i++,k++)
		{
			rev[i-1]=s[s.length-i];
			pow_31[k]=pow_mod(31, k, m);
			pow_iv[k]=pow_mod(pow_31[k], m-2, m);
		}
		long p=31;
		long hash_value=0,hash_value_rev=0,p_pow=1;
		long[]hash=new long[s.length+1];
		long[]hash_rev=new long[s.length+1];
		int k=1;
		for(int i=0;i<s.length;i++)
		{
			char c1=s[i],c2=rev[i];
			hash_value=(hash_value+(c1-'a'+1)*p_pow)%m;
			hash_value_rev=(hash_value_rev+(c2-'a'+1)*p_pow)%m;
			p_pow=(p_pow*p)%m;
			hash[k]=hash_value;
			hash_rev[k++]=hash_value_rev;
		}
		long[][]ans=new long[s.length][s.length];
		int n=s.length;
		for(int i=n-1;i>=0;i--)
		{
			for(int j=i;j<s.length;j++)
			{
				if(j!=i)
					ans[i][j]=ans[i+1][j]+ans[i][j-1]-ans[i+1][j-1];
				if(substring_hash(hash, i+1, j+1)==substring_hash(hash_rev, n-j, n-i))
				{
//					ans[i][j]=1;
					ans[i][j]+=1;
//					System.out.println(i+" "+j);
				}
			}
//			System.out.println(i+" "+Arrays.toString(ans[i]));
		}
		int q=sc.nextInt();
		while(q-->0)
		{
			int l=sc.nextInt()-1,r=sc.nextInt()-1;
			pw.println(ans[l][r]);
		}
		pw.close();
	}
	static long substring_hash(long hash[],int start,int end)
	{
		long m=(long)1e9+9;
//		return (((hash[end]-hash[start-1]+m)%m)*pow_mod(pow_mod(31, start, m), m-2, m))%m;		
		return (((hash[end]-hash[start-1]+m)%m)*pow_iv[start-1])%m;		
	}
	static long pow_mod(long b,long p,long m)//O(log(p))
	{
		long res=1;
		while(p>0)
		{
			if(p%2==1)
				res=res*b%m;
			b=b*b%m;
			p/=2;
		}
		return res;
	}
	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

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

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

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

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

		String nextLine() throws IOException {
			return br.readLine();
		}

		boolean hasnext() throws IOException {
			return br.ready();
		}

	}
}
