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

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

public class  test{
	
	// ArrayList<Integer> lis = new ArrayList<Integer>();
	// ArrayList<String> lis = new ArrayList<String>();
	// PriorityQueue<Integer> que = new PriorityQueue<Integer>();
	//  Stack<Integer> que = new Stack<Integer>();
    //	static long sum=0;
	// int a,b,c;
	// 1000000007 (10^9+7)
	//static int mod = 1000000007;
	static int mod = 1000000009,r=0,k;
   // static int dx[]={1,-1,0,0};
//	static int dy[]={0,0,1,-1};
//	static int dx[]={1,-1,0,0,1,1,-1,-1};
//  static int dy[]={0,0,1,-1,1,-1,1,-1};
	//static long H,L;
	//static Set<Integer> set = new HashSet<Integer>();
	
public  static void main(String[] args)   throws Exception, IOException{
   //String line=""; throws Exception, IOException
   //(line=br.readLine())!=null
	//Scanner sc =new Scanner(System.in);
	// !!caution!! int long //  
	Reader sc = new Reader(System.in);
  // while( ){
	  // int  n=sc.nextInt(),m=sc.nextInt();//a[]=new int[n],b[]=new int[n];
	//int  n=sc.nextInt(),m=sc.nextInt(),h=sc.nextInt(),t=sc.nextInt();
	String s=sc.nextString();
	
	int n=sc.nextInt(),l=s.length();
	//boolean dp[][]=new boolean[l+1][l+1];
	int dp[][]=new int[l+1][l+1];
    dp[1][1]++;
	for(int i=2;i<=l;i++){
		int k=s.charAt(i-1)-'a';
		for(int t=3; t<=i;t++){
			if( s.charAt(i-t)==s.charAt(i-1) )dp[i][t]+=dp[i-1][t-2];
		}
		dp[i][1]++;
		if( s.charAt(i-1)==s.charAt(i-2) )dp[i][2]++;
	}
		
	
	for(int i=2;i<=l;i++){
		for(int t=1;t+1<=i;t++){
		  dp[i][t+1]+=dp[i][t]+dp[i-1][t]-dp[i-1][t-1];	
		  
		}
	}
	
	for(int i=0;i<n;i++){
		int x=sc.nextInt(),y=sc.nextInt();
		System.out.println(dp[y][y-x+1]);
		
	}
	
}

static void db(Object... os){
    System.err.println(Arrays.deepToString(os));

}
}



class Reader
{
	private BufferedReader x;
	private StringTokenizer st;
	
	public Reader(InputStream in)
	{
		x = new BufferedReader(new InputStreamReader(in));
		st = null;
	}
	public String nextString() throws IOException
	{
		while( st==null || !st.hasMoreTokens() )
			st = new StringTokenizer(x.readLine());
		return st.nextToken();
	}
	public int nextInt() throws IOException
	{
		return Integer.parseInt(nextString());
	}
	public long nextLong() throws IOException
	{
		return Long.parseLong(nextString());
	}
	public double nextDouble() throws IOException
	{
		return Double.parseDouble(nextString());
	}
}




/*class P implements Comparable<P>{
//	implements Comparable<P>
	int a,b,c;
	P(int a,int b,int c){
		this.a=a;
		this.b=b;
		this.c=c;
	} 
	public int compareTo(P x){
	   	 return b-x.b; 
	   }
}
*/
