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.awt.geom.Line2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.TreeSet;

import javax.swing.text.Segment;

public class q1 
{
    public static void main(String[] args) {
        //      FasterScanner in=new FasterScanner();
        //      PrintWriter out=new PrintWriter(System.out);
        //      int t=in.nextInt();
        //      for(int ii=0;ii<t;ii++)
        //      {
        new q1().solve();
        //      }//System.out.println(Long.MIN_VALUE);
        //      out.close();
    }

    public q1()
    {
        in=new FasterScanner();
        out=new PrintWriter(System.out);
    }

    FasterScanner in;
    PrintWriter out;
    long mod=1000000007L ;
    long mod1=(long)(1e18+3);
    public void solve()
    {
        String str=in.nextLine();
//      long[] dp1=new long[str.length()+1];
//      dp1[0]=1;
//      for(int i=1;i<dp1.length;i++)
//      {
//          dp1[i]=dp1[i-1]*26L;
//          dp1[i]%=mod1;
//      }
////        out.println(Arrays.toString(dp1));
        
        boolean[][] pal=new boolean[str.length()+2][str.length()+2];
        for(int i=str.length();i>0;i--)
        {

            for(int j=0;j<i;j++)
                pal[i][j]=true;
            for(int j=i;j<=str.length();j++)
            {
                
                if(i==j)
                {
                    pal[i][j]=true;
                }
                else
                {
                    pal[i][j]=(pal[i+1][j-1]==true && (str.charAt(i-1)==str.charAt(j-1)))?true:false; 
                }
            }
        }
//      for(int i=1;i<=str.length();i++)
//      {
//          out.println(Arrays.toString(pal[i]));
//      }
//      out.println("******************");
        
        int[][] dp=new int[str.length()+2][str.length()+2];
        for(int i=str.length();i>0;i--)
        {
            for(int j=i;j<=str.length();j++)
            {
                if(i==j)
                {
                    dp[i][j]=1;
                    continue;
                }
                    
                dp[i][j]=dp[i+1][j]+dp[i][j-1];
                dp[i][j]-=dp[i+1][j-1];
                if(pal[i][j])
                    dp[i][j]++;
            }
        //  out.println(Arrays.toString(dp[i]));
        }
//      for(int i=1;i<=str.length();i++)
//      {
//          out.println(Arrays.toString(dp[i]));
//      }
//      out.println("***************");
        
        int q=in.nextInt();
        for(int i=0;i<q;i++)
        {
            int l=in.nextInt();
            int r=in.nextInt();
            out.println(dp[l][r]);
        }
        out.close();
    }
    
    public static class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = System.in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        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 String nextString() {
            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 long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        public long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        private 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;
        }
    }
} 
