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.io.*;

public class Main implements Runnable {
		
	public void solve() throws IOException {
		char[] c = nextToken().toCharArray();
                int N = c.length;
                                   //startPOS, endPOS
                int[][] palin = new int[N+1][N+1];//will use it as 1 based index
                for(int i = 0; i < N; i++) palin[i+1][i+1] = 1; //BASE CONDITION 1, FOR ODD
                for(int i = 0; i < N-1; i++){ //BASE CONDITION 2, FOR EVEN
                    if(c[i] == c[i+1]){
                        palin[i+1][i+2] = 1;
                    }
                }
                
                for(int len = 3; len <= N; len++){
                    for(int st = 0; st <= N - len; st++){
                        int end = st +  len - 1;
                        if(c[st] == c[end] && palin[st+1+1][end-1+1] == 1){
                            palin[st+1][end+1] = 1;
                        }
                    }
                }

//               System.out.println("----------"); 
//               print2Int(palin);
                
                //MEMORY LIMIT EXCIDED, SO USING THE SAME 2D ARRAY
                // pigeon hole principle/ set venn diagram
                for(int i = 1; i <= N; i++){
                    for(int j = 1; j <= N; j++){
                        palin[i][j] += palin[i-1][j] +  palin[i][j-1] - palin[i-1][j-1];
                    }
                }
                
//                System.out.println("----------");
//                print2Int(sum);
//                System.out.println("----------");
                
                int Q = nextInt();
                                            //    L R 
                for(int i = 0; i < Q; i++){ // caaaba
                    int l = nextInt();
                    int r = nextInt();
                    int answer = palin[r][r] - palin[r][l-1] - palin[l-1][r] + palin[l-1][l-1]; //note sum is also 1 based index
                    out.println(answer);
                }
	}

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

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }
        
        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }
        
	public void run() {
		try {
                        out = new PrintWriter(System.out);
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
                        out.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

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

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

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
        PrintWriter out;
	BufferedReader in;
	StringTokenizer tok;
}