#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000+10;
int isPal[maxN][maxN]; // 1 or 0
int dp[maxN][maxN]; // count of palindromes in [i,j]

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    string s;
    cin >> s;
    int n = (int)s.size();
    // preprocessing (constructive algorithm)
    for (int i = 0; i < n; i++) {
        isPal[i][i]=1; dp[i][i] = 1;
        // ensures that 2 consec equal #s are labeled pals
        isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
    }
    for (int len = 1; len < n; len++) {
        for (int start = 0; start+len < n; start++) {
            isPal[start][start+len] = isPal[start+1][start+len-1] &
            (s[start] == s[start+len]);
            dp[start][start+len] = dp[start][start+len-1] +
            dp[start+1][start+len] - dp[start+1][start+len-1] +
            isPal[start][start+len];
        }
    }
    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        int l,r; cin >> l >> r;
        l--; r--;
        cout << dp[l][r] << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000+10;
int isPal[maxN][maxN]; // 1 or 0
int dp[maxN][maxN]; // count of palindromes in [i,j]
char s[maxN];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> s;
    int n = strlen(s);
    // preprocessing (constructive algorithm)
    for (int i = 0; i < n; i++) {
        isPal[i][i]=1; dp[i][i] = 1;
        // ensures that 2 consec equal #s are labeled pals
        isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
    }
    for (int len = 1; len <= n; len++) {
        for (int start = 0; start <= n-len; start++) {
            isPal[start][start+len] = isPal[start+1][start+len-1] &
            (s[start] == s[start+len]);
            dp[start][start+len] = dp[start][start+len-1] +
            dp[start+1][start+len] - dp[start+1][start+len-1] +
            isPal[start][start+len];
        }
    }
    int q; scanf("%d",&q);
    int l,r;
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &l, &r);
        l--; r--;
        printf("%d\n", dp[l][r]);
    }

    return 0;
}
// #include <iostream>
// #include <cstdio>
// #include <cstring>
// using namespace std;
// const int SZ = 5010;

// char S[SZ];
// int get[SZ][SZ];
// bool ha[SZ][SZ];

// int main()
// {
//     int n, s, t;
//     scanf("%s", S + 1);
//     int len = strlen(S + 1);
//     for(int i = 1; i <= len; i++)
//         ha[i][i] = ha[i + 1][i] = get[i][i] = 1;
//     for(int k = 2; k <= len; k++)
//         for(int j, i = 1; i + k - 1 <= len; i++)
//         {
//             j = i + k - 1;
//             ha[i][j] = (S[i] == S[j] && ha[i + 1][j - 1]);
//             get[i][j] = get[i + 1][j] + get[i][j - 1] - get[i + 1][j - 1] + ha[i][j];
//         }
//     scanf("%d", &n);
//     while(n--)
//     {
//         scanf("%d%d", &s, &t);
//         printf("%d\n", get[s][t]);
//     }
//     return 0;
// }


#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000+10;
int isPal[maxN][maxN]; // 1 or 0
int dp[maxN][maxN]; // count of palindromes in [i,j]
char s[maxN];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    scanf("%s", s); // good strat for improving time
    int n = strlen(s);
    // preprocessing (constructive algorithm)
    for (int i = 0; i < n; i++) {
        isPal[i][i]=1; dp[i][i] = 1;
        // ensures that 2 consec equal #s are labeled pals
        isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
    }
    for (int len = 1; len <= n; len++) {
        for (int start = 0; start <= n-len; start++) {
            isPal[start][start+len] = isPal[start+1][start+len-1] &
            (s[start] == s[start+len]);
            dp[start][start+len] = dp[start][start+len-1] +
            dp[start+1][start+len] - dp[start+1][start+len-1] +
            isPal[start][start+len];
        }
    }
    int q; scanf("%d",&q);
    int l,r;
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &l, &r);
        l--; r--;
        printf("%d\n", dp[l][r]);
    }

    return 0;
}
// #include <iostream>
// #include <cstdio>
// #include <cstring>
// using namespace std;
// const int SZ = 5010;

// char S[SZ];
// int get[SZ][SZ];
// bool ha[SZ][SZ];

// int main()
// {
//     int n, s, t;
//     scanf("%s", S + 1);
//     int len = strlen(S + 1);
//     for(int i = 1; i <= len; i++)
//         ha[i][i] = ha[i + 1][i] = get[i][i] = 1;
//     for(int k = 2; k <= len; k++)
//         for(int j, i = 1; i + k - 1 <= len; i++)
//         {
//             j = i + k - 1;
//             ha[i][j] = (S[i] == S[j] && ha[i + 1][j - 1]);
//             get[i][j] = get[i + 1][j] + get[i][j - 1] - get[i + 1][j - 1] + ha[i][j];
//         }
//     scanf("%d", &n);
//     while(n--)
//     {
//         scanf("%d%d", &s, &t);
//         printf("%d\n", get[s][t]);
//     }
//     return 0;
// }


