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.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Crock2012_H {

    Boolean[] isPalnd;
    int N;
    int[] countPal;
    char[] data;

    public int map(int i, int j) {
        return N * (N - 1)/2 - (N-1-i)*(N-i)/2 + (j-i-1);
    }

    public boolean isPalnd(int first, int last) {

        if (first >= last) {
            return true;
        }
        int index = map(first, last);
        if (isPalnd[index] != null)
            return isPalnd[index];

        if (data[first] != data[last])
            return isPalnd[index] = false;
        return isPalnd[index] = isPalnd(first + 1, last - 1);

    }

    public int go(int first, int last) {

        if (first == last)
            return 1;
        if (last < first)
            return 0;
        int index = map(first, last);

        if (countPal[index] != -1)
            return countPal[index];

        countPal[index] = go(first + 1, last) + go(first, last - 1)
                - go(first + 1, last - 1) + ((isPalnd(first, last)) ? 1 : 0);
        return countPal[index];

    }

    public void solve() throws Exception {
        InputReader in = new InputReader();
        String s = in.next();
        data = s.toCharArray();
        N = s.length();
        int l = N * (N - 1) / 2;
        isPalnd = new Boolean[l];
        // for(int i =0 ; i < N;i++)
        // System.out.println(Arrays.toString(isPalnd[i]));

        countPal = new int[l];
        Arrays.fill(countPal, -1);
        go(0, N - 1);
        int q = in.nextInt();
        // System.out.println(q);
        StringBuffer t = new StringBuffer("");
        for (int i = 0; i < q; i++) {
            if (i == 0)
                t.append(go(in.nextInt() - 1, in.nextInt() - 1));
            else
                t.append(" " + go(in.nextInt() - 1, in.nextInt() - 1));

        }
        System.out.println(t);
    }

    public static void main(String[] args) throws Exception {
        new Crock2012_H().solve();

    }

    static class InputReader {
        BufferedReader in;
        StringTokenizer st;

        public InputReader() throws IOException {
            in = new BufferedReader(new InputStreamReader(System.in));
            st = new StringTokenizer(in.readLine());

        }

        public String next() throws IOException {

            while (!st.hasMoreElements())
                st = new StringTokenizer(in.readLine());
            return st.nextToken();
        }

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

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

}
