#include<bits/stdc++.h>
//#define nguyendz the_best

#define ll int
#define ld long double
#define pb push_back
#define all(n) n.begin(),n.end()
/*#define eb emplace_back
#define endl "\n"
#define pll pair<ll,ll>
#define YES cout <<"YES"<<endl;
#define NO cout <<"NO"<<endl;
#define ff first
#define ss second*/

//#define ((x|y)-y) (x&(~y))
//#define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn=5e3+500;
/*const ll maxx=1e13;
const ll mod=1e9+7;
const ll mod1=998244353;*/
//priority_queue<pll,vector<pll>,greater<pll>> q;
vector <ll> adj[maxn];
ll kc,t,n,m,k;

ll par[maxn];
ll sz[maxn];
ll ans;
ll pos;
bool dd[maxn];
ll bk[maxn];
ll  nextInt() {
    ll x = 0, p = 1;
    char c;
    do {
        c = getchar();
    } while (c <= 32);
    if (c == '-') {
        p = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * p;
}
ll kt[maxn][maxn];
ll dp[maxn][maxn];
int main()
{
    // freopen("test.txt","r",stdin);
    // freopen("CHEMIST.OUT","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   string s;
   cin>>s;
   ll n=s.length();
   s=" "+s;
   for (int i=1;i<=n;i++)
   {
       kt[i][i]=1;
       dp[i][i]=1;
   }
   for (int i=1;i<n;i++)
   {
       if (s[i]==s[i+1])
       {
           kt[i][i+1]=1;
       }
   }
   for (int i=1;i<=n;i++)
   {
       for (int j=2;j+i-1<=n;j++)
       {
           if (kt[i+1][i+j-2]&&s[i]==s[i+j-1])
           {
               kt[i][i+j-1]=1;
           }
       }
   }
   //cout <<kt[4][6];
  for (int len=2;len<=n;len++)
  {
      for (int start=1;start<=n-len+1;start++)
      {
          dp[start][start+len-1]=dp[start+1][start+len-1]+dp[start][start+len-2]-dp[start+1][start+len-2]+kt[start][start+len-1];
      }
  }
   ll q;
   cin>>q;
   while (q--)
   {
       ll x,y;
       cin>>x>>y;
       cout <<dp[x][y]<<endl;
   }

}


#include<bits/stdc++.h>
#define nguyendz the_best

#define ll int
#define ld long double
#define pb push_back
#define all(n) n.begin(),n.end()
#define eb emplace_back
#define endl "\n"
#define pll pair<ll,ll>
#define YES cout <<"YES"<<endl;
#define NO cout <<"NO"<<endl;
#define ff first
#define ss second

//#define ((x|y)-y) (x&(~y))
//#define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn=5e3+3;
const ll maxx=1e13;
const ll mod=1e9+7;
const ll mod1=998244353;

vector <ll> adj[maxn];
ll kc,t,n,m,k;

bool kt[maxn][maxn];
ll dp[maxn][maxn];
int main()
{
    // freopen("test.txt","r",stdin);
    // freopen("CHEMIST.OUT","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   string s;
   cin>>s;
    n=s.length();

   for (int i=0;i<=n;i++)
   {
       kt[i][i]=1;
       dp[i][i]=1;
       kt[i+1][i]=1;
   }

  for (int len=2;len<=n;len++)
  {
      for (int start=0;start<n-len+1;start++)
      {

               kt[start][start+len-1]=kt[start+1][start+len-2]&(s[start]==s[start+len-1]);

          dp[start][start+len-1]=dp[start+1][start+len-1]+dp[start][start+len-2]-dp[start+1][start+len-2]+kt[start][start+len-1];
      }
  }
   ll q;
   cin>>q;
   while (q--)
   {
       ll x,y;

       cin>>x>>y;
       x--;
       y--;
       cout <<dp[x][y]<<endl;
   }

}


#include<bits/stdc++.h>
//#define nguyendz the_best

#define ll int
#define ld long double
#define pb push_back
#define all(n) n.begin(),n.end()
/*#define eb emplace_back
#define endl "\n"
#define pll pair<ll,ll>
#define YES cout <<"YES"<<endl;
#define NO cout <<"NO"<<endl;
#define ff first
#define ss second*/

//#define ((x|y)-y) (x&(~y))
//#define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn=5e3+500;
/*const ll maxx=1e13;
const ll mod=1e9+7;
const ll mod1=998244353;*/
//priority_queue<pll,vector<pll>,greater<pll>> q;
vector <ll> adj[maxn];
ll kc,t,n,m,k;

ll par[maxn];
ll sz[maxn];
ll ans;
ll pos;
bool dd[maxn];
ll bk[maxn];
ll  nextInt() {
    ll x = 0, p = 1;
    char c;
    do {
        c = getchar();
    } while (c <= 32);
    if (c == '-') {
        p = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * p;
}
ll kt[maxn][maxn];
ll dp[maxn][maxn];
int main()
{
    // freopen("test.txt","r",stdin);
    // freopen("CHEMIST.OUT","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   string s;
   cin>>s;
   ll n=s.length();
   s=" "+s;
   for (int i=1;i<=n;i++)
   {
       kt[i][i]=1;
       dp[i][i]=1;
   }
   for (int i=1;i<n;i++)
   {
       if (s[i]==s[i+1])
       {
           kt[i][i+1]=1;
           dp[i][i+1]=3;
       }
   }
   for (int i=1;i<=n;i++)
   {
       for (int j=2;j+i-1<=n;j++)
       {
           if (kt[i+1][i+j-2]&&s[i]==s[i+j-1])
           {
               kt[i][i+j-1]=1;
           }
       }
   }
   //cout <<kt[4][6];
  for (int len=2;len<=n;len++)
  {
      for (int start=1;start<=n-len+1;start++)
      {
          dp[start][start+len-1]=dp[start+1][start+len-1]+dp[start][start+len-2]-dp[start+1][start+len-2]+kt[start][start+len-1];
      }
  }
   ll q;
   cin>>q;
   while (q--)
   {
       ll x,y;
       cin>>x>>y;
       cout <<dp[x][y]<<endl;
   }

}


#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pcc = pair<char, char>;

const ll inf = 9999999999;

//int dr[] = {-1, 0, 1,  0};// up, right, down, left
//int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};

const int sz = 5005;

string str;
int pal[sz][sz];
int dp[sz][sz];

int check(int l, int r)
{
    if(l>=r) return pal[l][r] = 1;
    if(l+1 == r) return pal[l][r] = (str[l] == str[r]);
    if(pal[l][r] != -1) return pal[l][r];

    if(str[l] == str[r] && check(l+1, r-1))
        return pal[l][r] = 1;
    return pal[l][r] = 0;
}

int main()
{
    int q;
    memset(pal, -1, sizeof pal);
    memset(dp, 0, sizeof dp);
    cin>>str;
    int len = str.size();

    for(int i=0; i<=len; i++) dp[i][i] = 1;

    for(int i=2; i<=len; i++){
        for(int j=0; j<=len-i; j++){
            dp[j][i+j-1] = dp[j][i+j-2]+dp[j+1][i+j-1]
            -dp[j+1][i+j-2]+check(j, i+j-1);
        }
    }
    scanf("%d", &q);
    while(q--){
        int l, r;
        scanf("%d%d", &l, &r);
        printf("%d\n", dp[l-1][r-1]);
    }

    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pcc = pair<char, char>;

const ll inf = 9999999999;

//int dr[] = {-1, 0, 1,  0};// up, right, down, left
//int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};

const int sz = 5005;

string str;
int pal[sz][sz];
int dp[sz][sz];

int check(int l, int r)
{
    if(l>=r) return pal[l][r] = 1;
    if(pal[l][r] != -1) return pal[l][r];

    if(str[l] == str[r] && check(l+1, r-1))
        return pal[l][r] = 1;
    return pal[l][r] = 0;
}

int main()
{
    int q;
    memset(pal, -1, sizeof pal);
    memset(dp, 0, sizeof dp);
    cin>>str;
    int len = str.size();

    for(int i=0; i<=len; i++) dp[i][i] = 1;

    for(int i=1; i<=len; i++){
        int k = i;
        for(int j = 1; j<=len; j++, k++){
            int mid = (j+k+1)/2;
            //printf("%d,%d = %d %d + %d %d\n", j, k+1, j, mid, mid, k+1);
            dp[j][k+1] = dp[j][mid]+dp[mid][k+1]+check(j-1, k);
        }
    }
    cin>>q;
    while(q--){
        int l, r;
        cin>>l>>r;
        if(l == r) cout << dp[l][r] <<endl;
        else cout << dp[l][r]+1 <<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pcc = pair<char, char>;

const ll inf = 9999999999;

//int dr[] = {-1, 0, 1,  0};// up, right, down, left
//int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};

const int sz = 5005;

string str;
int pal[sz][sz];
int dp[sz][sz];

int check(int l, int r)
{
    if(l>=r) return pal[l][r] = 1;
    if(pal[l][r] != -1) return pal[l][r];

    if(str[l] == str[r] && check(l+1, r-1))
        return pal[l][r] = 1;
    return pal[l][r] = 0;
}

int main()
{
    int q;
    memset(pal, -1, sizeof pal);
    memset(dp, 0, sizeof dp);
    cin>>str;
    int len = str.size();

    for(int i=0; i<=len; i++) dp[i][i] = 1;

    for(int i=2; i<=len; i++){
        for(int j=0; j<=len-i; j++){
            dp[j][i+j-1] = dp[j][i+j-2]+dp[j+1][i+j-1]
            -dp[j+1][i+j-2]+check(j, i+j-1);
        }
    }
    cin>>q;
    while(q--){
        int l, r;
        cin>>l>>r;
        cout << dp[l-1][r-1] <<endl;
    }

    return 0;
}


#include <iostream>

using namespace std;

const int MAXN = 5000 + 10;

string s;
int n, q, dp[MAXN][MAXN];
bool isPal[MAXN][MAXN];

int main()
{
    ios::sync_with_stdio(0);
    cin >> s;
    n = (int)s.size();
    for (int i = 0 ; i < n ; i++)
    {
        isPal[i][i] = 1;
        dp[i][i] = 1;
        isPal[i+1][i] = 1;
    }
    for (int len = 2 ; len <= n ; len++)
        for (int start = 0 ; start <= n-len ; start++)
        {
            isPal[start][start+len-1] = isPal[start+1][start+len-2] & s[start] == s[start+len-1];
            dp[start][start+len-1] = dp[start][start+len-2] + dp[start+1][start+len-1] - dp[start+1][start+len-2] + isPal[start][start+len-1];
        }
    cin >> q;
    for (int i = 1 ; i <= q ; i++)
    {
        int l, r;
        cin >> l >> r;
        l--;
        r--;
        cout << dp[l][r] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define pi acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()

//#define inf 1<<30

// isupper() -> upper case check
// tolower() -> convert upper to lower case

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pcc = pair<char, char>;

const ll inf = 9999999999;

//int dr[] = {-1, 0, 1,  0};// up, right, down, left
//int dc[] = { 0, 1, 0, -1};

// King moves similar to 8 direction
int dr[] = {-1, -1, 0, 1, 1,  1, 0, -1};
int dc[] = { 0,  1, 1, 1, 0, -1,-1, -1};

// Knight moves
int kr[] = {-1,  1, -2, -2, -1, 1,  2,  2};
int kc[] = {-2, -2, -1,  1,  2, 2, -1,  1};

const int sz = 5005;

string str;
int pal[sz][sz];
int dp[sz][sz];

int check(int l, int r)
{
    if(l>=r) return pal[l][r] = 1;
    if(pal[l][r] != -1) return pal[l][r];

    if(str[l] == str[r] && check(l+1, r-1))
        return pal[l][r] = 1;
    return pal[l][r] = 0;
}

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) ;
    int q;
    memset(pal, -1, sizeof pal);
    memset(dp, 0, sizeof dp);
    cin>>str;
    int len = str.size();

    for(int i=0; i<=len; i++) dp[i][i] = 1;

    for(int i=2; i<=len; i++){
        for(int j=0; j<=len-i; j++){
            dp[j][i+j-1] = dp[j][i+j-2]+dp[j+1][i+j-1]
            -dp[j+1][i+j-2]+check(j, i+j-1);
        }
    }
    cin>>q;
    while(q--){
        int l, r;
        cin>>l>>r;
        cout << dp[l-1][r-1] <<endl;
    }

    return 0;
}


// I love meowmur
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(a) (a).begin(), (a).end()
#define sz(a) (ll)((a).size())
#define heap priority_queue
#define hash_map unordered_map
#define hash_set unordered_set
#define ft first
#define sd second
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define mineq(a, b) (a) = ((a)>(b))?(b):(a)
#define maxeq(a, b) (a) = ((a)<(b))?(b):(a)
#define die(cond) if (cond) for(;;){}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef vector<ll> vll;
typedef set<ll> sll;
typedef map<ll, ll> mll;
const ll inf = numeric_limits<ll>::max() / 2;
const ld eps = 1e-9;
const ld pi = acos(-1);

inline void solve(), read();

int main() {
	fast;
	read();
	solve();
	return 0;
}

ll n;
string s;
ll q;
vector<vll> pal, dp;

inline void solve() {
	pal.resize(n, vll(n, 0));
	dp.resize(n, vll(n, 0));
	for (ll i = 0; i < n; i++) {
		pal[i][i] = 1;
		if (i + 1 < n) {
			if (s[i] == s[i + 1]) {
				pal[i][i + 1] = 1;
			}
		}
	}
	for (ll i = 0; i < n; i++) {
		dp[i][i] = 1;
		if (i + 1 < n) {
			dp[i][i + 1] = 2 + pal[i][i + 1];
		}
	}
	for (ll m = 3; m <= n; m++) {
		for (ll i = 0; i < n; i++) {
			ll j = i + m - 1;
			if (j >= n) {
				break;
			}
			if (s[i] == s[j] && pal[i + 1][j - 1]) {
				pal[i][j] = 1;
			} else {
				pal[i][j] = 0;
			}
			dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + pal[i][j];
		}
	}
	cin >> q;
	for (ll i = 0; i < q; i++) {
		ll l, r;
		cin >> l >> r;
		--l, --r;
		cout << dp[l][r] << endl;
	}
}

inline void read() {
	cin >> s;
	n = sz(s);
}


// I love meowmur
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(a) (a).begin(), (a).end()
#define sz(a) (ll)((a).size())
#define heap priority_queue
#define hash_map unordered_map
#define hash_set unordered_set
#define ft first
#define sd second
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define mineq(a, b) (a) = ((a)>(b))?(b):(a)
#define maxeq(a, b) (a) = ((a)<(b))?(b):(a)
#define die(cond) if (cond) for(;;){}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef vector<ll> vll;
typedef set<ll> sll;
typedef map<ll, ll> mll;
const ll inf = numeric_limits<ll>::max() / 2;
const ld eps = 1e-9;
const ld pi = acos(-1);

inline void solve(), read();

int main() {
	fast;
	read();
	solve();
	return 0;
}

ll n;
string s;
ll q;
vector<vll> pal, dp;

inline void solve() {
	pal.resize(n, vll(n, 0));
	for (ll i = 0; i < n; i++) {
		for (ll j = 0; j < n; j++) {
			ll a = i - j, b = i + j;
			if (a < 0 || b >= n) {
				break;
			}
			if (s[a] != s[b]) {
				break;
			}
			pal[a][b] = 1;
		}
	}
	for (ll i = 0; i < n - 1; i++) {
		for (ll j = 0; j < n; j++) {
			ll a = i - j, b = i + 1 + j;
			if (a < 0 || b >= n) {
				break;
			}
			if (s[a] != s[b]) {
				break;
			}
			pal[a][b] = 1;
		}
	}
	dp.resize(n, vll(n, 0));
	for (ll i = 0; i < n; i++) {
		dp[i][i] = 1;
		if (i < n - 1) {
			dp[i][i + 1] = 2 + pal[i][i + 1];
		}
	}
	for (ll m = 3; m < n; m++) {
		for (ll i = 0; i < n; i++) {
			ll j = i + m - 1;
			if (j >= n) {
				break;
			}
			dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + pal[i][j];
		}
	}
	cin >> q;
	for (ll i = 0; i < q; i++) {
		ll l, r;
		cin >> l >> r;
		--l, --r;
		cout << dp[l][r] << endl;
	}
}

inline void read() {
	cin >> s;
	n = sz(s);
}


// I love meowmur
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(a) (a).begin(), (a).end()
#define sz(a) (ll)((a).size())
#define heap priority_queue
#define hash_map unordered_map
#define hash_set unordered_set
#define ft first
#define sd second
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define mineq(a, b) (a) = ((a)>(b))?(b):(a)
#define maxeq(a, b) (a) = ((a)<(b))?(b):(a)
#define die(cond) if (cond) for(;;){}
using namespace std;
typedef int ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef vector<ll> vll;
typedef set<ll> sll;
typedef map<ll, ll> mll;
const ll inf = numeric_limits<ll>::max() / 2;
const ld eps = 1e-9;
const ld pi = acos(-1);

inline void solve(), read();

int main() {
	fast;
	read();
	solve();
	return 0;
}

const ll MAXN = 5e3 + 10;

ll n;
string s;
ll q;
ll pal[MAXN][MAXN], dp[MAXN][MAXN];

inline void solve() {
	for (ll i = 0; i < n; i++) {
		pal[i][i] = 1;
		if (i + 1 < n) {
			if (s[i] == s[i + 1]) {
				pal[i][i + 1] = 1;
			}
		}
	}
	for (ll i = 0; i < n; i++) {
		dp[i][i] = 1;
		if (i + 1 < n) {
			dp[i][i + 1] = 2 + pal[i][i + 1];
		}
	}
	for (ll m = 3; m <= n; m++) {
		for (ll i = 0; i < n; i++) {
			ll j = i + m - 1;
			if (j >= n) {
				break;
			}
			if (s[i] == s[j] && pal[i + 1][j - 1]) {
				pal[i][j] = 1;
			} else {
				pal[i][j] = 0;
			}
			dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + pal[i][j];
		}
	}
	cin >> q;
	for (ll i = 0; i < q; i++) {
		ll l, r;
		cin >> l >> r;
		--l, --r;
		cout << dp[l][r] << endl;
	}
}

inline void read() {
	cin >> s;
	n = sz(s);
}


// I love meowmur
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(a) (a).begin(), (a).end()
#define sz(a) (ll)((a).size())
#define heap priority_queue
#define hash_map unordered_map
#define hash_set unordered_set
#define ft first
#define sd second
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define mineq(a, b) (a) = ((a)>(b))?(b):(a)
#define maxeq(a, b) (a) = ((a)<(b))?(b):(a)
#define die(cond) if (cond) for(;;){}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef vector<ll> vll;
typedef set<ll> sll;
typedef map<ll, ll> mll;
const ll inf = numeric_limits<ll>::max() / 2;
const ld eps = 1e-9;
const ld pi = acos(-1);

inline void solve(), read();

int main() {
	fast;
	read();
	solve();
	return 0;
}

ll n;
string s;
ll q;
vector<vll> pal, dp;

inline void solve() {
	pal.resize(n, vll(n, 0));
	pal.resize(n, vll(n, 0));
	dp.resize(n, vll(n, 0));
	for (ll i = 0; i < n; i++) {
		pal[i][i] = 1;
		if (i + 1 < n) {
			if (s[i] == s[i + 1]) {
				pal[i][i + 1] = 1;
			}
		}
	}
	for (ll i = 0; i < n; i++) {
		dp[i][i] = 1;
		if (i + 1 < n) {
			dp[i][i + 1] = 2 + pal[i][i + 1];
		}
	}
	for (ll m = 3; m < n; m++) {
		for (ll i = 0; i < n; i++) {
			ll j = i + m - 1;
			if (j >= n) {
				break;
			}
			if (s[i] == s[j] && pal[i + 1][j - 1]) {
				pal[i][j] = 1;
			}
			dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + pal[i][j];
		}
	}
	cin >> q;
	for (ll i = 0; i < q; i++) {
		ll l, r;
		cin >> l >> r;
		--l, --r;
		cout << dp[l][r] << endl;
	}
}

inline void read() {
	cin >> s;
	n = sz(s);
}


// I love meowmur
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(a) (a).begin(), (a).end()
#define sz(a) (ll)((a).size())
#define heap priority_queue
#define hash_map unordered_map
#define hash_set unordered_set
#define ft first
#define sd second
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define mineq(a, b) (a) = ((a)>(b))?(b):(a)
#define maxeq(a, b) (a) = ((a)<(b))?(b):(a)
#define die(cond) if (cond) for(;;){}
using namespace std;
typedef int ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef vector<ll> vll;
typedef set<ll> sll;
typedef map<ll, ll> mll;
const ll inf = numeric_limits<ll>::max() / 2;
const ld eps = 1e-9;
const ld pi = acos(-1);

inline void solve(), read();

int main() {
	fast;
	read();
	solve();
	return 0;
}

const ll MAXN = 5e3 + 10;
ll n;
string s;
ll q;
ll pal[MAXN][MAXN], dp[MAXN][MAXN];

inline void solve() {
	for (ll i = 0; i < n; i++) {
		for (ll j = 0; j < n; j++) {
			ll a = i - j, b = i + j;
			if (a < 0 || b >= n) {
				break;
			}
			if (s[a] != s[b]) {
				break;
			}
			pal[a][b] = 1;
		}
	}
	for (ll i = 0; i < n - 1; i++) {
		for (ll j = 0; j < n; j++) {
			ll a = i - j, b = i + 1 + j;
			if (a < 0 || b >= n) {
				break;
			}
			if (s[a] != s[b]) {
				break;
			}
			pal[a][b] = 1;
		}
	}
	for (ll i = 0; i < n; i++) {
		dp[i][i] = 1;
		if (i < n - 1) {
			dp[i][i + 1] = 2 + pal[i][i + 1];
		}
	}
	for (ll m = 3; m <= n; m++) {
		for (ll i = 0; i < n; i++) {
			ll j = i + m - 1;
			if (j >= n) {
				break;
			}
			dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + pal[i][j];
		}
	}
	cin >> q;
	for (ll i = 0; i < q; i++) {
		ll l, r;
		cin >> l >> r;
		--l, --r;
		cout << dp[l][r] << endl;
	}
}

inline void read() {
	cin >> s;
	n = sz(s);
}


#include <bits/stdc++.h>

using namespace std;

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//
//using namespace __gnu_pbds;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

//class Timer {
//    clock_t start;
//    string name;
//public:
//    Timer() {name = "";start = clock();}
//    Timer(string s){name = s;start = clock();}
//    ~Timer() {fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0*(clock() - start) / CLOCKS_PER_SEC);}
//};

//#define isValid(a, b) ((a) >= 0 && (a) < (b))
//int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
//int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define um unordered_map
#define us unordered_set
#define endl '\n'
#define what_is(x) cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl

const double EPS = 1e-9;
const double PI = acos(-1.0);

template<typename T> inline T sq(T a){return a*a;}
template<typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b){return make_pair(a, b);}
template<typename T1, typename T2> inline T1 safeMod(T1 a, T2 m){return (a%m + m)%m;}/*handling negative sign of remainder*/
template<typename T1, typename T2> inline bool isEq(T1 a, T2 b){return abs(a - b) < EPS;}
template<typename T1, typename T2, typename T3> inline bool isEq(T1 a, T2 b, T3 eps){return abs(a - b) < eps;}
template<typename T> inline bool isKthBitOn(T n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;return bool((n & (ONE<<k)));}
template<typename T> inline void setKthBit(T& n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;n = (n|(ONE<<k));}
//mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
//template<typename T> inline T nextRand(T lo, T hi){return uniform_int_distribution<T>(lo, hi)(rng);}

#define toRad(deg) ((deg)*PI/180.0)
#define toDeg(rad) ((rad)*180.0/PI)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const int MAX = 5005;
const int precision = 10;

const int MOD_PRIME = 589602329;
ll bigMod(ll b, int p)
{
    ll ret = 1;
    while(p) {
        if(p&1)
            ret = (ret*b)%MOD_PRIME;
        b = (b*b)%MOD_PRIME;
        p/=2;
    }
    return ret;
}

ll base_prime = 31;
ll powers_of_base_prime[MAX];
ll inv_powers_of_base_prime[MAX];
void init_calc()
{
    ll p = base_prime;
    for(int i = 0; i < MAX; i++) {
        powers_of_base_prime[i] = p;
        inv_powers_of_base_prime[i] = bigMod(base_prime, MOD_PRIME-i-2);
//        assert(powers_of_base_prime[i]*inv_powers_of_base_prime[i]%MOD_PRIME == 1);
        p = (p*base_prime)%MOD_PRIME;
    }
}

ll getHash(string &s, ll prefix_hash_sum_of_s[])
{
    ll hs = 0;
    int sz = s.size();
    for(int i = 0; i < sz; i++) {
        hs = (hs + (s[i]-'a'+1)*powers_of_base_prime[i]%MOD_PRIME)%MOD_PRIME;
        prefix_hash_sum_of_s[i] = hs;
    }
    return hs;
}

ll getHashOfSubString(string &s, int st, int len, ll prefix_hash_of_s[])
{
    int n = s.size();
    if(st + len > n)return -1;
    int en = st+len-1;
    ll hash_till_end = prefix_hash_of_s[en];
    ll hash_before_st = ((st-1) >= 0)?prefix_hash_of_s[st-1]:0;
    ll hash_of_substr = (hash_till_end-hash_before_st+MOD_PRIME)%MOD_PRIME;
    ll inv = ((st-1) >= 0)?inv_powers_of_base_prime[st-1]:1;
    hash_of_substr = (hash_of_substr*inv)%MOD_PRIME;
    return hash_of_substr;
}

ll triSum[MAX][MAX], recSum[MAX][MAX];
int main()
{
    //assert(freopen("in.txt", "r", stdin));
    //assert(freopen("out.txt", "w", stdout));
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    //cout << fixed << setprecision(precision);
    init_calc();
    string s;
    cin >> s;
    string r;
    r = s;
    reverse(all(r));
    int n = s.size();

    ll prefix_hash_sum_of_s[MAX];
    getHash(s, prefix_hash_sum_of_s);
    ll prefix_hash_sum_of_r[MAX];
    getHash(r, prefix_hash_sum_of_r);

    for(int len = 1; len <= n; len++){
        int si = 1;
        int sj = si+len-1;
        while(sj <= n){
            int rj = n-sj+1;
            triSum[len][si] = (getHashOfSubString(s, si-1, len, prefix_hash_sum_of_s) ==
                             getHashOfSubString(r, rj-1, len, prefix_hash_sum_of_r));
            recSum[len][si] = triSum[len][si];
            si++;
            sj++;
        }
    }
    for(int r = 1; r <= n; r++){
        int c = 1;
        int rr = r;
        ll sum = triSum[rr][c];
        rr--;
        c++;
        while(rr > 0){
            triSum[rr][c] += sum;
            sum = triSum[rr][c];
            rr--;
            c++;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n-i+1; j++){
            triSum[i][j] += triSum[i][j-1];
            recSum[i][j] += recSum[i][j-1];
        }
    }
    for(int j = 1; j <= n; j++){
        for(int i = 1; i <= n-j+1; i++){
            recSum[i][j] += recSum[i-1][j];
        }
    }
    int q;
    cin >> q;
    while(q--){
        int lf, rg;
        cin >> lf >> rg;
//        what_is(s.substr(lf-1, rg-lf+1));
//        what_is(r.substr(n-rg+1-1, rg-lf+1));
        int len = rg-lf+1;
        cout << triSum[1][rg] - triSum[1+len][rg-len] - recSum[1+len-1][rg-len] << endl;
        cout.flush();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sq(a) ((a) * (a))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define what_is(x) cerr << fixed << setprecision(2) << #x << " = " << x << endl
#define setbitj(n, j) (n |= (1 << (j)))
#define isbitjOn(n, j) (((n & (1 << (j))) > 0) ? 1 : 0)
#define safeMod(a, m) ((((a)%(m))+(m))%(m))/*handling negative sign of remainder*/

typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int MAX = 5005;
const int MOD = 1000000007;

inline bool isEq(double a, double b){return abs(a - b) < EPS;}
inline double toRad(double deg){return deg*PI/180;}
inline double toDeg(double rad){return rad*180/PI;}

//#define isValid(a, b) (a >= 0 && a < b)
//int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
//int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};
unsigned int prime = 37;
unsigned int powPrimes[MAX];
void genPows()
{
    powPrimes[0] = 1;
    for(int i = 1; i < MAX; i++)
        powPrimes[i] = powPrimes[i-1]*prime;
}

string s;
unsigned int forHashvals[MAX], revHashvals[MAX];
ll dp[MAX][MAX];
int main(int argc, char* argv[])
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    genPows();
    cin >> s;
    int len = s.length();

    forHashvals[0] = 0;
    for(int i = 0; i < len; i++){
        forHashvals[i+1] = forHashvals[i] + (powPrimes[i+1]*(s[i]-'a'+1));
        dp[i+1][i+1] = 1;
    }

    revHashvals[len+1] = 0;
    for(int i = len-1; i >= 0; i--){
        revHashvals[i+1] = revHashvals[i+2] + (powPrimes[len-i]*(s[i]-'a'+1));
    }

    int l, r;
    for(int lp = 2; lp <= len; lp++){//lp length of palindrome
        for(l = 1; l <= len-lp+1; l++){
            r = l+lp-1;
            unsigned int forvalue = forHashvals[r] - forHashvals[l-1];
            unsigned int revvalue = revHashvals[l] - revHashvals[r+1];
            int pl = l;
            int pr = len-r+1;
            if(pl > pr)
                revvalue *= powPrimes[pl-pr];
            else
                forvalue *= powPrimes[pr-pl];
            dp[l][l+lp-1] = dp[l][l+lp-2] + dp[l+1][l+lp-1] - dp[l+1][l+lp-2] + (forvalue==revvalue);
        }
    }

    int q;
    cin >> q;
    while(q--){
        cin >> l >> r;
        cout << dp[l][r] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//
//using namespace __gnu_pbds;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

//class Timer {
//    clock_t start;
//    string name;
//public:
//    Timer() {name = "";start = clock();}
//    Timer(string s){name = s;start = clock();}
//    ~Timer() {fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0*(clock() - start) / CLOCKS_PER_SEC);}
//};

//#define isValid(a, b) ((a) >= 0 && (a) < (b))
//int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
//int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define um unordered_map
#define us unordered_set
#define endl '\n'
#define what_is(x) cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl

const double EPS = 1e-9;
const double PI = acos(-1.0);

template<typename T> inline T sq(T a){return a*a;}
template<typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b){return make_pair(a, b);}
template<typename T1, typename T2> inline T1 safeMod(T1 a, T2 m){return (a%m + m)%m;}/*handling negative sign of remainder*/
template<typename T1, typename T2> inline bool isEq(T1 a, T2 b){return abs(a - b) < EPS;}
template<typename T1, typename T2, typename T3> inline bool isEq(T1 a, T2 b, T3 eps){return abs(a - b) < eps;}
template<typename T> inline bool isKthBitOn(T n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;return bool((n & (ONE<<k)));}
template<typename T> inline void setKthBit(T& n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;n = (n|(ONE<<k));}
//mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
//template<typename T> inline T nextRand(T lo, T hi){return uniform_int_distribution<T>(lo, hi)(rng);}

#define toRad(deg) ((deg)*PI/180.0)
#define toDeg(rad) ((rad)*180.0/PI)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const int MAX = 5005;
const int MOD = 1000000007;
const int precision = 10;

const int MOD_PRIME = 589602329;
ll bigMod(ll b, int p)
{
    ll ret = 1;
    while(p) {
        if(p&1)
            ret = (ret*b)%MOD_PRIME;
        b = (b*b)%MOD_PRIME;
        p/=2;
    }
    return ret;
}

ll base_prime = 31;
ll powers_of_base_prime[MAX];
ll inv_powers_of_base_prime[MAX];
void init_calc()
{
    ll p = base_prime;
    for(int i = 0; i < MAX; i++) {
        powers_of_base_prime[i] = p;
        inv_powers_of_base_prime[i] = bigMod(base_prime, MOD_PRIME-i-2);
        //assert(powers_of_base_prime[i]*inv_powers_of_base_prime[i]%MOD_PRIME == 1);
        p = (p*base_prime)%MOD_PRIME;
    }
}

ll getHash(string &s, ll prefix_hash_sum_of_s[])
{
    ll hs = 0;
    int sz = s.size();
    for(int i = 0; i < sz; i++) {
        hs = (hs + (s[i]-'a'+1)*powers_of_base_prime[i]%MOD_PRIME)%MOD_PRIME;
        prefix_hash_sum_of_s[i] = hs;
    }
    return hs;
}

ll getHashOfSubString(string &s, int st, int len, ll prefix_hash_of_s[])
{
    int n = s.size();
    if(st + len > n)return -1;
    int en = st+len-1;
    ll hash_till_end = prefix_hash_of_s[en];
    ll hash_before_st = ((st-1) >= 0)?prefix_hash_of_s[st-1]:0;
    ll hash_of_substr = (hash_till_end-hash_before_st+MOD_PRIME)%MOD_PRIME;
    ll inv = ((st-1) >= 0)?inv_powers_of_base_prime[st-1]:1;
    hash_of_substr = (hash_of_substr*inv)%MOD_PRIME;
    return hash_of_substr;
}

ll memo[MAX][MAX];
ll fst[MAX], snd[MAX];
int main()
{
    //assert(freopen("in.txt", "r", stdin));
    //assert(freopen("out.txt", "w", stdout));
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    //cout << fixed << setprecision(precision);
    init_calc();
    string s;
    cin >> s;
    string r;
    r = s;
    reverse(all(r));
    int n = s.size();

    ll prefix_hash_sum_of_s[MAX];
    getHash(s, prefix_hash_sum_of_s);
    ll prefix_hash_sum_of_r[MAX];
    getHash(r, prefix_hash_sum_of_r);

    for(int len = 1; len <= n; len++){
        int si = 1;
        int sj = si+len-1;
        while(sj <= n){
            int rj = n-sj+1;
            memo[len][sj] = (getHashOfSubString(s, si-1, len, prefix_hash_sum_of_s) ==
                             getHashOfSubString(r, rj-1, len, prefix_hash_sum_of_r));
            memo[len][sj] += memo[len][sj-1];
            si++;
            sj++;
        }
    }
//    for(int i = 0; i <= n; i++){
//        for(int j = 0; j <= n; j++){
//            cout << memo[i][j] << " ";
//        }
//        cout << endl;
//    }
    for(int j = 0; j <= n; j++){
        fst[j] = 0;
        for(int i = 0; i <= n; i++){
            fst[j] += memo[i][j];
        }
      //  cout << fst[j] << " ";
    }
    //cout << endl;
    for(int j = 1; j <= n; j++){
        int r = 1;
        int c = j;
        snd[j] = 0;
        while(r <= n && c <= n){
            snd[j] += memo[r][c];
            r++;
            c++;
        }
  //      cout << snd[j] << " ";
    }
//    cout << endl;
    int q;
    cin >> q;
    while(q--){
        int l, r;
        cin >> l >> r;
        cout << fst[r] - snd[l-1] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//
//using namespace __gnu_pbds;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

//class Timer {
//    clock_t start;
//    string name;
//public:
//    Timer() {name = "";start = clock();}
//    Timer(string s){name = s;start = clock();}
//    ~Timer() {fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0*(clock() - start) / CLOCKS_PER_SEC);}
//};

//#define isValid(a, b) ((a) >= 0 && (a) < (b))
//int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
//int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define um unordered_map
#define us unordered_set
#define endl '\n'
#define what_is(x) cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl

const double EPS = 1e-9;
const double PI = acos(-1.0);

template<typename T> inline T sq(T a){return a*a;}
template<typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b){return make_pair(a, b);}
template<typename T1, typename T2> inline T1 safeMod(T1 a, T2 m){return (a%m + m)%m;}/*handling negative sign of remainder*/
template<typename T1, typename T2> inline bool isEq(T1 a, T2 b){return abs(a - b) < EPS;}
template<typename T1, typename T2, typename T3> inline bool isEq(T1 a, T2 b, T3 eps){return abs(a - b) < eps;}
template<typename T> inline bool isKthBitOn(T n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;return bool((n & (ONE<<k)));}
template<typename T> inline void setKthBit(T& n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;n = (n|(ONE<<k));}
//mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
//template<typename T> inline T nextRand(T lo, T hi){return uniform_int_distribution<T>(lo, hi)(rng);}

#define toRad(deg) ((deg)*PI/180.0)
#define toDeg(rad) ((rad)*180.0/PI)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const int MAX = 5005;
const int MOD = 1000000007;
const int precision = 10;

const int MOD_PRIME = 589602329;
ll bigMod(ll b, int p)
{
    ll ret = 1;
    while(p) {
        if(p&1)
            ret = (ret*b)%MOD_PRIME;
        b = (b*b)%MOD_PRIME;
        p/=2;
    }
    return ret;
}

ll base_prime = 31;
ll powers_of_base_prime[MAX];
ll inv_powers_of_base_prime[MAX];
void init_calc()
{
    ll p = base_prime;
    for(int i = 0; i < MAX; i++) {
        powers_of_base_prime[i] = p;
        inv_powers_of_base_prime[i] = bigMod(base_prime, MOD_PRIME-i-2);
        //assert(powers_of_base_prime[i]*inv_powers_of_base_prime[i]%MOD_PRIME == 1);
        p = (p*base_prime)%MOD_PRIME;
    }
}

ll getHash(string &s, ll prefix_hash_sum_of_s[])
{
    ll hs = 0;
    int sz = s.size();
    for(int i = 0; i < sz; i++) {
        hs = (hs + (s[i]-'a'+1)*powers_of_base_prime[i]%MOD_PRIME)%MOD_PRIME;
        prefix_hash_sum_of_s[i] = hs;
    }
    return hs;
}

ll getHashOfSubString(string &s, int st, int len, ll prefix_hash_of_s[])
{
    int n = s.size();
    if(st + len > n)return -1;
    int en = st+len-1;
    ll hash_till_end = prefix_hash_of_s[en];
    ll hash_before_st = ((st-1) >= 0)?prefix_hash_of_s[st-1]:0;
    ll hash_of_substr = (hash_till_end-hash_before_st+MOD_PRIME)%MOD_PRIME;
    ll inv = ((st-1) >= 0)?inv_powers_of_base_prime[st-1]:1;
    hash_of_substr = (hash_of_substr*inv)%MOD_PRIME;
    return hash_of_substr;
}

ll memo[MAX][MAX];
ll fst[MAX], snd[MAX];
int main()
{
    //assert(freopen("in.txt", "r", stdin));
    //assert(freopen("out.txt", "w", stdout));
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    //cout << fixed << setprecision(precision);
    init_calc();
    string s;
    cin >> s;
    string r;
    r = s;
    reverse(all(r));
    int n = s.size();

    ll prefix_hash_sum_of_s[MAX];
    getHash(s, prefix_hash_sum_of_s);
    ll prefix_hash_sum_of_r[MAX];
    getHash(r, prefix_hash_sum_of_r);

    for(int len = 1; len <= n; len++){
        int si = 1;
        int sj = si+len-1;
        while(sj <= n){
            int rj = n-sj+1;
            memo[len][sj] = (getHashOfSubString(s, si-1, len, prefix_hash_sum_of_s) ==
                             getHashOfSubString(r, rj-1, len, prefix_hash_sum_of_r));
            memo[len][sj] += memo[len][sj-1];
            si++;
            sj++;
        }
    }
//    for(int i = 0; i <= n; i++){
//        for(int j = 0; j <= n; j++){
//            cout << memo[i][j] << " ";
//        }
//        cout << endl;
//    }
    for(int j = 1; j <= n; j++){
        fst[j] = 0;
        for(int i = 1; i <= n; i++){
            fst[j] += memo[i][j];
        }
      //  cout << fst[j] << " ";
    }
    //cout << endl;
    for(int j = 1; j <= n; j++){
        int r = 1;
        int c = j;
        snd[j] = 0;
        while(r <= n && c <= n){
            snd[j] += memo[r][c];
            r++;
            c++;
        }
  //      cout << snd[j] << " ";
    }
//    cout << endl;
    int q;
    cin >> q;
    while(q--){
        int lf, rg;
        cin >> lf >> rg;
        cout << fst[rg] - snd[lf-1] << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//
//using namespace __gnu_pbds;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

//class Timer {
//    clock_t start;
//    string name;
//public:
//    Timer() {name = "";start = clock();}
//    Timer(string s){name = s;start = clock();}
//    ~Timer() {fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0*(clock() - start) / CLOCKS_PER_SEC);}
//};

//#define isValid(a, b) ((a) >= 0 && (a) < (b))
//int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
//int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define um unordered_map
#define us unordered_set
#define endl '\n'
#define what_is(x) cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl

const double EPS = 1e-9;
const double PI = acos(-1.0);

template<typename T> inline T sq(T a){return a*a;}
template<typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b){return make_pair(a, b);}
template<typename T1, typename T2> inline T1 safeMod(T1 a, T2 m){return (a%m + m)%m;}/*handling negative sign of remainder*/
template<typename T1, typename T2> inline bool isEq(T1 a, T2 b){return abs(a - b) < EPS;}
template<typename T1, typename T2, typename T3> inline bool isEq(T1 a, T2 b, T3 eps){return abs(a - b) < eps;}
template<typename T> inline bool isKthBitOn(T n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;return bool((n & (ONE<<k)));}
template<typename T> inline void setKthBit(T& n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;n = (n|(ONE<<k));}
//mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
//template<typename T> inline T nextRand(T lo, T hi){return uniform_int_distribution<T>(lo, hi)(rng);}

#define toRad(deg) ((deg)*PI/180.0)
#define toDeg(rad) ((rad)*180.0/PI)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const int MAX = 5001;
const int precision = 10;

const int MOD_PRIME = 589602329;
ll bigMod(ll b, int p)
{
    ll ret = 1;
    while(p) {
        if(p&1)
            ret = (ret*b)%MOD_PRIME;
        b = (b*b)%MOD_PRIME;
        p/=2;
    }
    return ret;
}

ll base_prime = 31;
ll powers_of_base_prime[MAX];
ll inv_powers_of_base_prime[MAX];
void init_calc()
{
    ll p = base_prime;
    for(int i = 0; i < MAX; i++) {
        powers_of_base_prime[i] = p;
        inv_powers_of_base_prime[i] = bigMod(base_prime, MOD_PRIME-i-2);
//        assert(powers_of_base_prime[i]*inv_powers_of_base_prime[i]%MOD_PRIME == 1);
        p = (p*base_prime)%MOD_PRIME;
    }
}

ll getHash(string &s, ll prefix_hash_sum_of_s[])
{
    ll hs = 0;
    int sz = s.size();
    for(int i = 0; i < sz; i++) {
        hs = (hs + (s[i]-'a'+1)*powers_of_base_prime[i]%MOD_PRIME)%MOD_PRIME;
        prefix_hash_sum_of_s[i] = hs;
    }
    return hs;
}

ll getHashOfSubString(string &s, int st, int len, ll prefix_hash_of_s[])
{
    int n = s.size();
    if(st + len > n)return -1;
    int en = st+len-1;
    ll hash_till_end = prefix_hash_of_s[en];
    ll hash_before_st = ((st-1) >= 0)?prefix_hash_of_s[st-1]:0;
    ll hash_of_substr = (hash_till_end-hash_before_st+MOD_PRIME)%MOD_PRIME;
    ll inv = ((st-1) >= 0)?inv_powers_of_base_prime[st-1]:1;
    hash_of_substr = (hash_of_substr*inv)%MOD_PRIME;
    return hash_of_substr;
}

int triSum[MAX][MAX], recSum[MAX][MAX];

int main()
{
    //assert(freopen("in.txt", "r", stdin));
    //assert(freopen("out.txt", "w", stdout));
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    //cout << fixed << setprecision(precision);
    init_calc();
    string s;
    cin >> s;
    string r;
    r = s;
    reverse(all(r));
    int n = s.size();

    ll prefix_hash_sum_of_s[MAX];
    getHash(s, prefix_hash_sum_of_s);
    ll prefix_hash_sum_of_r[MAX];
    getHash(r, prefix_hash_sum_of_r);

    for(int len = 1; len <= n; len++){
        int si = 1;
        int sj = si+len-1;
        while(sj <= n){
            int rj = n-sj+1;
            triSum[len][si] = (getHashOfSubString(s, si-1, len, prefix_hash_sum_of_s) ==
                             getHashOfSubString(r, rj-1, len, prefix_hash_sum_of_r));
            recSum[len][si] = triSum[len][si];
            si++;
            sj++;
        }
    }
    for(int r = 1; r <= n; r++){
        int c = 1;
        int rr = r;
        ll sum = triSum[rr][c];
        rr--;
        c++;
        while(rr > 0){
            triSum[rr][c] += sum;
            sum = triSum[rr][c];
            rr--;
            c++;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n-i+1; j++){
            triSum[i][j] += triSum[i][j-1];
            recSum[i][j] += recSum[i][j-1];
        }
    }
    for(int j = 1; j <= n; j++){
        for(int i = 1; i <= n-j+1; i++){
            recSum[i][j] += recSum[i-1][j];
        }
    }
    int q;
    cin >> q;
    while(q--){
        int lf, rg;
        cin >> lf >> rg;
//        what_is(s.substr(lf-1, rg-lf+1));
//        what_is(r.substr(n-rg+1-1, rg-lf+1));
        int len = rg-lf+1;
        cout << triSum[1][rg] - triSum[1+len][rg-len] - recSum[1+len-1][rg-len] << endl;
//        cout.flush();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//
//using namespace __gnu_pbds;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

//class Timer {
//    clock_t start;
//    string name;
//public:
//    Timer() {name = "";start = clock();}
//    Timer(string s){name = s;start = clock();}
//    ~Timer() {fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0*(clock() - start) / CLOCKS_PER_SEC);}
//};

//#define isValid(a, b) ((a) >= 0 && (a) < (b))
//int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
//int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define um unordered_map
#define us unordered_set
#define endl '\n'
#define what_is(x) cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl

const double EPS = 1e-9;
const double PI = acos(-1.0);

template<typename T> inline T sq(T a){return a*a;}
template<typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b){return make_pair(a, b);}
template<typename T1, typename T2> inline T1 safeMod(T1 a, T2 m){return (a%m + m)%m;}/*handling negative sign of remainder*/
template<typename T1, typename T2> inline bool isEq(T1 a, T2 b){return abs(a - b) < EPS;}
template<typename T1, typename T2, typename T3> inline bool isEq(T1 a, T2 b, T3 eps){return abs(a - b) < eps;}
template<typename T> inline bool isKthBitOn(T n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;return bool((n & (ONE<<k)));}
template<typename T> inline void setKthBit(T& n, int k){assert(n <= numeric_limits<T>::max());assert(k<=numeric_limits<T>::digits);T ONE = 1;n = (n|(ONE<<k));}
//mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
//template<typename T> inline T nextRand(T lo, T hi){return uniform_int_distribution<T>(lo, hi)(rng);}

#define toRad(deg) ((deg)*PI/180.0)
#define toDeg(rad) ((rad)*180.0/PI)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const int MAX = 5005;
const int MOD = 1000000007;
const int precision = 10;

const int MOD_PRIME = 589602329;
ll bigMod(ll b, int p)
{
    ll ret = 1;
    while(p) {
        if(p&1)
            ret = (ret*b)%MOD_PRIME;
        b = (b*b)%MOD_PRIME;
        p/=2;
    }
    return ret;
}

ll base_prime = 31;
ll powers_of_base_prime[MAX];
ll inv_powers_of_base_prime[MAX];
void init_calc()
{
    ll p = base_prime;
    for(int i = 0; i < MAX; i++) {
        powers_of_base_prime[i] = p;
        inv_powers_of_base_prime[i] = bigMod(base_prime, MOD_PRIME-i-2);
        //assert(powers_of_base_prime[i]*inv_powers_of_base_prime[i]%MOD_PRIME == 1);
        p = (p*base_prime)%MOD_PRIME;
    }
}

ll getHash(string &s, ll prefix_hash_sum_of_s[])
{
    ll hs = 0;
    int sz = s.size();
    for(int i = 0; i < sz; i++) {
        hs = (hs + (s[i]-'a'+1)*powers_of_base_prime[i]%MOD_PRIME)%MOD_PRIME;
        prefix_hash_sum_of_s[i] = hs;
    }
    return hs;
}

ll getHashOfSubString(string &s, int st, int len, ll prefix_hash_of_s[])
{
    int n = s.size();
    if(st + len > n)return -1;
    int en = st+len-1;
    ll hash_till_end = prefix_hash_of_s[en];
    ll hash_before_st = ((st-1) >= 0)?prefix_hash_of_s[st-1]:0;
    ll hash_of_substr = (hash_till_end-hash_before_st+MOD_PRIME)%MOD_PRIME;
    ll inv = ((st-1) >= 0)?inv_powers_of_base_prime[st-1]:1;
    hash_of_substr = (hash_of_substr*inv)%MOD_PRIME;
    return hash_of_substr;
}

ll memo[MAX][MAX];
int main()
{
    //assert(freopen("in.txt", "r", stdin));
    //assert(freopen("out.txt", "w", stdout));
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    //cout << fixed << setprecision(precision);
    init_calc();
    string s;
    cin >> s;
    string r;
    r = s;
    reverse(all(r));
    int n = s.size();

    ll prefix_hash_sum_of_s[MAX];
    getHash(s, prefix_hash_sum_of_s);
    ll prefix_hash_sum_of_r[MAX];
    getHash(r, prefix_hash_sum_of_r);

    for(int len = 1; len <= n; len++){
        int si = 1;
        int sj = si+len-1;
        while(sj <= n){
            int ri = n-si+1;
            int rj = n-sj+1;
//            what_is(s.substr(si-1, sj-si+1));
//            what_is(r.substr(rj-1, ri-rj+1));
            memo[len][sj] = (getHashOfSubString(s, si-1, len, prefix_hash_sum_of_s) ==
                             getHashOfSubString(r, rj-1, len, prefix_hash_sum_of_r));
            memo[len][sj] += memo[len][sj-1];
            si++;
            sj++;
        }
    }
//    for(int i = 0; i <= n; i++){
//        for(int j = 0; j <= n; j++){
//            cout << memo[i][j] << " ";
//        }
//        cout << endl;
//    }
    ll fst[MAX], snd[MAX];
    for(int j = 0; j <= n; j++){
        fst[j] = 0;
        for(int i = 0; i <= n; i++){
            fst[j] += memo[i][j];
        }
      //  cout << fst[j] << " ";
    }
    //cout << endl;
    for(int j = 1; j <= n; j++){
        int r = 1;
        int c = j;
        snd[j] = 0;
        while(r <= n && c <= n){
            snd[j] += memo[r][c];
            r++;
            c++;
        }
  //      cout << snd[j] << " ";
    }
//    cout << endl;
    int q;
    cin >> q;
    while(q--){
        int l, r;
        cin >> l >> r;
        cout << fst[r] - snd[l-1] << endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxL = 5005;
char a[maxL];
int N, dp[maxL][maxL], Q, OK[maxL][maxL];
bool check(int l, int r)
{
	if(OK[l][r] != 0x3f3f3f3f) return OK[l][r];
	if(l == r) return OK[l][r] = 1;
	if(l == r - 1) return OK[l][r] = (a[l] == a[r] ? 1 : 0);
	if(a[l] != a[r]) return OK[l][r] = 0;
	return OK[l][r] = check(l + 1, r - 1);
}
int main()
{
	memset(OK, 0x3f, sizeof(OK));
	scanf("%s%d", a + 1, &Q);
	N = strlen(a + 1);
	for(int i = 1; i <= N; i++) dp[i][i] = 1;
	for(int i = N - 1; i >= 1; i--)
		for(int j = i + 1; j <= N; j++)
			dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + (check(i, j) ? 1 : 0);
	int l, r;
	while(Q--)
	{
		scanf("%d%d", &l, &r);
		printf("%d\n", dp[l][r]);
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <random>
using namespace std;

char s[5010];
int d[5010][5010];
bool pal[5010][5010];

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    fgets(s, 5001, stdin);

    int n = 0;
    for (int i = 0; isalpha(s[i]); i++) {
        d[i][i] = 1;
        pal[i][i] = 1;
        n++;
    }

    for (int i = 0; i < n - 1; i++) {
        d[i][i + 1] = (s[i] == s[i + 1] ? 3 : 2);
        pal[i][i + 1] = (s[i] == s[i + 1]);
    }

    for (int len = 3; len <= n; len++) {
        for (int l = 0, r = l + len - 1; r < n; l++, r++) {
            d[l][r] = d[l][r - 1] + d[l + 1][r] - d[l + 1][r - 1];
            pal[l][r] = pal[l + 1][r - 1] && s[l] == s[r];
            d[l][r] += pal[l][r];
        }
    }

    int q;
    scanf("%d", &q);

    for (int i = 0; i < q; i++) {
        int l, r;
        scanf("%d%d", &l, &r);
        printf("%d\n", d[l - 1][r - 1]);
    }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <deque>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
int p[6000][6000];
int ans[6000][6000];
int e[6000][6000];
int main()
{
	string s;
	cin>>s;
	for(int i=0; i<6000; i++)
		for(int j=0; j<6000; j++)
			p[i][j]=0;
	for(int i=0; i<s.size(); i++)
	{
		for(int j=0; (i-j)>=0&&((i+j)<s.size()); j++)
		{
			if(s[i-j]==s[i+j]) p[i-j][i+j]++;
			else break;
		}
		for(int j=0; (i-j)>=0&&((i+j+1)<s.size()); j++)
		{
			if(s[i-j]==s[i+j+1]) p[i-j][i+j+1]++;
		}
	}
	for(int i=0; i<6000; i++)
	{
		for(int j=i; j<6000; j++)
		{
			e[i][j]=p[i][j];
			if(i!=0) e[i][j]+=e[i-1][j];
		}
	}
	for(int i=0; i<6000; i++)
	{
		for(int j=i; j<6000; j++)
		{
			ans[i][j]=e[j][j];
			if(j>0) ans[i][j]+=ans[i][j-1];
			if(i>0) ans[i][j]-=e[i-1][j];
		}
	}
	int n;
	cin>>n;
	int l, r;
	for(int i=0; i<n; i++)
	{
		scanf("%d %d", &l, &r);
		printf("%d", ans[l-1][r-1]);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <deque>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
int p[5002][5002];
int ans[5002][5002];
int main()
{
	string s;
	cin>>s;
	for(int i=0; i<5002; i++)
		for(int j=0; j<5002; j++)
			p[i][j]=0;
	for(int i=0; i<s.size(); i++)
	{
		for(int j=0; (i-j)>=0&&((i+j)<s.size()); j++)
		{
			if(s[i-j]==s[i+j]) p[i-j][i+j]++;
			else break;
		}
		for(int j=0; (i-j)>=0&&((i+j+1)<s.size()); j++)
		{
			if(s[i-j]==s[i+j+1]) p[i-j][i+j+1]++;
			else break;
		}
	}
	for(int i=0; i<5002; i++)
	{
		for(int j=i; j<5002; j++)
		{
			p[i][j]=p[i][j];
			if(i!=0) p[i][j]+=p[i-1][j];
		}
	}
	for(int i=0; i<5002; i++)
	{
		for(int j=i; j<5002; j++)
		{
			ans[i][j]=p[j][j];
			if(j>0) ans[i][j]+=ans[i][j-1];
			if(i>0) ans[i][j]-=p[i-1][j];
		}
	}
	int n;
	cin>>n;
	int l, r;
	for(int i=0; i<n; i++)
	{
		scanf("%d %d", &l, &r);
		printf("%d\n", ans[l-1][r-1]);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <deque>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
int p[5002][5002];
int ans[5002][5002];
int main()
{
	string s;
	cin>>s;
	for(int i=0; i<5002; i++)
		for(int j=0; j<5002; j++)
			p[i][j]=0;
	for(int i=0; i<s.size(); i++)
	{
		for(int j=0; (i-j)>=0&&((i+j)<s.size()); j++)
		{
			if(s[i-j]==s[i+j]) p[i-j][i+j]++;
			else break;
		}
		for(int j=0; (i-j)>=0&&((i+j+1)<s.size()); j++)
		{
			if(s[i-j]==s[i+j+1]) p[i-j][i+j+1]++;
		}
	}
	for(int i=0; i<5002; i++)
	{
		for(int j=i; j<5002; j++)
		{
			p[i][j]=p[i][j];
			if(i!=0) p[i][j]+=p[i-1][j];
		}
	}
	for(int i=0; i<5002; i++)
	{
		for(int j=i; j<5002; j++)
		{
			ans[i][j]=p[j][j];
			if(j>0) ans[i][j]+=ans[i][j-1];
			if(i>0) ans[i][j]-=p[i-1][j];
		}
	}
	int n;
	cin>>n;
	int l, r;
	for(int i=0; i<n; i++)
	{
		scanf("%d %d", &l, &r);
		printf("%d", ans[l-1][r-1]);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <deque>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
using namespace std;
int p[5002][5002];
int ans[5002][5002];
int main()
{
	string s;
	cin>>s;
	for(int i=0; i<5002; i++)
		for(int j=0; j<5002; j++)
			p[i][j]=0;
	for(int i=0; i<s.size(); i++)
	{
		for(int j=0; (i-j)>=0&&((i+j)<s.size()); j++)
		{
			if(s[i-j]==s[i+j]) p[i-j][i+j]++;
			else break;
		}
		for(int j=0; (i-j)>=0&&((i+j+1)<s.size()); j++)
		{
			if(s[i-j]==s[i+j+1]) p[i-j][i+j+1]++;
		}
	}
	for(int i=0; i<5002; i++)
	{
		for(int j=i; j<5002; j++)
		{
			p[i][j]=p[i][j];
			if(i!=0) p[i][j]+=p[i-1][j];
		}
	}
	for(int i=0; i<5002; i++)
	{
		for(int j=i; j<5002; j++)
		{
			ans[i][j]=p[j][j];
			if(j>0) ans[i][j]+=ans[i][j-1];
			if(i>0) ans[i][j]-=p[i-1][j];
		}
	}
	int n;
	cin>>n;
	int l, r;
	for(int i=0; i<n; i++)
	{
		scanf("%d %d", &l, &r);
		printf("%d\n", ans[l-1][r-1]);
	}
	return 0;
}


#pragma comment(linker, "/STACK:500000000")
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
#include <stdio.h>
#include <bitset>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define Int long long
#define INF 0x3F3F3F3F
#define eps 1e-9
using namespace std;

char A[5555];
int a[5555][5555];
int sum[5555][5555];

int main()
{
    gets(A);
    int n = strlen(A), i, j, k;
    for(int center = 0; center < n; center++)
    {
        for(i = center, j = center; i >= 0 && j < n; i--, j++)
        {
            if (A[i] == A[j])
                a[i + 1][j + 1] = 1;
            else
                break;
        }
        for(i = center, j = center + 1; i >= 0 && j < n; i--, j++)
        {
            if (A[i] == A[j])
                a[i + 1][j + 1] = 1;
            else
                break;
        }
    }
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
            sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
    int q, L, R;
    cin >> q;
    while(q--)
    {
        scanf("%d %d", &L, &R);
        printf("%d\n", sum[n][R] - sum[L - 1][R]);
    }

}


#pragma comment(linker, "/STACK:500000000")
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
#include <stdio.h>
#include <bitset>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define Int long long
#define INF 0x3F3F3F3F
#define eps 1e-9
using namespace std;

char A[5555];
int a[5555][5555];
int sum[5555][5555];

int main()
{
    gets(A);
    int n = strlen(A), i, j, k;
    for(int center = 0; center < n; center++)
    {
        for(i = center, j = center; i >= 0 && j < n; i--, j++)
        {
            if (A[i] == A[j])
                a[i + 1][j + 1] = 1;
            else
                break;
        }
        for(i = center, j = center + 1; i >= 0 && j < n; i--, j++)
        {
            if (A[i] == A[j])
                a[i + 1][j + 1] = 1;
            else
                break;
        }
    }
    for(i = 0; i <= n; i++)
        for(j = 0; j <= n; j++)
            sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
    int q, L, R;
    cin >> q;
    while(q--)
    {
        scanf("%d %d", &L, &R);
        printf("%d\n", sum[n][R] - sum[L - 1][R]);
    }

}


#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<iostream>
#include<numeric>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<stdio.h>
#include<fstream>
#include<vector>
#include<bitset>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>

using namespace std;

#define all(v)             v.begin(),v.end()
#define sz(v)            ((int)((v).size()))
#define psh(x)                  push_back(x)
#define sc(x)                 scanf("%d",&x)
#define sc2(x,y)         scanf("%d%d",&x,&y)
#define scl(x)              scanf("%lld",&x)
#define lop(i,n)        for(int i=0;i<n;++i)
#define loop(i,f,l)     for(int i=f;i<l;++i)
#define R_(s)         freopen(s, "r", stdin)
#define W_(s)        freopen(s, "w", stdout)
#define R_W R_("input.txt"),W_("output.txt")
#define PI           acos(-1)
#define INF             1<<30
#define DFS_GRAY           -1
#define DFS_WHITE           0
#define DFS_BLACK           1

typedef string            ss;
typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<ss, int>     si;
typedef pair<int, ss>     is;
typedef pair<char, int>  chi;
typedef pair<ss, ss>     pss;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<ss>        vs;
typedef vector<ll>       vll;
typedef vector<vll>     vvll;

ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll gcd(ll a, ll b)  { return (b == 0 ? a : gcd(b, a % b)); }
ll sd(ll x)  { return x<10 ? x : x % 10 + sd(x / 10); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }

ll ispal[5100][5100];
int n, L, r, q;
ll dp[5100][5100];

int main()
{
    ss x; cin >> x;
    n = sz(x);
    lop(i, n)ispal[i][1] = 1, ispal[i][0] = 1;
    loop(l, 2, n + 1)lop(i, n+1-l)if (ispal[i + 1][l - 2] && x[i] == x[i + l - 1])ispal[i][l] = 1;

    lop(i, n)dp[i][i] = 1,dp[i][i+1]=ispal[i][2]+2;
    loop(l,3, n+1)lop(i, n+1-l)
    {
        dp[i][i + l - 1] = dp[i][i + l - 2] + dp[i + 1][i + 1 + l - 2] - dp[i + 1][i + 1 + l - 3]+ispal[i][l];
    }

    sc(q);
    lop(i, q)
    {
        sc2(L, r);
        printf("%lld ", dp[L - 1][r - 1]);
    }
}


/*
*
* Solved By OmarHashim
*
*/
#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<iostream>
#include<numeric>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<stdio.h>
#include<fstream>
#include<vector>
#include<bitset>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>

using namespace std;

#define all(v)             v.begin(),v.end()
#define sz(v)            ((int)((v).size()))
#define psh(x)                  push_back(x)
#define sc(x)                 scanf("%d",&x)
#define sc2(x,y)         scanf("%d%d",&x,&y)
#define scl(x)              scanf("%lld",&x)
#define lop(i,n)        for(int i=0;i<n;++i)
#define loop(i,f,l)     for(int i=f;i<l;++i)
#define R_(s)         freopen(s, "r", stdin)
#define W_(s)        freopen(s, "w", stdout)
#define R_W R_("input.txt"),W_("output.txt")
#define PI           acos(-1)
#define INF             1<<30
#define DFS_GRAY           -1
#define DFS_WHITE           0
#define DFS_BLACK           1

typedef string            ss;
typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<ss, int>     si;
typedef pair<int, ss>     is;
typedef pair<char, int>  chi;
typedef pair<ss, ss>     pss;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<ss>        vs;
typedef vector<ll>       vll;
typedef vector<vll>     vvll;

ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll gcd(ll a, ll b)  { return (b == 0 ? a : gcd(b, a % b)); }
ll sd(ll x)  { return x<10 ? x : x % 10 + sd(x / 10); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }

int ispal[5100][5100];
int n, L, r, q;
int dp[5100][5100];

int main()
{
    ss x; cin >> x;
    n = sz(x);
    lop(i, n)ispal[i][1] = 1, ispal[i][0] = 1;
    loop(l, 2, n + 1)lop(i, n+1-l)if (ispal[i + 1][l - 2] && x[i] == x[i + l - 1])ispal[i][l] = 1;

    lop(i, n)dp[i][i] = 1,dp[i][i+1]=ispal[i][2]+2;
    loop(l,3, n+1)lop(i, n+1-l)
    {
        dp[i][i + l - 1] = dp[i][i + l - 2] + dp[i + 1][i + 1 + l - 2] - dp[i + 1][i + 1 + l - 3]+ispal[i][l];
    }

    sc(q);
    lop(i, q)
    {
        sc2(L, r);
        printf("%d ", dp[L - 1][r - 1]);
    }
}


//#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>
#include <queue>
#include <memory.h>
#include <time.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstdio>
#include <set>
#include <queue>
#include <cassert>
#include <climits>
#include <deque>
#include <limits>
#define sz(x) int((x).size())
#define FOR(i,a,b) for(ll(i) = (a); (i) <= (b); ++(i))
#define ROF(i,a,b) for(ll(i) = (a); (i) >= (b); --(i))
#define rep(i,n) for (int(i) = 0; (i) < (n); ++(i))
#define fe(i,a) for (ll(i) = 0; (i) < int((a).size()); ++(i))
#define C(a) memset((a),0,sizeof(a))
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))
#define X first
#define Y second
#define file freopen("input.txt","r",stdin)
#define file2 freopen("output.txt", "w",stdout)
#define text freopen("input.txt","r",stdin); freopen("output.txt", "w",stdout)
using namespace std;
typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define y1 vamdlsfmvldf
const int mod = (int)1e9 + 7;
const ll INF = (int)(1e9);
const int N = 1000005;
bool pal[5005][5005] ;
ll se[5005][5005] ;
string s;
ll n;
int main() {
    cin >> s;
    ll sz = (int)s.size();
    cin >> n;
    for(int i = 0; i < sz; ++i){
        pal[i][i] = 1 ;
    }
    for(int i = 1; i < sz; ++i)
        if(s[i - 1] == s[i])
            pal[i - 1][i] = 1 ;
    for(ll l = sz - 1; l >= 0; --l) {
        for(ll r = l + 2; r < sz; ++r) {
            if(pal[l + 1][r - 1] == 1 && s[l] == s[r] && l + 1 <= r - 1) pal[l][r] = 1 ;
        }
    }
    for(ll l = 0; l < sz; ++l) {
        for(ll r = 0; r < sz; ++r) {
            if(pal[l][r]) se[l][r]++ ;
        }
    }
    for(ll l = sz - 1; l >= 0; --l) {
        for(ll r = 0; r < sz; ++r) {
            if(l > r) continue ;
            if(r >= 1) se[l][r] += se[l][r-1] ;
            if(l + 1 < sz) se[l][r] += se[l + 1][r] ;
            if(l + 1 < sz && r >= 1 && r - 1 >= l + 1) se[l][r] -= se[l + 1][r - 1] ;
        }
    }
    while(n --) {
        ll x, y;
        scanf("%I64d%I64d",&x,&y) ;
        x--; y--;
        printf("%I64d\n",se[x][y]) ;
    }
    return 0 ;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cstdio>
#include <ctime>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <cassert>
#include <iostream>
#include <cmath>
#include <memory.h>

using namespace std;

#pragma comment(linker, "/STACK:64000000")
typedef long long int64;
typedef unsigned long long uint64;

#define y1 _dsfkjdsfksdj
#define y0 _sfsdkfdop

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef pair<int64,int64> pii64;
typedef pair<pii,int> piii;
typedef pair<pii,pii> piiii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

const int MAXN = 5005;

int n, m;
char s[MAXN];

inline void init()
{
    gets(s);
    n = (int)strlen(s);
    scanf("%d", &m);
}

int f1[MAXN][MAXN];
int f2[MAXN][MAXN];
int l, r;

int rec1(int x, int y)
{
    if (f1[x][y] != -1) return f1[x][y];

    if (x == y) return f1[x][y] = 1;
    if (x + 1 == y) return f1[x][y] = (s[x] == s[y]);

    if (s[x] == s[y] && rec1(x + 1, y - 1)) return f1[x][y] = 1;
    return f1[x][y] = 0;
}

int main()
{
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

    init();

    memset(f1, -1, sizeof f1);

    for (int i = n - 1; i >= 0; --i)
    {
        f2[i][i] = rec1(i, i);
        for (int j = i + 1; j < n; ++j)
        {
            f2[i][j] += f2[i][j - 1];
            f2[i][j] += f2[i + 1][j];
            f2[i][j] -= f2[i + 1][j - 1];
            f2[i][j] += rec1(i, j);
        }
    }

    for (;m--;)
    {
        scanf("%d%d", &l, &r);
        --l, --r;
        int res = f2[l][r];
        printf("%d\n", res);
    }

    return 0;
}


#ifdef _WIN32
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <utility>
#include <stack>
#include <climits>
#include <deque>
#include <map>
#include <cctype>
#define MAXN 100005
#define MAXM 1000000005
#define MAXLG 18
#define endl '\n'
const double PI = 3.1415926535897932384626433832795;
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
bool ispal[5005][5005]; int dp[5005][5005];
int main() {
	//freopen("input.txt", "r", stdin);//freopen("output.txt","w",stdout);
	ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(12);
	cout << fixed;
	string s;
	cin >> s;
	int sz = s.size(),q,l,r;
	for(int i = 0; i < sz; ++i) ispal[i][i] = true, dp[i][i] = 1;
	for(int i = 0; i < sz-1; ++i){
		dp[i][i+1]=2;
		if(s[i] == s[i+1]) ispal[i][i+1] = true, dp[i][i+1]++;
	}
	for(int l = 2; l < sz; ++l){
		for(int i = 0; i + l < sz; ++i){
			if(s[i]==s[i+l] && ispal[i+1][i+l-1]) ispal[i][i+l] = true;
		}
	}
	for(int l = 2; l < sz; ++l){
		for(int i = 0; i + l < sz; ++i){
			dp[i][i+l] = dp[i][i+l-1] + dp[i+1][i+l] - dp[i+1][i+l-1] + ispal[i][i+l];
		}
	}
	cin >> q;
	while(q--){
		cin >> l >> r;
		--l;--r;
		cout << dp[l][r] << endl;
	}
	return 0;
}


#ifdef _WIN32
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <utility>
#include <stack>
#include <climits>
#include <deque>
#include <map>
#include <cctype>
#define MAXN 100005
#define MAXM 1000000005
#define MAXLG 18
#define endl '\n'
const double PI = 3.1415926535897932384626433832795;
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
bool ispal[5005][5005]; int dp[5005][5005];
int main() {
	//freopen("input.txt", "r", stdin);//freopen("output.txt","w",stdout);
	//ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(12);
	//cout << fixed;
	string s;
	cin >> s;
	int sz = s.size(),q,l,r;
	for(int i = 0; i < sz; ++i) ispal[i][i] = true, dp[i][i] = 1;
	for(int i = 0; i < sz-1; ++i){
		dp[i][i+1]=2;
		if(s[i] == s[i+1]) ispal[i][i+1] = true, dp[i][i+1]++;
	}
	for(int l = 2; l < sz; ++l){
		for(int i = 0; i + l < sz; ++i){
			if(s[i]==s[i+l] && ispal[i+1][i+l-1]) ispal[i][i+l] = true;
		}
	}
	for(int l = 2; l < sz; ++l){
		for(int i = 0; i + l < sz; ++i){
			dp[i][i+l] = dp[i][i+l-1] + dp[i+1][i+l] - dp[i+1][i+l-1] + ispal[i][i+l];
		}
	}
	cin >> q;
	while(q--){
		scanf("%d%d", &l, &r);
		l--, r--;
		printf("%d\n", dp[l][r]);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

const int MAXN = 5002;

int dyn[MAXN][MAXN];
bool pal[MAXN][MAXN];

int main()
{
    char str[MAXN];
    char c;
    int it = 1;

    while(1)
    {
        scanf("%c", &c);

        if (c == '\n')
            break;

        str[it++] = c;
    }

    int i = 0;
    int n = it - 1;

    while(i <= n)
    {
        dyn[i][i] = 1;
		pal[i][i] = 1;
        ++i;
    }

    int j = 2;
    i = 1;

    while(j <= n)
    {
        int jj = j, ii = 1;

        while(jj <= n)
        {
			if (ii + 1 == jj) pal[ii][jj]=str[ii]==str[jj];
			else pal[ii][jj] = (pal[ii+1][jj-1]) & (str[ii]==str[jj]);
            dyn[ii][jj] = dyn[ii + 1][jj] + dyn[ii][jj-1]-dyn[ii+1][jj-1]+(bool)(pal[ii][jj]);

            ++jj;
            ++ii;
        }

        ++j;

    }

	int q;
	scanf("%d", &q);
	for (int i=0; i<q; i++) {
		int l, r; scanf("%d%d", &l, &r);
		printf("%d\n", dyn[l][r]);
	}
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

const int MAXN = 5002;

int dyn[MAXN][MAXN];

int main()
{
    char str[MAXN];
    char c;
    int it = 1;

    while(1)
    {
        scanf("%c", &c);

        if (c == '\n')
            break;

        str[it++] = c;
    }

    int i = 0;
    int n = it - 1;

    while(i <= n)
    {
        dyn[i][i] = 1;
        ++i;
    }

    int j = 2;
    i = 1;

    while(j <= n)
    {
        int jj = j, ii = 1;

        while(jj <= n)
        {
            dyn[ii][jj] = dyn[ii + 1][jj] + dyn[ii][jj-1]-dyn[ii+1][jj-1]+(bool)(str[ii]==str[jj]);

            ++jj;
            ++ii;
        }

        ++j;

    }

	int q;
	scanf("%d", &q);
	for (int i=0; i<q; i++) {
		int l, r; scanf("%d%d", &l, &r);
		printf("%d\n", dyn[l][r]);
	}
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iostream>
#include <queue>
#define ll long long int
using namespace std;
const ll mod=1e9+7;

char c[5555];
int d[5555][5555], st[5555][5555];

int main(){
#pragma comment(linker,"/STACK:268435456")
#pragma warning(disable:4996)
#ifdef _DEBUG
    freopen("input.txt","rt",stdin);
    freopen("output.txt","wt",stdout);
#endif
    string s;
    int i, j, n, x, l, r, m;
    gets(c);
    s=c;
    n=s.size();
    for(i=0;i<n;i++){
        st[i][i]=1;
        d[i][i]=1;
        st[i+1][i]=1;
    }
    for(i=1;i<n;i++){
        for(j=0;j<n-i;j++){
            if(s[j]==s[i+j]){
                st[j][i+j]=st[j+1][i+j-1];
            }
            d[j][j+i]=st[j][i+j];
            d[j][j+i]+=d[j][j+i-1]+d[j+1][i+j]-d[j+1][i+j-1];
        }
    }
    scanf("%d",&m);
    for(i=0;i<m;i++){
        scanf("%d%d",&l,&r);
        printf("%d\n",d[l-1][r-1]);
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <ctime>
#include <cmath>
using namespace std;

const int P1 = 31;
const int P2 = 17239;

const int N = 5005;

int n, q;
char s[N], rs[N];
int powP1[N], powP2[N];
int h1s[N], h2s[N], h1rs[N], h2rs[N];
int res[N][N];

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    powP1[0] = 1;
    powP2[0] = 1;
    for (int i = 1; i < N; i++)
    {
        powP1[i] = powP1[i - 1] * P1;
        powP2[i] = powP2[i - 1] * P2;
    }

    gets(s);
    n = strlen(s);

    strcpy(rs, s);
    reverse(rs, rs + n);

    h1s[0] = s[0] - 'a' + 1;
    h2s[0] = s[0] - 'a' + 1;
    for (int i = 1; i < n; i++)
    {
        h1s[i] = h1s[i - 1] + (s[i] - 'a' + 1) * powP1[i];
        h2s[i] = h2s[i - 1] + (s[i] - 'a' + 1) * powP2[i];
    }

    h1rs[0] = rs[0] - 'a' + 1;
    h2rs[0] = rs[0] - 'a' + 1;
    for (int i = 1; i < n; i++)
    {
        h1rs[i] = h1rs[i - 1] + (rs[i] - 'a' + 1) * powP1[i];
        h2rs[i] = h2rs[i - 1] + (rs[i] - 'a' + 1) * powP2[i];
    }

    for (int l = 0; l < n; l++)
        for (int r = l; r < n; r++)
        {
            int nl = n - 1 - r;
            int nr = n - 1 - l;

            int hash1s = h1s[r] - (l - 1 >= 0 ? h1s[l - 1] : 0);
            hash1s *= powP1[n - l];
            int hash2s = h2s[r] - (l - 1 >= 0 ? h2s[l - 1] : 0);
            hash2s *= powP2[n - l];

            int hash1rs = h1rs[nr] - (nl - 1 >= 0 ? h1rs[nl - 1] : 0);
            hash1rs *= powP1[n - nl];
            int hash2rs = h2rs[nr] - (nl - 1 >= 0 ? h2rs[nl - 1] : 0);
            hash2rs *= powP2[n - nl];

            if (hash1s == hash1rs && hash2s == hash2rs)
                res[l][r]++;
        }

    for (int len = 2; len <= n; len++)
        for (int l = 0; l + len - 1 < n; l++)
        {
            int r = l + len - 1;
            res[l][r] += res[l + 1][r] + res[l][r - 1];
            if (l + 1 <= r - 1)
                res[l][r] -= res[l + 1][r - 1];
        }

    scanf("%d", &q);
    for (int i = 0; i < q; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        x--; y--;
        printf("%d\n", res[x][y]);
    }

    return 0;
}


#pragma comment(linker, "/STACK:65000000")
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<string>
#include<cmath>
#include<complex>
#include<ctime>

using namespace std;

#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vit vi::iterator
#define vpi vector<pii >
#define sq(x) (x)*(x)

int dyn[5005][5005];
bool pal[5005][5005];

char s[5005];
int n;

int main()
{
    //freopen(".in","r",stdin);freopen(".out","w",stdout);
    scanf("%s",s);
    n=strlen(s);
    for(int i=0; i<n; ++i)
        pal[i][1]=pal[i][0]=1;
    for(int i=2; i<=n; ++i)
        for(int j=0; j+i<=n; ++j)
        {
            pal[j][i]=pal[j+1][i-2]&&(s[j]==s[j+i-1]);
        }
    for(int i=0; i<n; ++i)
        dyn[i][1]=1;
    for(int i=2; i<=n; ++i)
        for(int j=0; j+i<=n; ++j)
        {
            dyn[j][i]=dyn[j][i-1]+dyn[j+1][i-1]-dyn[j+1][i-2]+pal[j][i];
        }
    int q;
    scanf("%d",&q);
    for(int i=0; i<q; ++i)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",dyn[a-1][b-a+1]);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
using namespace std;
int dp[5001][5001]={};
bool same[5001][5001]={},kaibun[5001][5001]={};
bool a=false;
char str[5001];
int main(){
	scanf("%s",str);
	int length=strlen(str);
	for(int i=0;i<length;i++){
		for(int j=i;j<length;j++){
			if(i==j){
				kaibun[i][j]=true;
				kaibun[j][i]=true;
			}else{
				if((j-i)%2==0){
					for(int k=i;k<(j+i)/2;k++){
						if(str[k]!=str[i+j-k]){
							a=true;
							break;
						}
					}
					if(!a){
						kaibun[i][j]=true;
						kaibun[j][i]=true;
					}
					a=false;
				}else{
					for(int k=i;k<=(j+i)/2;k++){
						if(str[k]!=str[i+j-k]){
							a=true;
							break;
						}
					}
					if(!a){
						kaibun[i][j]=true;
						kaibun[j][i]=true;
					}
					a=false;
				}
			}
		}
	}
	for(int i=0;i<length;i++){
		dp[i][i]=1;
	}
	for(int i=0;i<length-1;i++){
		if(str[i]==str[i+1]){
			dp[i][i+1]=3;
		}else{
			dp[i][i+1]=2;
		}
	}
	for(int i=2;i<length;i++){
		for(int j=0;j<length-i;j++){
			if(kaibun[j][j+i]){
				dp[j][j+i]=dp[j+1][j+i]+dp[j][j+i-1]-dp[j+1][j+i-1]+1;
			}else{
				dp[j][j+i]=dp[j+1][j+i]+dp[j][j+i-1]-dp[j+1][j+i-1];
			}
		}
	}
	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		printf("%d\n",dp[a-1][b-1]);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
using namespace std;
int dp[5001][5001]={};
bool same[5001][5001]={},kaibun[5001][5001]={};
bool a=false;
char str[5001];
int main(){
	scanf("%s",str);
	int length=strlen(str);
	for(int i=0;i<length;i++){
		for(int j=i;j<length;j++){
			if(i==j){
				kaibun[i][j]=true;
				kaibun[j][i]=true;
			}else{
				if((j-i)%2==0){
					for(int k=i;k<(j+i)/2;k++){
						if(str[k]!=str[i+j-k]){
							a=true;
							break;
						}
					}
					if(!a){
						kaibun[i][j]=true;
						kaibun[j][i]=true;
					}
					a=false;
				}else{
					for(int k=i;k<=(j+i)/2;k++){
						if(str[k]!=str[i+j-k]){
							a=true;
							break;
						}
					}
					if(!a){
						kaibun[i][j]=true;
						kaibun[j][i]=true;
					}
					a=false;
				}
			}
		}
	}
	for(int i=0;i<length;i++){
		dp[i][i]=1;
	}
	for(int i=0;i<length-1;i++){
		if(same[i][i+1]){
			dp[i][i+1]=3;
		}else{
			dp[i][i+1]=2;
		}
	}
	for(int i=2;i<length;i++){
		for(int j=0;j<length-i;j++){
			if(kaibun[j][j+i]){
				dp[j][j+i]=dp[j+1][j+i]+dp[j][j+i-1]-dp[j+1][j+i-1]+1;
			}else{
				dp[j][j+i]=dp[j+1][j+i]+dp[j][j+i-1]-dp[j+1][j+i-1];
			}
		}
	}
	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		printf("%d\n",dp[a-1][b-1]);
	}
	return 0;
}


#include <stdio.h>
bool x[5001][5001];
int main(){
 return 0;
}


#include <cstdio>
#include <cstring>
using namespace std;
int dp[5001][5001]={};
bool same[5001][5001]={},kaibun[5001][5001]={};
bool a=false;
char str[5001];
int main(){
	scanf("%s",str);
	int length=strlen(str);
	for(int i=0;i<length;i++){
		for(int j=i+1;j<length;j++){
			if(str[i]==str[j]){
				same[i][j]=true;
				same[j][i]=true;
			}
		}
	}
	for(int i=0;i<length;i++){
		for(int j=i;j<length;j++){
			if(i==j){
				kaibun[i][j]=true;
				kaibun[j][i]=true;
			}else{
				if((j-i)%2==0){
					for(int k=i;k<(j+i)/2;k++){
						if(!same[k][i+j-k]){
							a=true;
							break;
						}
					}
					if(!a){
						kaibun[i][j]=true;
						kaibun[j][i]=true;
					}
					a=false;
				}else{
					for(int k=i;k<=(j+i)/2;k++){
						if(!same[k][i+j-k]){
							a=true;
							break;
						}
					}
					if(!a){
						kaibun[i][j]=true;
						kaibun[j][i]=true;
					}
					a=false;
				}
			}
		}
	}
	for(int i=0;i<length;i++){
		dp[i][i]=1;
	}
	for(int i=0;i<length-1;i++){
		if(same[i][i+1]){
			dp[i][i+1]=3;
		}else{
			dp[i][i+1]=2;
		}
	}
	for(int i=2;i<length;i++){
		for(int j=0;j<length-i;j++){
			if(kaibun[j][j+i]){
				dp[j][j+i]=dp[j+1][j+i]+dp[j][j+i-1]-dp[j+1][j+i-1]+1;
			}else{
				dp[j][j+i]=dp[j+1][j+i]+dp[j][j+i-1]-dp[j+1][j+i-1];
			}
		}
	}
	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		printf("%d\n",dp[a-1][b-1]);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
using namespace std;
int dp[5001][5001]={};
bool same[5001][5001]={},kaibun[5001][5001]={};
bool allsame=true;
int now=0;
bool a=false;
char str[5001];
int main(){
	scanf("%s",str);
	int length=strlen(str);
	for(int i=0;i<length-1;i++){
		if(str[i]==str[i+1]){
			if(allsame){
			for(int j=now;j<=i+1;j++){
				kaibun[j][i+1]=true;
			}
			}else{
				allsame=true;
				now=i;
			}
		}else{
			allsame=false;
		}
	}
	for(int i=0;i<length;i++){
		for(int j=i;j<length;j++){
			if(!kaibun[i][j-1]){
				if((j-i)%2==0){
					for(int k=i;k<(j+i)/2;k++){
						if(str[k]!=str[i+j-k]){
							a=true;
							break;
						}
					}
					if(!a){
						kaibun[i][j]=true;
						kaibun[j][i]=true;
					}
					a=false;
				}else{
					for(int k=i;k<=(j+i)/2;k++){
						if(str[k]!=str[i+j-k]){
							a=true;
							break;
						}
					}
					if(!a){
						kaibun[i][j]=true;
						kaibun[j][i]=true;
					}
					a=false;
				}
			}
		}
	}
	for(int i=0;i<length;i++){
		dp[i][i]=1;
	}
	for(int i=0;i<length-1;i++){
		if(str[i]==str[i+1]){
			dp[i][i+1]=3;
		}else{
			dp[i][i+1]=2;
		}
	}
	for(int i=2;i<length;i++){
		for(int j=0;j<length-i;j++){
			if(kaibun[j][j+i]){
				dp[j][j+i]=dp[j+1][j+i]+dp[j][j+i-1]-dp[j+1][j+i-1]+1;
			}else{
				dp[j][j+i]=dp[j+1][j+i]+dp[j][j+i-1]-dp[j+1][j+i-1];
			}
		}
	}
	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		printf("%d\n",dp[a-1][b-1]);
	}
	return 0;
}


#include <stdio.h>
int x[5001][5001];
int main(){
 return 0;
}


#include <string>
#include <vector>
#include <list>
#include <iostream>
#include <set>
#include <queue>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>

#define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
#define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i))
#define CLEAR(a) memset((a),0,sizeof(a))
#define INF 1000000000
#define PB push_back
#define ALL(c) (c).begin(), (c).end()
#define pi 2*acos(0.0)
#define SQR(a) (a)*(a)
#define MP make_pair
#define MAX 128
#define MOD 1000000007

using namespace std;

long long sum[5001];
int a[5000][5000];

string s;

int main()
{
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);

	cin >> s;

	FOR(i,0,s.length())
		FOR(j,i,s.length()) a[j][i]=1;

	RFOR(i,s.length()-1,-1)
		FOR(j,i,s.length()) if (a[i+1][j-1]==1 && s[i]==s[j])
		a[i][j]=1;

	sum[0]=0;
	FOR(i,0,s.length())
	{
		FOR(j,0,i+1)
		sum[i+1]+=a[j][i];
		sum[i+1]+=sum[i];
	}

int n;
int x,y;
cin >> n;
FOR(i,0,n){
scanf("%d %d",&x,&y);
long long t=sum[y]-sum[x]+1;
printf("%d\n",t);
}
   	return 0;
}


#include <string>
#include <vector>
#include <list>
#include <iostream>
#include <set>
#include <queue>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>

#define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
#define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i))
#define CLEAR(a) memset((a),0,sizeof(a))
#define INF 1000000000
#define PB push_back
#define ALL(c) (c).begin(), (c).end()
#define pi 2*acos(0.0)
#define SQR(a) (a)*(a)
#define MP make_pair
#define MAX 128
#define MOD 1000000007

using namespace std;

int sum[5002][5002];
int a[5002][5002];

string s;

int main()
{
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);

	cin >> s;

	FOR(i,0,s.length())
		FOR(j,i,s.length()) a[j][i]=1;

	RFOR(i,s.length(),0)
		FOR(j,i,s.length()) if (a[i+1][j-1]==1 && s[i]==s[j])
		a[i][j]=1;

	RFOR(i,s.length()+1,1)
		FOR(j,i,s.length()+1)
		sum[i][j]=-sum[i+1][j-1]+sum[i][j-1]+sum[i+1][j]+a[i-1][j-1];

	int n;
int x,y;
cin >> n;
FOR(i,0,n){
scanf("%d %d",&x,&y);
long long t=sum[x][y];
printf("%d\n",t);
}
   	return 0;
}


#include <iostream>
#include <string>

using namespace std;

const int MAXN = 5000 + 10;

string s;
int n, q, dp[MAXN][MAXN];
bool isPal[MAXN][MAXN];

int main()
{
    ios::sync_with_stdio(0);
    cin >> s;
    n = (int)s.size();
    for (int i = 0 ; i < n ; i++)
    {
        isPal[i][i] = 1;
        dp[i][i] = 1;
        isPal[i+1][i] = 1;
    }
    for (int len = 2 ; len <= n ; len++)
        for (int start = 0 ; start <= n-len ; start++)
        {
            isPal[start][start+len-1] = isPal[start+1][start+len-2] & s[start] == s[start+len-1];
            dp[start][start+len-1] = dp[start][start+len-2] + dp[start+1][start+len-1] - dp[start+1][start+len-2] + isPal[start][start+len-1];
        }
    cin >> q;
    for (int i = 1 ; i <= q ; i++)
    {
        int l, r;
        cin >> l >> r;
        l--;
        r--;
        cout << dp[l][r] << endl;
    }
    return 0;
}


#pragma comment(linker, "/STACK:160777216")
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
#include <set>
#include <bitset>
using namespace std;
#define wtf(s){while(1)puts(s);}
#define FOR(i,b,n) for( int i = b; i < n; ++i )
#define all(v) (v).begin(), (v).end()
#define sz(v) int(  (v).size()   )

int n;
string s;

const int ntree = 5005*4;
vector<int> tree[ntree];

void push( int lpos, int rpos, int v = 1, int tl = 1, int tr = n ){
    tree[v].push_back( rpos );
    int tm = (tl+tr)/2;
    if( tl == tr ) return;
    if  ( lpos <= tm ) push( lpos, rpos, 2*v  , tl  , tm );
    else               push( lpos, rpos, 2*v+1, tm+1, tr );

}

int qw( int l, int r, int rpos, int v = 1, int tl = 1, int tr = n ){
    if( l > r ) return 0;
    if( l == tl && r==tr ){
        int cnt = upper_bound(all(tree[v]),rpos) - tree[v].begin();
        return cnt;
    }
    else{
        int tm = (tl+tr)/2;
        return
            qw( l          , min(r,tm), rpos, 2*v  , tl  , tm ) +
            qw( max(tm+1,l), r        , rpos, 2*v+1, tm+1, tr );
    }
}

__int32 main(){
    //freopen("input.txt", "r", stdin);

    getline( cin, s );
    n = sz(s);
    s = '#' + s + '~';

    FOR( i, 1, n+1 ){
        int l, r;

        l = r = i;
        push( l, r );
        while( s[l-1] == s[r+1] ){
            --l, ++r;
            push( l, r );
        }
        if( s[i] == s[i+1] ){
            l = i;
            r = i + 1;
            push( l, r );
            while( s[l-1] == s[r+1] ){
                --l, ++r;
                push( l, r );
            }
        }
    }

    FOR( i, 0, ntree ){
        sort( all(tree[i]) );
    }

    int cnt;
    scanf( "%d", &cnt );
    FOR( i, 0, cnt ){
        int l, r;
        scanf( "%d %d", &l, &r );
        printf( "%d\n", qw(l,r,r) );
    }

    return 0;
}


#pragma comment(linker, "/STACK:160777216")
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
#include <set>
#include <bitset>
using namespace std;
#define wtf(s){while(1)puts(s);}
#define FOR(i,b,n) for( int i = b; i < n; ++i )
#define all(v) (v).begin(), (v).end()
#define sz(v) int(  (v).size()   )

int n;
string s;

const int ntree = 5005*4;
vector<short> tree[ntree];

void push( int lpos, int rpos, int v = 1, int tl = 1, int tr = n ){
    tree[v].push_back( rpos );
    int tm = (tl+tr)/2;
    if( tl == tr ) return;
    if  ( lpos <= tm ) push( lpos, rpos, 2*v  , tl  , tm );
    else               push( lpos, rpos, 2*v+1, tm+1, tr );

}

int qw( int l, int r, int rpos, int v = 1, int tl = 1, int tr = n ){
    if( l > r ) return 0;
    if( l == tl && r==tr ){
        int cnt = upper_bound(all(tree[v]),rpos) - tree[v].begin();
        return cnt;
    }
    else{
        int tm = (tl+tr)/2;
        return
            qw( l          , min(r,tm), rpos, 2*v  , tl  , tm ) +
            qw( max(tm+1,l), r        , rpos, 2*v+1, tm+1, tr );
    }
}

__int32 main(){
    //freopen("input.txt", "r", stdin);
    //cout << 12 * (5000*5000*2 *2) / (1024.*1024) << endl;

    getline( cin, s );
    n = sz(s);
    s = '#' + s + '~';

    FOR( i, 1, n+1 ){
        int l, r;

        l = r = i;
        push( l, r );
        while( s[l-1] == s[r+1] ){
            --l, ++r;
            push( l, r );
        }
        if( s[i] == s[i+1] ){
            l = i;
            r = i + 1;
            push( l, r );
            while( s[l-1] == s[r+1] ){
                --l, ++r;
                push( l, r );
            }
        }
    }

    FOR( i, 0, ntree ){
        sort( all(tree[i]) );
    }

    int cnt;
    scanf( "%d", &cnt );
    FOR( i, 0, cnt ){
        int l, r;
        scanf( "%d %d", &l, &r );
        printf( "%d\n", qw(l,r,r) );
    }

    return 0;
}


#pragma comment(linker, "/STACK:160777216")
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
#include <set>
#include <bitset>
using namespace std;
#define wtf(s){while(1)puts(s);}
#define FOR(i,b,n) for( int i = b; i < n; ++i )
#define all(v) (v).begin(), (v).end()
#define sz(v) int(  (v).size()   )

int n;
string s;
vector<vector<int>> cnt;

__int32 main(){
    //freopen("input.txt", "r", stdin);

    getline( cin, s );
    n = sz(s);
    cnt.assign( n+1, vector<int>(n+1,0) );
    s = '#' + s + '~';

    FOR( i, 1, n+1 ){
        int l, r;

        l = r = i;
        ++cnt[l][r];
        while( s[l-1] == s[r+1] ){
            --l, ++r;
            ++cnt[l][r];
        }

        if( s[i] == s[i+1] ){
            l = i;
            r = i + 1;
            ++cnt[l][r];
            while( s[l-1] == s[r+1] ){
                --l, ++r;
                ++cnt[l][r];
            }
        }
    }

    FOR( i, 1, n+1 ){
        FOR( j, 1, n+1 ){
            cnt[i][j] += cnt[i][j-1] + cnt[i-1][j] - cnt[i-1][j-1];
        }
    }

    int qcnt;
    scanf( "%d", &qcnt );
    FOR( i, 0, qcnt ){
        int l, r;
        scanf( "%d %d", &l, &r );
        printf( "%d\n", cnt[r][r] - cnt[r][l-1] - cnt[l-1][r] + cnt[l-1][l-1] );
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
	string s; cin >> s;
	int n = s.length(), x, y;

	vector<vector<char> > isp(n, vector<char> (n, 0));

	for (int i = n; i >= 0; --i)
		for (int j = 0; j < i; ++j)
		{
			x = j; y = j + (n - i);
			if (x == y)
				isp[x][y] = 1;
			else if (x + 1 == y)
				isp[x][y] = s[x] == s[y];
			else
				isp[x][y] = (s[x] == s[y]) && isp[x + 1][y - 1];
		}

	vector<vector<int> > dp(n, vector<int> (n, 0));

	for (int i = n; i >= 0; --i)
		for (int j = 0; j < i; ++j)
		{
			x = j; y = j + (n - i);
			if (x == y)
				dp[x][y] = 1;
			else if (x + 1 == y)
				dp[x][y] = 1 + 1 + isp[x][y];
			else
				dp[x][y] = dp[x + 1][y] + dp[x][y - 1] - dp[x + 1][y - 1] + isp[x][y];
		}

	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> x >> y;
		cout << dp[--x][--y] << endl;
	}

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <set>

using namespace std;

typedef __int64 int64;

const double eps=0.0000001;
const double pi=3.14159265358979323846;
const int maxn=5010;

int n;
int A[maxn][maxn];
char s[maxn];

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	gets(s);
	n=strlen(s);
	for (int i=0; i<n; i++)
		for (int j=0; j<n; j++)
			A[i][j]=0;
	for (int i=0; i<n; i++)
	{
		int d=0;
		while (i-d>=0 && i+d<n && s[i-d]==s[i+d])
		{
			A[i-d][i+d]++;
			d++;
		}
		d=0;
		while (i-d>=0 && i+d+1<n && s[i-d]==s[i+d+1])
		{
			A[i-d][i+d+1]++;
			d++;
		}
	}
	/*for (int i=0; i<n; i++)
	{
		printf("\n");
		for (int j=0; j<n; j++)
			printf("%d ",A[i][j]);
	}*/
	for (int s=1; s<n; s++)
		for (int i=0; i<n; i++)
		{
			int j=i+s;
			if (j<n)
				A[i][j]+=(A[i+1][j]+A[i][j-1]-A[i+1][j-1]);
		}
	int q;
	scanf("%d",&q);
	for (int i=0; i<q; i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		printf("%d\n",A[x-1][y-1]);
	}
	return 0;
}


//In the name of Allah
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<functional>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<ctime>
#include<string>
#include<cstdio>
#include<locale>
#include<codecvt>
using namespace std;
#define lop(i,a,n) for(int i=a;i<n;++i)
#define loop(i,n,a)for(int i=n-1;i>=a;--i)
#define R_(s)      freopen(s, "r", stdin)
#define W_(s)      freopen(s, "w", stdout)
#define R_W        R_("in.txt"),W_("out.txt")
#define ll         long long
#define ld         long double
#define ii         pair<ll,ll>
#define vii        vector<ii>
#define vi         vector<int>
#define vll        vector<ll>
#define vs         vector<string>
#define vvii       vector<vector<ii>>
#define vvi        vector<vector<int>>
#define vvll       vector<vector<ll>>
#define sz(v)      (ll)v.size()
#define all(v)     v.begin(),v.end()
#define sc(n)      scanf("%d",&n)
#define scl(n)     scanf("%lld",&n)
#define pr1(n)     printf("%d\n",n)
#define pr2(n)     printf("%d " ,n)
#define pr4(n)     printf("%lld\n",n)
#define pr3(n)     cout<<fixed<<setprecision(4)<<n<<endl
#define endl       "\n"
#define PI         2*acos(0.0)
#define DFS_GRAY  -1
#define DFS_WHITE  0
#define DFS_BLACK  1
#define oo  1e9
#define OO  1e18
#define EPS 1e-9
int dr[] = { 1, 0, -1, 0, -1, -1, 1, 1 };
int dc[] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = 5e3 + 7;
const int MOD = 1e9 + 7;
ll isPal[MAX][MAX];
int main()
{
	string s; cin >> s;
	int n = s.length();
	lop(i, 0, n)isPal[i][i] = 1;
	lop(length, 2, n+1)
	lop(i, 0, n-length+1)
	{
		int j = i + length - 1;
		if (s[i] == s[j]) {
			if (length == 2)isPal[i][j] = 1;
		    else isPal[i][j] = isPal[i + 1][j - 1];
		}
	}
	loop(i, n-1, 0)lop(j, 0, n)isPal[i][j] += isPal[i + 1][j];
	lop(j, 1, n)lop(i, 0, n)isPal[i][j] += isPal[i][j - 1];
	int m; cin >> m;
	lop(i, 0, m)
	{
		int l, r; cin >> l >> r; l--, r--;
		pr4(isPal[l][r]);
	}
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>

using namespace std;

char s[5005] = {0};
int a[5005][5005] = {0};

int main()
{
    scanf("%s", s);

    int n = strlen(s);
    vector<int> d1 (n);

    int l=0, r=-1;
    for (int i=0; i<n; ++i) {
        int k = (i>r ? 0 : min (d1[l+r-i], r-i)) + 1;
        while (i+k < n && i-k >= 0 && s[i+k] == s[i-k])  ++k;
        d1[i] = k--;
        if (i+k > r)
            l = i-k,  r = i+k;
        while(k >= 0)
        {
            a[i-k][i+k]++;
            k--;
        }
    }
    vector<int> d2 (n);
    l=0, r=-1;
    for (int i=0; i<n; ++i) {
        int k = (i>r ? 0 : min (d2[l+r-i+1], r-i+1)) + 1;
        while (i+k-1 < n && i-k >= 0 && s[i+k-1] == s[i-k])  ++k;
        d2[i] = --k;
        if (i+k-1 > r)
            l = i-k,  r = i+k-1;
        while(k > 0)
        {
            a[i-k][i+k-1]++;
            k--;
        }
    }

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(i > 0 && j > 0)
                a[i][j] -= a[i-1][j-1];
            if(i > 0)
                a[i][j] += a[i-1][j];
            if(j > 0)
                a[i][j] += a[i][j-1];
        }
    }

    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        l--;
        r--;
        printf("%d\n", a[r][r] - ((l-1 >= 0) ? (a[l-1][r] - a[r][l-1] + a[l-1][l-1]) : 0));
    }

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>

using namespace std;

char s[5005] = {0};
int a[5005][5005] = {0};

int main()
{
    scanf("%s", s);

    int n = strlen(s);

    vector<int> d1 (n);
    int l=0, r=-1;
    for (int i=0; i<n; ++i) {
        int k = (i>r ? 0 : min (d1[l+r-i], r-i)) + 1;
        while (i+k < n && i-k >= 0 && s[i+k] == s[i-k]) ++k;
        d1[i] = k--;
        if (i+k > r)
            l = i-k,  r = i+k;
        while(k >= 0)
        {
            a[i-k][i+k]++;
            k--;
        }
    }
    vector<int> d2 (n);
    l=0, r=-1;
    for (int i=0; i<n; ++i) {
        int k = (i>r ? 0 : min (d2[l+r-i+1], r-i+1)) + 1;
        while (i+k-1 < n && i-k >= 0 && s[i+k-1] == s[i-k])  ++k;
        d2[i] = --k;
        if (i+k-1 > r)
            l = i-k,  r = i+k-1;
        while(k > 0)
        {
            a[min(i-k, i+k-1)][max(i-k, i+k-1) ]++;
            k--;
        }
    }

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(i > 0 && j > 0)
                a[i][j] -= a[i-1][j-1];
            if(i > 0)
                a[i][j] += a[i-1][j];
            if(j > 0)
                a[i][j] += a[i][j-1];
        }
    }

    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%d%d", &l, &r);
        l--;
        r--;
        printf("%d\n", a[r][r] - ((l > 0) ? a[l-1][r] - a[r][l-1] + a[l-1][l-1] : 0));
    }

    return 0;
}