#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000+10;
int isPal[maxN][maxN]; // 1 or 0
int dp[maxN][maxN]; // count of palindromes in [i,j]

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    char s[5000+10];
    cin >> s;
    int n = strlen(s);
    // preprocessing (constructive algorithm)
    for (int i = 0; i < n; i++) {
        isPal[i][i]=1; dp[i][i] = 1;
        // ensures that 2 consec equal #s are labeled pals
        isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
    }
    for (int len = 1; len < n; len++) {
        for (int start = 0; start+len < n; start++) {
            isPal[start][start+len] = isPal[start+1][start+len-1] &
            (s[start] == s[start+len]);
            dp[start][start+len] = dp[start][start+len-1] +
            dp[start+1][start+len] - dp[start+1][start+len-1] +
            isPal[start][start+len];
        }
    }
    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        int l,r; cin >> l >> r;
        l--; r--;
        cout << dp[l][r] << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000+10;
int isPal[maxN][maxN]; // 1 or 0
int dp[maxN][maxN]; // count of palindromes in [i,j]

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    string s;
    cin >> s;
    int n = s.size();
    // preprocessing (constructive algorithm)
    for (int i = 0; i < n; i++) {
        isPal[i][i]=1; dp[i][i] = 1;
        // ensures that 2 consec equal #s are labeled pals
        isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
    }
    for (int len = 1; len < n; len++) {
        for (int start = 0; start+len < n; start++) {
            isPal[start][start+len] = isPal[start+1][start+len-1] &
            (s[start] == s[start+len]);
        }
    }
    for (int len = 1; len < n; len++) {
        for (int start = 0; start+len < n; start++) {
            dp[start][start+len] = dp[start][start+len-1] +
            dp[start+1][start+len] - dp[start+1][start+len-1] +
            isPal[start][start+len];
        }
    }
    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        int l,r; cin >> l >> r;
        l--; r--;
        cout << dp[l][r] << endl;
    }

    return 0;
}


// #include <bits/stdc++.h>
// using namespace std;
// const int maxN = 5000+10;
// int isPal[maxN][maxN]; // 1 or 0
// int dp[maxN][maxN]; // count of palindromes in [i,j]
// int main() {
//     ios::sync_with_stdio(0);
//     cin.tie(0); cout.tie(0);
//     string s; cin >> s;
//     int n = s.size();
//     // preprocessing (constructive algorithm)
//     for (int i = 0; i < n; i++) {
//         isPal[i][i]=1; dp[i][i] = 1;
//         // ensures that 2 consec equal #s are labeled pals
//         isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
//     }
//     for (int len = 1; len <= n; len++) {
//         for (int start = 0; start <= n-len; start++) {
//             isPal[start][start+len] = isPal[start+1][start+len-1] &
//             (s[start] == s[start+len]);
//             dp[start][start+len] = dp[start][start+len-1] +
//             dp[start+1][start+len] - dp[start+1][start+len-1] +
//             isPal[start][start+len];
//         }
//     }
//     int q; scanf("%d",&q);
//         for (int i = 0; i < q; i++) {
//         int l,r; scanf("%d %d", &l, &r);
//         l--; r--;
//         printf("%d\n", dp[l][r]);
//     }

//     return 0;
// }
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

char word[5000];
ll dp[5000][5000];
bool palindrome[5000][5000];

