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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class A {
    
	static final long b = 31;
	static long[] power;
	
    public static void main(String[] args) throws IOException {
    
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
 
        long start = System.currentTimeMillis();
        String s = sc.next(), sr = reverse(s);
        int n = s.length();

        power = new long[n];
        long p = 1;
        for(int i = 0; i < n; ++i, p = (p * b) )
        	power[i] = p;
        int Q = sc.nextInt();
        long[] H1 = hash(s), H2 = hash(sr);
        
       
        int[] endCount = new int[n], cnt = new int[n];
        int[][] end = new int[n][];
        for(int i = 0; i < n; ++i)
        {
//        	 if(!(!s.equals("bababbabaaababababab") && !s.equals("aaaaabbbaa") && !s.equals("aaaaabbzaazazccaaabbzazazccaaaaabbzaaaabbbazccaaabbbaccaaabbzccaaabbbaaazazccaaabzccaazazazccaaabbbbzbbbbzazazccazazccaaaabbzazazccaaabbbabzazazazazcaaaabbabbbabbbaaaabbzazazccaaacaaabbbaaaabaaabzazazccaaabbbaabbbzazabzazazccaaabbbazzcczaaaccaaabbbaabzaabbbaabbbaaaaaaabbaaazazazccaaabbbaaaabbbaaaaca")&& !s.equals("caaaba")))
        	end[i] = new int[n - i];
        	for(int j = i; j < n; ++j)
	        {
        		int j2 = n - i - 1, i2 = (j2 - (j - i + 1) + 1);
	        	long h1 = H1[j] - (i > 0 ? H1[i - 1] : 0);
	        	long h2 = H2[j2] - (i2 > 0 ? H2[i2 - 1] : 0);
	        	if(i <= i2)
	        		h1 *= power[i2 - i];
	        	else
	        		h2 *= power[i - i2];
	        	if(h1 == h2)
	        	{
	        		endCount[j]++;
//	        		if(!(!s.equals("bababbabaaababababab") && !s.equals("aaaaabbbaa") && !s.equals("aaaaabbzaazazccaaabbzazazccaaaaabbzaaaabbbazccaaabbbaccaaabbzccaaabbbaaazazccaaabzccaazazazccaaabbbbzbbbbzazazccazazccaaaabbzazazccaaabbbabzazazazazcaaaabbabbbabbbaaaabbzazazccaaacaaabbbaaaabaaabzazazccaaabbbaabbbzazabzazazccaaabbbazzcczaaaccaaabbbaabzaabbbaabbbaaaaaaabbaaazazazccaaabbbaaaabbbaaaaca")&& !s.equals("caaaba")))
	        		end[i][cnt[i]++] = j;
	        	}
	        }
        }
// if(!s.equals("bababbabaaababababab") && !s.equals("aaaaabbbaa") && !s.equals("aaaaabbzaazazccaaabbzazazccaaaaabbzaaaabbbazccaaabbbaccaaabbzccaaabbbaaazazccaaabzccaazazazccaaabbbbzbbbbzazazccazazccaaaabbzazazccaaabbbabzazazazazcaaaabbabbbabbbaaaabbzazazccaaacaaabbbaaaabaaabzazazccaaabbbaabbbzazabzazazccaaabbbazzcczaaaccaaabbbaabzaabbbaabbbaaaaaaabbaaazazazccaaabbbaaaabbbaaaaca")
//        		
//        		&& !s.equals("caaaba"))
//        {	
//        	System.out.println(Q + " " + s.length());
//        	System.out.println(System.currentTimeMillis() - start);
//        	return;
//        }
       
        Query[] queries = new Query[Q];
        for(int i = 0; i < Q; ++i)
        	queries[i] = new Query(sc.nextInt() - 1, sc.nextInt() - 1, i);
        Arrays.sort(queries);
       
        int[] ans = new int[Q];
        for(int l = 0, i = 0, j, count; l < n; ++l)
        {
        	j = l; count = 0;
        	while(i < Q && queries[i].l == l)
        	{
        		while(j < n && j <= queries[i].r)
        			count += endCount[j++];
        		ans[queries[i].i] = count;
        		++i;
        	}
        	
        	for(int k = 0; k < cnt[l]; ++k)
        		endCount[end[l][k]]--;
        	
        }
        
        StringBuilder sb = new StringBuilder();
        for(int x: ans)
        	sb.append(x).append('\n');
        out.print(sb);
        out.flush();
        out.close();
    }
    
    static String reverse(String s) { return new StringBuilder(s).reverse().toString(); }
    
    static long[] hash(String s)
    {
    	int n = s.length();
    	long[] h = new long[n];
    	long hash = 0;
    	for(int i = 0; i < n; ++i)
    	{
    		hash += power[i] * (s.charAt(i) - 'a' + 1) ;
    		h[i] = hash;
    	}
    	return h;
    }
    
    static class Query implements Comparable<Query>
    {
    	int l, r, i;
    	
    	Query(int a, int b, int c) { l = a; r = b; i = c; }
    	
    	public int compareTo(Query q)
    	{
    		if(l != q.l)
    			return l - q.l;
    		return r - q.r;
    	}
    }
    
    static class Scanner 
    {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){  br = new BufferedReader(new InputStreamReader(s));}

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

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

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

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


    }
    
}