int main() {
    memset(dp, 0, sizeof dp);
    cin >> word;

    for (int i=0 ; i<strlen(word)-1 ; i++) {
        palindrome[i][i] = dp[i][i] = 1; // Yes, it's a palindrome, so it counts as one
        palindrome[i][i+1] = (word[i] == word[i+1]); // Check all the two-length palindromes.

        // Now the dp - How many palindromes
        dp[i][i+1] = 2 + (word[i] == word[i+1]); // Then sum them up. The best case is 3:
                                                // S[i], S[i+1] are itself palindromes and S[i..i+1] can also be a palindrome.
    }
    int l = strlen(word) - 1;
    palindrome[l][l] = dp[l][l] = 1;

    for (int j=2 ; j<strlen(word) ; j++) { // First check all the lengths. These loops have to keep this ordering.
        for (int i=0 ; i+j<strlen(word) ; i++) { // For all starting points, check the j-length palindrome
            int l, r;
            l = i;
            r = i+j;
            palindrome[l][r] = (palindrome[l+1][r-1] && (word[l] == word[r]));
        }
    }

    for (int j=2 ; j<strlen(word) ; j++) { // Evaluate all the sizes from 2 to |S|. This loop has to be done this way. Can you figure out why?
        for (int i=0 ; i+j<strlen(word) ; i++) { // Evaluate all the sizes from
            int l, r;
            l = i;
            r = i+j;
            dp[l][r] = dp[l+1][r] + dp[l][r-1] - dp[l+1][r-1] + (palindrome[l][r]); // inclusion - exclusion principle
        }
    }

    int n;
    cin >> n;
    while (n--) {
        int a, b;
        cin >> a >> b;
        cout << dp[a-1][b-1] << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000+10;
int isPal[maxN][maxN]; // 1 or 0
int dp[maxN][maxN]; // count of palindromes in [i,j]
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    string s; cin >> s;
    int n = s.size();
    // preprocessing (constructive algorithm)
    for (int i = 0; i < n; i++) {
        isPal[i][i]=1; dp[i][i] = 1;
        // ensures that 2 consec equal #s are labeled pals
        isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
    }
    for (int len = 1; len <= n; len++) {
        for (int start = 0; start <= n-len; start++) {
            isPal[start][start+len] = isPal[start+1][start+len-1] &
            (s[start] == s[start+len]);
            dp[start][start+len] = dp[start][start+len-1] +
            dp[start+1][start+len] - dp[start+1][start+len-1] +
            isPal[start][start+len];
        }
    }
    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        int l,r; scanf("%d %d", l, r);
        l--; r--;
        cout << dp[l][r] << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000+10;
int isPal[maxN][maxN]; // 1 or 0
int dp[maxN][maxN]; // count of palindromes in [i,j]

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    string s;
    cin >> s;
    int n = s.size();
    // preprocessing (constructive algorithm)
    for (int i = 0; i < n; i++) {
        isPal[i][i]=1; dp[i][i] = 1;
        // ensures that 2 consec equal #s are labeled pals
        isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
    }
    for (int len = 1; len <= n; len++) {
        for (int start = 0; start+len <= n; start++) {
            isPal[start][start+len] = isPal[start+1][start+len-1] &
            (s[start] == s[start+len]);
            dp[start][start+len] = dp[start][start+len-1] +
            dp[start+1][start+len] - dp[start+1][start+len-1] +
            isPal[start][start+len];
        }
    }
    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        int l,r; cin >> l >> r;
        l--; r--;
        cout << dp[l][r] << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxN = 5000+10;
int isPal[maxN][maxN]; // 1 or 0
int dp[maxN][maxN]; // count of palindromes in [i,j]

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    string s;
    cin >> s;
    int n = s.size();
    // preprocessing (constructive algorithm)
    for (int i = 0; i < n; i++) {
        isPal[i][i]=1; dp[i][i] = 1;
        // ensures that 2 consec equal #s are labeled pals
        isPal[i+1][i] = 1; // for [start+1][start+len-1] len 2 pals
    }
    for (int len = 1; len < n; len++) {
        for (int start = 0; start+len < n; start++) {
            isPal[start][start+len] = isPal[start+1][start+len-1] &
            (s[start] == s[start+len]);
            dp[start][start+len] = dp[start][start+len-1] +
            dp[start+1][start+len] - dp[start+1][start+len-1] +
            isPal[start][start+len];
        }
    }
    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        int l,r; cin >> l >> r;
        l--; r--;
        cout << dp[l][r] << endl;
    }

    return 0;
}


/* I solemnly swear that I am upto no good. */

// Tags : Rolling hash technique, prefix hashing, suffix hashing
// Link : http://codeforces.com/contest/245/problem/H

 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define MOD1 999983
 #define p1 1793
 #define MOD2 999931
 #define p2 953

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 #define N 5050
 ll F[N] , B[N], F2[N],B2[N] ;
 ll P[N],P2[N] ;

 pair<ll,ll> phash[N] ;
 pair<ll,ll> shash[N] ;
 void init(){
     P[0] = 1 ; P2[0] = 1 ;
     for(int i = 1 ; i < 5050 ; i++){
         P[i] = (P[i-1]*p1)%MOD1 ;
         P2[i] = (P2[i-1]*p2)%MOD2 ;
     }
 }
 void prehash(string s){

     phash[0] = (mp((ll)s[0],(ll)s[0])) ;
     for(int i = 1 ; i < s.size() ; i++){
         ll fst = phash[i-1].xx ;
         ll snd = phash[i-1].yy ;
         phash[i] = (mp((((fst*p1)%MOD1+(ll)s[i]))%MOD1,((snd*p2)%MOD2+(ll)s[i])%MOD2)) ;
     }
     return ;
 }
 void sufhash(string s){

     int sz = s.size() ;
     shash[sz-1] = mp((ll)s[sz-1],(ll)s[sz-1]) ;
     for(int i = sz-2 ; i >=0 ; i--){
         ll fst = shash[i+1].xx ;
         ll snd = shash[i+1].yy ;
         shash[i] = mp(((fst*p1)%MOD1+(ll)s[i])%MOD1,((snd*p2)%MOD2+(ll)s[i])%MOD2) ;
     }
     return ;
 }
 pair<ll,ll> fhash(string s,int from , int to){
     pair<ll,ll> f2 = phash[to] ;
     pair<ll,ll> f1 = (from>0)?phash[from-1]:mp(0LL,0LL) ;
     return mp((f2.xx%MOD1-(f1.xx*(P[to-from+1]))%MOD1+MOD1)%MOD1,(f2.yy%MOD2-(f1.yy*(P2[to-from+1]))%MOD2+MOD2)%MOD2) ;
 }
 pair<ll,ll> bhash(string s,int from , int to){
     pair<ll,ll> f2 = shash[from] ;
     pair<ll,ll> f1 = ((to+1)<s.size())?shash[to+1]:mp(0LL,0LL) ;
     return mp((f2.xx%MOD1-(f1.xx*(P[to-from+1]))%MOD1+MOD1)%MOD1 ,(f2.yy%MOD2-(f1.yy*(P2[to-from+1]))%MOD2+MOD2)%MOD2) ;
 }
 char pal[5050][5050] ;
 int dp[5050][5050] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ;
    getline(cin,s) ;

    int sz = s.size() ;
    cout << sz << el ;
    cout << "string read" << el ;
    init() ;
    prehash(s) ;
    sufhash(s) ;

    cout << "preprocess" << el ;
    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    cout << "initial" << el ;
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            pair<ll,ll> fh = fhash(s,i,j) ;
            pair<ll,ll> bh = bhash(s,i,j) ;
            //cout << i << sp << j << sp << " OK  " << el ;
            if (fh.xx == bh.xx && fh.yy == bh.yy){
                pal[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
        dp[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)){
                pal[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){
                if (i+1>=sz || j-1<0){
                    dp[i][j] = pal[i][j] ;
                }
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 //ll pal[5002][5002] ;
 ll dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    //memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    // for(int i = 0 ; i < sz; i++){
    //     //pal[i][i] = 1 ;
    //     dp[i][i] = 1 ;
    // }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){
            if (i > j) continue ;
            if (j == 0){
                dp[i][j] = (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)) ;
            }
            else{
                dp[i][j] = dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]+ (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)) ;
            }
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }
    ll pal[5002][5002] ;
    ll dp[5002][5002] ;

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
        dp[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)){
                dp[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){
            if (i > j) continue ;
            if (j == 0){
                dp[i][j] = pal[i][j] ;
            }
            else{
                dp[i][j] = dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]+pal[i][j] ;
            }
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */

// Tags : Rolling hash technique, prefix hashing, suffix hashing
// Link : http://codeforces.com/contest/245/problem/H

 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD
 #define MOD1 999983
 #define p1 1793
 #define MOD2 999931
 #define p2 953

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 #define N 5050
 ll F[N] , B[N], F2[N],B2[N] ;
 ll P[N],P2[N] ;

 pair<ll,ll> phash[N] ;
 pair<ll,ll> shash[N] ;
 void init(){
     P[0] = 1 ; P2[0] = 1 ;
     for(int i = 1 ; i < 5050 ; i++){
         P[i] = (P[i-1]*p1)%MOD1 ;
         P2[i] = (P2[i-1]*p2)%MOD2 ;
     }
 }
 void prehash(string s){

     phash[0] = (mp((ll)s[0],(ll)s[0])) ;
     for(int i = 1 ; i < s.size() ; i++){
         ll fst = phash[i-1].xx ;
         ll snd = phash[i-1].yy ;
         phash[i] = (mp((((fst*p1)%MOD1+(ll)s[i]))%MOD1,((snd*p2)%MOD2+(ll)s[i])%MOD2)) ;
     }
     return ;
 }
 void sufhash(string s){

     int sz = s.size() ;
     shash[sz-1] = mp((ll)s[sz-1],(ll)s[sz-1]) ;
     for(int i = sz-2 ; i >=0 ; i--){
         ll fst = shash[i+1].xx ;
         ll snd = shash[i+1].yy ;
         shash[i] = mp(((fst*p1)%MOD1+(ll)s[i])%MOD1,((snd*p2)%MOD2+(ll)s[i])%MOD2) ;
     }
     return ;
 }
 pair<ll,ll> fhash(string s,int from , int to){
     pair<ll,ll> f2 = phash[to] ;
     pair<ll,ll> f1 = (from>0)?phash[from-1]:mp(0LL,0LL) ;
     return mp((f2.xx%MOD1-(f1.xx*(P[to-from+1]))%MOD1+MOD1)%MOD1,(f2.yy%MOD2-(f1.yy*(P2[to-from+1]))%MOD2+MOD2)%MOD2) ;
 }
 pair<ll,ll> bhash(string s,int from , int to){
     pair<ll,ll> f2 = shash[from] ;
     pair<ll,ll> f1 = ((to+1)<s.size())?shash[to+1]:mp(0LL,0LL) ;
     return mp((f2.xx%MOD1-(f1.xx*(P[to-from+1]))%MOD1+MOD1)%MOD1 ,(f2.yy%MOD2-(f1.yy*(P2[to-from+1]))%MOD2+MOD2)%MOD2) ;
 }
 char pal[5050][5050] ;
 int dp[5050][5050] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ;
    getline(cin,s) ;

    int sz = s.size() ;

    init() ;
    prehash(s) ;
    sufhash(s) ;

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }

    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            pair<ll,ll> fh = fhash(s,i,j) ;
            pair<ll,ll> bh = bhash(s,i,j) ;
            //cout << i << sp << j << sp << " OK  " << el ;
            if (fh.xx == bh.xx && fh.yy == bh.yy){
                pal[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 9768971 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*(b%mod1)+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*(b%mod2)+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(F[i],mul1(F[j+1],pow1(p1,j-i+1)),-1)){
                pal[i][j] = 1 ;
            }
        }
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(F[i],mul1(F[j+1],pow1(p1,j-i+1)),-1)){
                pal[i][j] = 1 ;
            }
        }
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
        dp[i][i] = 1 ;
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){
            pal[i][j] = ((pal[i+1][j-1])&&(s[i]==s[j])) ;
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){
                if (i+1>=sz || j-1<0){
                    dp[i][j] = pal[i][j] ;
                }
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll pal[5002][5002] ;
 ll dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
        dp[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)){
                pal[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){
            if (i > j) continue ;
            if (j == 0){
                dp[i][j] = pal[i][j] ;
            }
            else{
                dp[i][j] = dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]+pal[i][j] ;
            }
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999931 ;
 ll p1 = 953 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return (a*b)%mod1 ;
 }
 ll mul2(ll a, ll b){
     return (a*b)%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow1(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return mul1(temp,temp) ;
 }
 ll pow2(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow2(a,x/2) ;
     if (x&1){
         return mul2(mul2(temp,temp),a) ;
     }
     return mul2(temp,temp) ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    //clock_t begin = clock() ;
    string s ; cin >> s ;

    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
        F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
        B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    memset(P,0,sizeof(P)) ;
    P[0] = 1 ;
    for(int i = 1 ; i < 5050 ; i++){
        P[i] = (P[i-1]*p1)%mod1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) ){
                pal[i][j] = 1 ;
            }
        }
        //cout << i << el ;
    }
    // clock_t end = clock() ;
    // cout << end-begin << el ;
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999931 ;
 ll p1 = 953 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*(b%mod1)+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*(b%mod2)+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b%mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow1(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return mul1(temp,temp) ;
 }
 ll pow2(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow2(a,x/2) ;
     if (x&1){
         return mul2(mul2(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
        F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
        B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            //cout << i << sp << j << el ;
            //cout << p1 << sp << j-i+1 << sp << pow1(p1,j-i+1) << el ;
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1) ){
                pal[i][j] = 1 ;

            }
        }
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 9873 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*(b%mod1)+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*(b%mod2)+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(F[i],mul1(F[j+1],pow1(p1,j-i+1)),-1)){
                pal[i][j] = 1 ;
            }
        }
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>
////
 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 909327 ;
 ll p1 = 1799 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;
 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 ll P2[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;
 string s ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b%mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 void init(){
     memset(pal,0,sizeof(pal)) ;
     memset(dp,0,sizeof(dp)) ;

     int sz = s.size() ;
     for(int i = 0 ; i < sz; i++){
         pal[i][i] = 1 ;
     }

     F[0] = (int)s[0] ;
     F2[0] = (int)s[0] ;
     for(int i = 1 ; i < s.size() ; i++){
         F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
         F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
     }

     B[sz-1] = (int)s[sz-1];
     B2[sz-1] = (int)s[sz-1] ;
     for(int i = sz-2 ; i >= 0 ; i--){
         B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
         B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
     }

     memset(P,0,sizeof(P)) ;
     memset(P2,0,sizeof(P2)) ;
     P[0] = 1 ;
     P2[0] = 1 ;
     for(int i = 1 ; i < 5050 ; i++){
         P[i] = (P[i-1]*p1)%mod1 ;
         P2[i] = (P2[i-1]*p2)%mod2;
     }
 }

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    cin >> s ;

    int sz = s.size() ;
    init() ;

    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) && (add2(F2[j],mul2((i>0)?F2[i-1]:0,P2[j-i+1]),-1) == add2(B2[i],mul2((j+1<sz)?B2[j+1]:0,P2[j-i+1]),-1)) ){
                pal[i][j] = 1 ;
            }
        }
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 909327 ;
 ll p1 = 1799 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return (a*b)%mod1 ;
 }
 ll mul2(ll a, ll b){
     return (a*b)%mod2 ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 ll P2[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    //clock_t begin = clock() ;
    string s ; cin >> s ;

    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = ((F[i-1]*p1)%mod1+(int)s[i])%mod1  ;
        F2[i] = ((F2[i-1]*p2)%mod2+(int)s[i])%mod2 ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = ((B[i+1]*p1)%mod1+(int)s[i])%mod1 ;
        B2[i] = ((B2[i+1]*p2)%mod2+(int)s[i])%mod2 ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    memset(P,0,sizeof(P)) ;
    memset(P2,0,sizeof(P2)) ;
    P[0] = 1 ;
    P2[0] = 1 ;
    for(int i = 1 ; i < 5050 ; i++){
        P[i] = (P[i-1]*p1)%mod1 ;
        P2[i] = (P2[i-1]*p2)%mod2;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) && (add2(F2[j],mul2((i>0)?F2[i-1]:0,P2[j-i+1]),-1) == add2(B2[i],mul2((j+1<sz)?B2[j+1]:0,P2[j-i+1]),-1)) ){
                pal[i][j] = 1 ;
            }
        }
        //cout << i << el ;
    }
    // clock_t end = clock() ;
    // cout << end-begin << el ;
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll pal[5002][5002] ;
 ll dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
        dp[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)){
                dp[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){
            if (i > j) continue ;
            if (j == 0){
                dp[i][j] = pal[i][j] ;
            }
            else{
                dp[i][j] = dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]+pal[i][j] ;
            }
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 9000001 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*(b%mod1)+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*(b%mod2)+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(F[i],mul1(F[j+1],pow1(p1,j-i+1)),-1)){
                pal[i][j] = 1 ;
            }
        }
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 909327 ;
 ll p1 = 1799 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return (a*b)%mod1 ;
 }
 ll mul2(ll a, ll b){
     return (a*b)%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow1(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return mul1(temp,temp) ;
 }
 ll pow2(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow2(a,x/2) ;
     if (x&1){
         return mul2(mul2(temp,temp),a) ;
     }
     return mul2(temp,temp) ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    //clock_t begin = clock() ;
    string s ; cin >> s ;

    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
        F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
        B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    memset(P,0,sizeof(P)) ;
    P[0] = 1 ;
    for(int i = 1 ; i < 5050 ; i++){
        P[i] = (P[i-1]*p1)%mod1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) ){
                pal[i][j] = 1 ;
            }
        }
        //cout << i << el ;
    }
    // clock_t end = clock() ;
    // cout << end-begin << el ;
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
        dp[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)){
                pal[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){

                dp[i][j] = dp[i+1][j]+pal[i][j] ;
            }
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999931 ;
 ll p1 = 953 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return (a*b)%mod1 ;
 }
 ll mul2(ll a, ll b){
     return (a*b)%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow1(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return mul1(temp,temp) ;
 }
 ll pow2(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow2(a,x/2) ;
     if (x&1){
         return mul2(mul2(temp,temp),a) ;
     }
     return mul2(temp,temp) ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    //clock_t begin = clock() ;
    string s ; cin >> s ;

    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
        F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
        B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    memset(P,0,sizeof(P)) ;
    P[0] = 1 ;
    for(int i = 1 ; i < 5050 ; i++){
        P[i] = (P[i-1]*p1)%mod1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) ){
                pal[i][j] = 1 ;
            }
        }
        //cout << i << el ;
    }
    // clock_t end = clock() ;
    // cout << end-begin << el ;
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }
/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999931 ;
 ll p1 = 953 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return (a*b)%mod1 ;
 }
 ll mul2(ll a, ll b){
     return (a*b)%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow1(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return mul1(temp,temp) ;
 }
 ll pow2(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow2(a,x/2) ;
     if (x&1){
         return mul2(mul2(temp,temp),a) ;
     }
     return mul2(temp,temp) ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    //clock_t begin = clock() ;
    string s ; cin >> s ;

    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
        F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
        B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    memset(P,0,sizeof(P)) ;
    P[0] = 1 ;
    for(int i = 1 ; i < 5050 ; i++){
        P[i] = (P[i-1]*p1)%mod1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) ){
                pal[i][j] = 1 ;
            }
        }
        //cout << i << el ;
    }
    // clock_t end = clock() ;
    // cout << end-begin << el ;
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 909327 ;
 ll p1 = 1799 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return (a*b)%mod1 ;
 }
 ll mul2(ll a, ll b){
     return (a*b)%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow1(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return mul1(temp,temp) ;
 }
 ll pow2(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow2(a,x/2) ;
     if (x&1){
         return mul2(mul2(temp,temp),a) ;
     }
     return mul2(temp,temp) ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 ll P2[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    //clock_t begin = clock() ;
    string s ; cin >> s ;

    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
        F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
        B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    memset(P,0,sizeof(P)) ;
    memset(P2,0,sizeof(P2)) ;
    P[0] = 1 ;
    P2[0] = 1 ;
    for(int i = 1 ; i < 5050 ; i++){
        P[i] = (P[i-1]*p1)%mod1 ;
        P2[i] = (P2[i-1]*p2)%mod2;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) && (add2(F2[j],mul2((i>0)?F2[i-1]:0,P2[j-i+1]),-1) == add2(B2[i],mul2((j+1<sz)?B2[j+1]:0,P2[j-i+1]),-1)) ){
                pal[i][j] = 1 ;
            }
        }
        //cout << i << el ;
    }
    // clock_t end = clock() ;
    // cout << end-begin << el ;
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
        dp[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)){
                pal[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){

                dp[i][j] = dp[i+1][j]+pal[i][j] ;
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 909327 ;
 ll p1 = 1799 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;
 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 ll P2[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;
 string s ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b%mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 void init(){
     memset(pal,0,sizeof(pal)) ;
     memset(dp,0,sizeof(dp)) ;

     int sz = s.size() ;
     for(int i = 0 ; i < sz; i++){
         pal[i][i] = 1 ;
     }

     F[0] = (int)s[0] ;
     F2[0] = (int)s[0] ;
     for(int i = 1 ; i < s.size() ; i++){
         F[i] = ((F[i-1]*p1)%mod1+(int)s[i])%mod1  ;
         F2[i] = ((F2[i-1]*p2)%mod2+(int)s[i])%mod2 ;
     }

     B[sz-1] = (int)s[sz-1];
     B2[sz-1] = (int)s[sz-1] ;
     for(int i = sz-2 ; i >= 0 ; i--){
         B[i] = ((B[i+1]*p1)%mod1+(int)s[i])%mod1 ;
         B2[i] = ((B2[i+1]*p2)%mod2+(int)s[i])%mod2 ;
     }

     memset(P,0,sizeof(P)) ;
     memset(P2,0,sizeof(P2)) ;
     P[0] = 1 ;
     P2[0] = 1 ;
     for(int i = 1 ; i < 5050 ; i++){
         P[i] = (P[i-1]*p1)%mod1 ;
         P2[i] = (P2[i-1]*p2)%mod2;
     }
 }

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    cin >> s ;

    int sz = s.size() ;
    init() ;

    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) && (add2(F2[j],mul2((i>0)?F2[i-1]:0,P2[j-i+1]),-1) == add2(B2[i],mul2((j+1<sz)?B2[j+1]:0,P2[j-i+1]),-1)) ){
                pal[i][j] = 1 ;
            }
        }
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999931 ;
 ll p1 = 953 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return (a*b)%mod1 ;
 }
 ll mul2(ll a, ll b){
     return (a*b)%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow1(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return mul1(temp,temp) ;
 }
 ll pow2(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow2(a,x/2) ;
     if (x&1){
         return mul2(mul2(temp,temp),a) ;
     }
     return mul2(temp,temp) ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    //clock_t begin = clock() ;
    string s ; cin >> s ;

    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
        F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
        B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    memset(P,0,sizeof(P)) ;
    P[0] = 1 ;
    for(int i = 1 ; i < 5050 ; i++){
        P[i] = (P[i-1]*p1)%mod1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) ){
                pal[i][j] = 1 ;
            }
        }
        //cout << i << el ;
    }
    // clock_t end = clock() ;
    // cout << end-begin << el ;
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 999983 ;
 ll p1 = 971 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a%mod1+fl*b%mod1+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a%mod2+fl*b%mod2+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b*mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 ll pow1(ll a, ll x){
     if(x == 0) return 1LL ;
     ll temp = pow(a,x/2) ;
     if (x&1){
         return mul1(mul1(temp,temp),a) ;
     }
     return temp ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    string s ; cin >> s ;
    F[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
    }

    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        dp[i][i] = 1 ;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i ; j < sz ; j++){
            if (add1(F[j],mul1(F[i-1],pow1(p1,j-i+1)),-1) == add1(B[i],mul1(B[j+1],pow1(p1,j-i+1)),-1)){
                dp[i][j] = 1 ;
            }
        }
    }

    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j < sz ; j++){
            if (i > j) continue ;
            if (j == 0){
                dp[i][j] = pal[i][j] ;
            }
            else{
                dp[i][j] += dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1] ;
            }
        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ;
        cin >> x >> y ; x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>
////
 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 909327 ;
 ll p1 = 1799 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return (a*b)%mod1 ;
 }
 ll mul2(ll a, ll b){
     return (a*b)%mod2 ;
 }

 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 ll P2[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    //clock_t begin = clock() ;
    string s ; cin >> s ;

    F[0] = (int)s[0] ;
    F2[0] = (int)s[0] ;
    for(int i = 1 ; i < s.size() ; i++){
        F[i] = ((F[i-1]*p1)%mod1+(int)s[i])%mod1  ;
        F2[i] = ((F2[i-1]*p2)%mod2+(int)s[i])%mod2 ;
    }
    int sz = s.size() ;
    B[sz-1] = (int)s[sz-1];
    B2[sz-1] = (int)s[sz-1] ;
    for(int i = sz-2 ; i >= 0 ; i--){
        B[i] = ((B[i+1]*p1)%mod1+(int)s[i])%mod1 ;
        B2[i] = ((B2[i+1]*p2)%mod2+(int)s[i])%mod2 ;
    }

    memset(pal,0,sizeof(pal)) ;
    memset(dp,0,sizeof(dp)) ;
    for(int i = 0 ; i < sz; i++){
        pal[i][i] = 1 ;
    }
    memset(P,0,sizeof(P)) ;
    memset(P2,0,sizeof(P2)) ;
    P[0] = 1 ;
    P2[0] = 1 ;
    for(int i = 1 ; i < 5050 ; i++){
        P[i] = (P[i-1]*p1)%mod1 ;
        P2[i] = (P2[i-1]*p2)%mod2;
    }
    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) && (add2(F2[j],mul2((i>0)?F2[i-1]:0,P2[j-i+1]),-1) == add2(B2[i],mul2((j+1<sz)?B2[j+1]:0,P2[j-i+1]),-1)) ){
                pal[i][j] = 1 ;
            }
        }
        //cout << i << el ;
    }
    // clock_t end = clock() ;
    // cout << end-begin << el ;
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }


/* I solemnly swear that I am upto no good. */
 #include <iostream>
 #include <cstring>
 #include <vector>
 #include <map>
 #include <set>
 #include <stack>
 #include <queue>
 #include <deque>
 #include <sstream>
 #include <iomanip>
 #include <cassert>
 #include <sys/time.h>
 #include <algorithm>
 #include <bitset>
 #include <cmath>
 #include <functional>
 #include <cmath>

 using namespace std ;

 #define xx first
 #define yy second
 #define ll long long
 #define ld long double
 #define el (char)0x0A
 #define sp " "
 #define pb push_back
 #define mp make_pair
 #define all(x) (x).begin(),(x).end()
 #define clr(x) memset((x),0,sizeof(x))
 #define uint unsigned int
 #define abs(x) ((x<=0)?(-x):(x))
 #define MAXN 100005
 #define INF (int)1E10+1
 #define MOD (int)1E9+7
 #define last_set_bit(x) log2((x)&(-x))
 #define add(x,y) (x)=((x)+(y))%MOD

 typedef pair<int,int> PII ; typedef vector<int> VI ; typedef vector<vector<int> > VVI ; typedef vector<bool> VB ; typedef vector<string> VS ;
 typedef map<int,int> MII ; typedef map<int,VI > MIV ; typedef set<int> SI ; typedef set<PII > SPII ; typedef vector<PII > VPII ; typedef
 vector<VPII > VVPII ;

 ll mod1 = 909327 ;
 ll p1 = 1799 ;
 ll mod2 = 999931 ;
 ll p2 = 953 ;
 ll F[5002] ;
 ll B[5002] ;
 ll F2[5002] ;
 ll B2[5002] ;
 ll P[5050] ;
 ll P2[5050] ;
 char pal[5002][5002] ;
 int dp[5002][5002] ;
 string s ;

 ll add1(ll a, ll b, int fl){
     return (a+fl*b+mod1)%mod1 ;
 }
 ll add2(ll a, ll b, int fl){
     return (a+fl*b+mod2)%mod2 ;
 }
 ll mul1(ll a , ll b){
     return ((a%mod1)*(b%mod1))%mod1 ;
 }
 ll mul2(ll a, ll b){
     return ((a%mod2)*(b%mod2))%mod2 ;
 }
 void init(){
     memset(pal,0,sizeof(pal)) ;
     memset(dp,0,sizeof(dp)) ;

     int sz = s.size() ;
     for(int i = 0 ; i < sz; i++){
         pal[i][i] = 1 ;
     }

     F[0] = (int)s[0] ;
     F2[0] = (int)s[0] ;
     for(int i = 1 ; i < s.size() ; i++){
         F[i] = add1(mul1(F[i-1],p1),(int)s[i],1)  ;
         F2[i] = add2(mul2(F2[i-1],p2),(int)s[i],1) ;
     }

     B[sz-1] = (int)s[sz-1];
     B2[sz-1] = (int)s[sz-1] ;
     for(int i = sz-2 ; i >= 0 ; i--){
         B[i] = add1(mul1(B[i+1],p1),(int)s[i],1) ;
         B2[i] = add2(mul2(B2[i+1],p2),(int)s[i],1) ;
     }

     memset(P,0,sizeof(P)) ;
     memset(P2,0,sizeof(P2)) ;
     P[0] = 1 ;
     P2[0] = 1 ;
     for(int i = 1 ; i < 5050 ; i++){
         P[i] = (P[i-1]*p1)%mod1 ;
         P2[i] = (P2[i-1]*p2)%mod2;
     }
 }

 int main(){
 	ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;

    cin >> s ;

    int sz = s.size() ;
    init() ;

    for(int i = 0 ; i < sz ; i++){
        for(int j = i+1 ; j < sz ; j++){
            if (add1(F[j],mul1((i>0)?F[i-1]:0,P[j-i+1]),-1) == add1(B[i],mul1((j+1<sz)?B[j+1]:0,P[j-i+1]),-1) && (add2(F2[j],mul2((i>0)?F2[i-1]:0,P2[j-i+1]),-1) == add2(B2[i],mul2((j+1<sz)?B2[j+1]:0,P2[j-i+1]),-1)) ){
                pal[i][j] = 1 ;
            }
        }
    }
    for(int i = sz-1 ; i >= 0 ; i--){
        for(int j = i ; j<sz ; j++){
            if (j == 0) dp[i][j] = pal[i][j] ;
            else{
                dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] + pal[i][j] ;
            }

        }
    }

    int q ; cin >> q ;
    while (q--){
        int x , y ; cin >> x >> y ;
        x-- ; y-- ;
        cout << dp[x][y] << el ;
    }

 }