/*
      H   H
       \ /
        C
       / \
      H   H
*/

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll, ll>
#define vi          vector<ll>
#define vvi			vector<vi>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        998244353
#define rep(i,a,b)	for(ll i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define tr(c,i)		for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x)	((c).find(x) != (c).end())
#define cpresent(c,x)	(find(all(c),x) != (c).end())
#define int ll
using namespace std;

#define N  5005
int p = 31, m = 1e9+9, n;
int hash1[N], hash2[N], p_val[N], inv[N];

int power(int x, int y)
{
	if(y==0)
		return 1;
	int temp = power(x, y/2);

	if(y%2!=0)
		return (((x*temp)%m)*temp)%m;
	else
		return (temp*temp)%m;
}

void preprocess(string s)
{
	int prev = 1;
	rep(i, 0, n)
	{
		p_val[i] = prev;
		prev = (prev*p)%m;
	}

	rep(i, 0, n)
	inv[i] = power(p_val[i], m-2);
}

void compute_hash(string s)
{
	int val = 0, val1 = 0;

	rep(i, 0, n)
	{
		val = (val + (s[i]-'a'+1)*p_val[i])%m;
		hash1[i] = val;
		val1 = (val1 + (s[n-i-1]-'a'+1)*p_val[i])%m;
		hash2[i] = val1;
	}
}

int gethash(int i, int j, int f)
{
	int p_inv = inv[i];
	if(!f)
		return (p_inv*((hash1[j] - (i>0?hash1[i-1]:0) +m)%m))%m;
	else
		return (p_inv*((hash2[j] - (i>0?hash2[i-1]:0) +m)%m))%m;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		string s;
		cin >> s;
		n = sz(s);

		preprocess(s);
		compute_hash(s);

		set< pii > s1;

		rep(i, 0, n)
		{
			rep(j, i, n)
			{
				if(gethash(i, j, 0)==gethash(n-j-1, n-i-1, 1))
				{
					s1.insert({i, j});
				}
			}
		}

		int dp[n][n];
		memset(dp, 0, sizeof(dp));

		rep(i, 0, n)
		{
			if(s1.find({i, i})!=s1.end())
			dp[i][i] = 1;
		}

		rep(i, 0, n-1)
		{
			int j = i+1;
			dp[i][j] = dp[i][i] + dp[j][j];
			if(s1.find({i, j})!=s1.end())
				dp[i][j]++;
		}

		rep(l, 3, n+1)
		{
			rep(i, 0, n-l+1)
			{
				int j = i+l-1;
				dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
				if(s1.find({i, j})!=s1.end())
					dp[i][j]++;
			}
		}

		int q;
		cin >> q;

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


/*
      H   H
       \ /
        C
       / \
      H   H
*/

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll, ll>
#define vi          vector<ll>
#define vvi			vector<vi>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        998244353
#define rep(i,a,b)	for(ll i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define tr(c,i)		for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x)	((c).find(x) != (c).end())
#define cpresent(c,x)	(find(all(c),x) != (c).end())
#define int ll
using namespace std;

#define N  5005
int p = 31, m = 1e9+9, n;
int hash1[N], hash2[N], p_val[N], inv[N];
int dp[25000001];

int power(int x, int y)
{
	if(y==0)
		return 1;
	int temp = power(x, y/2);

	if(y%2!=0)
		return (((x*temp)%m)*temp)%m;
	else
		return (temp*temp)%m;
}

void preprocess(string s)
{
	int prev = 1;
	rep(i, 0, n)
	{
		p_val[i] = prev;
		prev = (prev*p)%m;
	}

	rep(i, 0, n)
	inv[i] = power(p_val[i], m-2);
}

void compute_hash(string s)
{
	int val = 0, val1 = 0;

	rep(i, 0, n)
	{
		val = (val + (s[i]-'a'+1)*p_val[i])%m;
		hash1[i] = val;
		val1 = (val1 + (s[n-i-1]-'a'+1)*p_val[i])%m;
		hash2[i] = val1;
	}
}

int gethash(int i, int j, int f)
{
	int p_inv = inv[i];
	if(!f)
		return (p_inv*((hash1[j] - (i>0?hash1[i-1]:0) +m)%m))%m;
	else
		return (p_inv*((hash2[j] - (i>0?hash2[i-1]:0) +m)%m))%m;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		string s;
		cin >> s;
		n = sz(s);

		preprocess(s);
		compute_hash(s);

		set< pii > s1;

		rep(i, 0, n)
		{
			rep(j, i, n)
			{
				if(gethash(i, j, 0)==gethash(n-j-1, n-i-1, 1))
				{
					s1.insert({i, j});
				}
			}
		}

		memset(dp, 0, sizeof(dp));

		rep(i, 0, n)
		{
			if(s1.find({i, i})!=s1.end())
			dp[i*n+i] = 1;
		}

		rep(i, 0, n-1)
		{
			int j = i+1;
			dp[i*n+j] = dp[i*n+i] + dp[j*n+j];
			if(s1.find({i, j})!=s1.end())
				dp[i*n+j]++;
		}

		rep(l, 3, n+1)
		{
			rep(i, 0, n-l+1)
			{
				int j = i+l-1;
				dp[i*n+j] = dp[i*n+j-1] + dp[(i+1)*n+j] - dp[(i+1)*n+j-1];
				if(s1.find({i, j})!=s1.end())
					dp[i*n+j]++;
			}
		}

		int q;
		cin >> q;

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


/*
      H   H
       \ /
        C
       / \
      H   H
*/

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll, ll>
#define vi          vector<ll>
#define vvi			vector<vi>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        998244353
#define rep(i,a,b)	for(ll i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define tr(c,i)		for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x)	((c).find(x) != (c).end())
#define cpresent(c,x)	(find(all(c),x) != (c).end())
using namespace std;

#define N  5005
ll p = 31, m = 1e9+9, n;
int hash1[N], hash2[N], p_val[N], inv[N];
int dp[25000001];

ll power(ll x, ll y)
{
	if(y==0)
		return 1;
	ll temp = power(x, y/2);

	if(y%2!=0)
		return (((x*temp)%m)*temp)%m;
	else
		return (temp*temp)%m;
}

void preprocess(string s)
{
	ll prev = 1;
	rep(i, 0, n)
	{
		p_val[i] = (int)prev;
		prev = (prev*p)%m;
	}

	rep(i, 0, n)
	inv[i] = (int)power(p_val[i], m-2);
}

void compute_hash(string s)
{
	ll val = 0, val1 = 0;

	rep(i, 0, n)
	{
		val = (val + (ll)((s[i]-'a'+1)*(ll)p_val[i]))%m;
		hash1[i] = (int)val;
		val1 = (val1 + (ll)((s[n-i-1]-'a'+1)*(ll)p_val[i]))%m;
		hash2[i] = (int)val1;
	}
}

int gethash(int i, int j, int f)
{
	ll p_inv = inv[i];
	if(!f)
		return (p_inv*((ll)((hash1[j] - (i>0?hash1[i-1]:0) +m)%m)))%m;
	else
		return (p_inv*((ll)((hash2[j] - (i>0?hash2[i-1]:0) +m)%m)))%m;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		string s;
		cin >> s;
		n = sz(s);

		preprocess(s);
		compute_hash(s);

		set< pii > s1;

		rep(i, 0, n)
		{
			rep(j, i, n)
			{
				if(gethash(i, j, 0)==gethash(n-j-1, n-i-1, 1))
				{
					s1.insert({i, j});
				}
			}
		}

		memset(dp, 0, sizeof(dp));

		rep(i, 0, n)
		{
			if(s1.find({i, i})!=s1.end())
			dp[i*n+i] = 1;
		}

		rep(i, 0, n-1)
		{
			int j = i+1;
			dp[i*n+j] = dp[i*n+i] + dp[j*n+j];
			if(s1.find({i, j})!=s1.end())
				dp[i*n+j]++;
		}

		rep(l, 3, n+1)
		{
			rep(i, 0, n-l+1)
			{
				int j = i+l-1;
				dp[i*n+j] = dp[i*n+j-1] + dp[(i+1)*n+j] - dp[(i+1)*n+j-1];
				if(s1.find({i, j})!=s1.end())
					dp[i*n+j]++;
			}
		}

		int q;
		cin >> q;

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


/*
      H   H
       \ /
        C
       / \
      H   H
*/

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define	endl		'\n'
#define pii         pair<ll, ll>
#define vi          vector<ll>
#define vvi			vector<vi>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        998244353
#define rep(i,a,b)	for(ll i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define tr(c,i)		for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x)	((c).find(x) != (c).end())
#define cpresent(c,x)	(find(all(c),x) != (c).end())
using namespace std;

#define N  5005
ll p = 31, m = 1e9+9, n;
int hash1[N], hash2[N], p_val[N], inv[N];
int dp[25000001];
int f[25000001];

ll power(ll x, ll y)
{
	if(y==0)
		return 1;
	ll temp = power(x, y/2);

	if(y%2!=0)
		return (((x*temp)%m)*temp)%m;
	else
		return (temp*temp)%m;
}

void preprocess(string s)
{
	ll prev = 1;
	rep(i, 0, n)
	{
		p_val[i] = (int)prev;
		prev = (prev*p)%m;
	}

	rep(i, 0, n)
	inv[i] = (int)power(p_val[i], m-2);
}

void compute_hash(string s)
{
	ll val = 0, val1 = 0;

	rep(i, 0, n)
	{
		val = (val + (ll)((s[i]-'a'+1)*(ll)p_val[i]))%m;
		hash1[i] = (int)val;
		val1 = (val1 + (ll)((s[n-i-1]-'a'+1)*(ll)p_val[i]))%m;
		hash2[i] = (int)val1;
	}
}

int gethash(int i, int j, int f)
{
	ll p_inv = inv[i];
	if(!f)
		return (p_inv*((ll)((hash1[j] - (i>0?hash1[i-1]:0) +m)%m)))%m;
	else
		return (p_inv*((ll)((hash2[j] - (i>0?hash2[i-1]:0) +m)%m)))%m;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS;
	while(TESTS--)
	{
		string s;
		cin >> s;
		n = sz(s);

		preprocess(s);
		compute_hash(s);

		memset(f, 0, sizeof(f));

		rep(i, 0, n)
		{
			rep(j, i, n)
			{
				if(gethash(i, j, 0)==gethash(n-j-1, n-i-1, 1))
				{
					f[i*n+j] = 1;
				}
			}
		}

		memset(dp, 0, sizeof(dp));

		rep(i, 0, n)
		{
			dp[i*n+i] = f[i*n+i];
		}

		rep(i, 0, n-1)
		{
			int j = i+1;
			dp[i*n+j] = dp[i*n+i] + dp[j*n+j];
			dp[i*n+j]+=f[i*n+j];
		}

		rep(l, 3, n+1)
		{
			rep(i, 0, n-l+1)
			{
				int j = i+l-1;
				dp[i*n+j] = dp[i*n+j-1] + dp[(i+1)*n+j] - dp[(i+1)*n+j-1];
				dp[i*n+j]+=f[i*n+j];
			}
		}

		int q;
		cin >> q;

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


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, dp[maxn][maxn], pre[maxn][maxn];
int h[maxn], h1[maxn], a[maxn], a1[maxn];
string s;

int get(int l, int r){
	if(l == 0) return h[r];
	int y = (h[r] - ((ll)h[l - 1] * a[r - l + 1] % M) + M) % M;
	return y;
}

int get1(int l, int r){
	if(l == 0) return h1[r];
	int y = (h1[r] - ((ll)h1[l - 1] * a1[r - l + 1] % M) + M) % M;
	return y;
}

bool check(int l, int r){
	//int z = (r - l + 1) / 2;
	//if(z == 0 or (get(l, l + z - 1) == get(r, r - z + 1) and get1(l, l + z - 1) == get1(r, r - z + 1)))
	//	return 1;
	while(l < r)
		if(s[l] != s[r])
			return 0;
		else
			l++, r--;
	return 1;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	//cout << s.size() << endl;
	n = s.size();
	h[0] = s[0], a[0] = 1;
	h1[0] = s[0], a1[0] = 1;
	for(int i = 1; i < n + 5; i++){
		a[i] = ((ll)a[i - 1] * b) % M, h[i] = ((ll)h[i - 1] * b % M + s[i]) % M;
		a1[i] = ((ll)a1[i - 1] * b1) % M, h1[i] = ((ll)h1[i - 1] * b1 % M + s[i]) % M;
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
		}
	}
	/*for(int i = 0; i < n; i++)
		for(int j = i; j < n; j++)
			cout << i << " " << j << " " << dp[i][j] << endl;*/
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, dp[maxn][maxn], pre[maxn][maxn];
int h[maxn], h1[maxn], a[maxn], a1[maxn];
string s;

int get(int l, int r){
	if(l == 0) return h[r];
	int y = (h[r] - ((ll)h[l - 1] * a[r - l + 1] % M) + M) % M;
	return y;
}

int get1(int l, int r){
	if(l == 0) return h1[r];
	int y = (h1[r] - ((ll)h1[l - 1] * a1[r - l + 1] % M) + M) % M;
	return y;
}

bool check(int l, int r){
	int z = (r - l + 1) / 2;
	if(z == 0 or (get(l, l + z - 1) == get(r - z + 1, r) and get1(l, l + z - 1) == get1(r - z + 1, r)))
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	h[0] = s[0], a[0] = 1;
	h1[0] = s[0], a1[0] = 1;
	for(int i = 1; i < n + 5; i++){
		a[i] = ((ll)a[i - 1] * b) % M, h[i] = ((ll)h[i - 1] * b % M + s[i]) % M;
		a1[i] = ((ll)a1[i - 1] * b1) % M, h1[i] = ((ll)h1[i - 1] * b1 % M + s[i]) % M;
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
		}
	}
	/*for(int i = 0; i < n; i++)
		for(int j = i; j < n; j++)
			cout << i << " " << j << " " << dp[i][j] << endl;*/
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, dp[maxn][maxn], pre[maxn][maxn], pal[maxn][maxn];
string s;

bool check(int l, int r){
	if(pal[l][r])
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n; j++){
			if(i + j - 1 > n - 1)
				break;
			if(i == 1)
				pal[j][j] = 1;
			else{
				if(s[j] == s[i + j - 1] and (pal[j + 1][i + j - 2] or i == 2))
					pal[j][i + j - 1] = 1;
			}
		}
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
			//cout << i << " " << j << " " << pal[i][j] << endl;
		}
	}
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, now, bef, dp[maxn][maxn], pre[2][maxn], pal[maxn][maxn];
string s;

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n - i + 1; j++){
			if(i == 1)
				pal[j][j] = 1;
			else{
				if(s[j] == s[i + j - 1] and (pal[j + 1][i + j - 2] or i == 2))
					pal[j][i + j - 1] = 1;
			}
		}
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(pal[j][i])
				dp[0][i]++;
		if(pal[0][i])
			pre[0][i]++;
	}
	now = 1;
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[now][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[now][j] = pre[now][j - 1];
			if(pal[i][j])
				pre[now][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[bef][j];
		}
		swap(now, bef);
	}
	for(int f = 0; f < q; f++){
		scanf("%d %d",&l,&r);
		printf( "%d\n", dp[l-1][r-1]);
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, now, bef, dp[maxn][maxn], pre[2][maxn], pal[maxn][maxn];
string s;

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n - i + 1; j++){
			if(i == 1)
				pal[j][j] = 1;
			else{
				if(s[j] == s[i + j - 1] and (pal[j + 1][i + j - 2] or i == 2))
					pal[j][i + j - 1] = 1;
			}
		}
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(pal[j][i])
				dp[0][i]++;
		if(pal[0][i])
			pre[0][i]++;
	}
	now = 1;
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[now][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[now][j] = pre[now][j - 1];
			if(pal[i][j])
				pre[now][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[bef][j];
		}
		swap(now, bef);
	}
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, dp[maxn][maxn], pre[maxn][maxn];
int h[maxn], h1[maxn], a[maxn], a1[maxn];
string s;

int get(int l, int r){
	if(l == 0) return h[r];
	int y = (h[r] - ((ll)h[l - 1] * a[r - l + 1] % M) + M) % M;
	return y;
}

int get1(int l, int r){
	if(l == 0) return h1[r];
	int y = (h1[r] - ((ll)h1[l - 1] * a1[r - l + 1] % M) + M) % M;
	return y;
}

bool check(int l, int r){
	int z = (r - l + 1) / 2;
	if(z == 0 or (get(l, l + z - 1) == get(r, r - z + 1) and get1(l, l + z - 1) == get1(r, r - z + 1)))
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	h[0] = s[0], a[0] = 1;
	h1[0] = s[0], a1[0] = 1;
	for(int i = 1; i < n + 5; i++){
		a[i] = ((ll)a[i - 1] * b) % M, h[i] = ((ll)h[i - 1] * b % M + s[i]) % M;
		a1[i] = ((ll)a1[i - 1] * b1) % M, h1[i] = ((ll)h1[i - 1] * b1 % M + s[i]) % M;
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
		}
	}
	/*for(int i = 0; i < n; i++)
		for(int j = i; j < n; j++)
			cout << i << " " << j << " " << dp[i][j] << endl;*/
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;

int l, r, n, q, dp[maxn][maxn], pre[maxn][maxn];
int h[maxn], h1[maxn], a[maxn], a1[maxn];
string s;

int get(int l, int r){
	if(l == 0) return h[r];
	return (h[r] - ((ll)h[l - 1] * a[r - l + 1] % M) + M) % M;
}

bool check(int l, int r){
	int z = (r - l + 1) / 2;
	if(z == 0 or get(l, l + z - 1) == get(r, r - z + 1))
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	h[0] = s[0], a[0] = 1;
	for(int i = 1; i < n + 5; i++)
		a[i] = ((ll)a[i - 1] * b) % M, h[i] = ((ll)h[i - 1] * b % M + s[i]) % M;
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
		}
	}
	/*for(int i = 0; i < n; i++)
		for(int j = i; j < n; j++)
			cout << i << " " << j << " " << dp[i][j] << endl;*/
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, now, bef, dp[maxn][maxn], pre[2][maxn], pal[maxn][maxn];
string s;

int main(){
	cin >> s;
	scanf("%d",&q);
	n = s.size();
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n - i + 1; j++)
			if(i == 1)
				pal[j][j] = 1;
			else if(s[j] == s[i + j - 1] and (pal[j + 1][i + j - 2] or i == 2))
				pal[j][i + j - 1] = 1;
		if(i == n)
			break;
			pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(pal[j][i])
				dp[0][i]++;
		if(pal[0][i])
			pre[0][i]++;
	}
	now = 1;
	//cout << "salam2" << endl;
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[now][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[now][j] = pre[now][j - 1];
			if(pal[i][j])
				pre[now][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[bef][j];
		}
		swap(now, bef);
	}
	//cout << "salam3" << endl;
	for(int f = 0; f < q; f++){
		scanf("%d %d",&l,&r);
		printf( "%d\n", dp[l-1][r-1]);
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;

int l, r, n, q, h[maxn], dp[maxn][maxn], pre[maxn][maxn], a[maxn];
int h[maxn], h1[maxn], a[maxn], a1[maxn];
string s;

int get(int l, int r){
	if(l == 0) return h[r];
	return (h[r] - ((ll)h[l - 1] * a[r - l + 1] % M) + M) % M;
}

bool check(int l, int r){
	int z = (r - l + 1) / 2;
	if(z == 0 or get(l, l + z - 1) == get(r, r - z + 1))
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	h[0] = s[0], a[0] = 1;
	for(int i = 1; i < n + 5; i++)
		a[i] = ((ll)a[i - 1] * b) % M, h[i] = ((ll)h[i - 1] * b % M + s[i]) % M;
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
		}
	}
	/*for(int i = 0; i < n; i++)
		for(int j = i; j < n; j++)
			cout << i << " " << j << " " << dp[i][j] << endl;*/
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 3;

int l, r, n, q, h[maxn], dp[maxn][maxn], pre[maxn][maxn], a[maxn];
vector<int> vr;
vector<int> vl;
string s;

int get(int l, int r){
	if(l == 0) return h[r];
	return (h[r] - ((ll)h[l - 1] * a[r - l + 1] % M) + M) % M;
}

bool check(int l, int r){
	int z = (r - l + 1) / 2;
	if(z == 0 or get(l, l + z - 1) == get(r, r - z + 1))
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	h[0] = s[0], a[0] = 1;
	for(int i = 1; i < n + 5; i++)
		a[i] = ((ll)a[i - 1] * b) % M, h[i] = ((ll)h[i - 1] * b % M + s[i]) % M;
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
		}
	}
	/*for(int i = 0; i < n; i++)
		for(int j = i; j < n; j++)
			cout << i << " " << j << " " << dp[i][j] << endl;*/
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, dp[maxn][maxn], pre[maxn][maxn];
int h[maxn], h1[maxn], a[maxn], a1[maxn];
string s;

int get(int l, int r){
	if(l == 0) return h[r];
	int y = (h[r] - ((ll)h[l - 1] * a[r - l + 1] % M) + M) % M;
	return y;
}

int get1(int l, int r){
	if(r == n - 1) return h1[l];
	int y = (h1[l] - (ll)h1[r + 1] * a[r - l + 1] % M + M) % M;
	return y;
}

bool check(int l, int r){
	int z = (r - l + 1) / 2;
	if(z == 0 or get(l, l + z - 1) == get1(r - z + 1, r))
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	h[0] = s[0], a[0] = 1;
	h1[n - 1] = s[n - 1];
	for(int i = 1; i < n; i++)
		a[i] = ((ll)a[i - 1] * b) % M, h[i] = ((ll)h[i - 1] * b % M + s[i]) % M;
	for(int i = n - 2; i >= 0; i--)
		h1[i] = ((ll)h1[i + 1] * b % M + s[i]) % M;
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
		}
	}
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
		/*if(check(l, r))
			cout << "YES" << endl;
		else
		cout << "NO" << endl;*/
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, dp[maxn][maxn], pre[maxn][maxn];
int h[maxn], h1[maxn], a[maxn], a1[maxn];
string s;

int get(int l, int r){
	if(l == 0) return h[r];
	int y = (h[r] - ((ll)h[l - 1] * a[r - l + 1] % M) + M) % M;
	return y;
}

int get1(int l, int r){
	if(l == 0) return h1[r];
	int y = (h1[r] - ((ll)h1[l - 1] * a1[r - l + 1] % M) + M) % M;
	return y;
}

bool check(int l, int r){
	int z = (r - l + 1) / 2;
	if(z == 0 or (get(l, l + z - 1) == get(r, r - z + 1) and get1(l, l + z - 1) == get1(r - z + 1, r)))
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	h[0] = s[0], a[0] = 1;
	h1[0] = s[0], a1[0] = 1;
	for(int i = 1; i < n + 5; i++){
		a[i] = ((ll)a[i - 1] * b) % M, h[i] = ((ll)h[i - 1] * b % M + s[i]) % M;
		a1[i] = ((ll)a1[i - 1] * b1) % M, h1[i] = ((ll)h1[i - 1] * b1 % M + s[i]) % M;
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(check(j, i))
				dp[0][i]++;
		if(check(0, i))
			pre[0][i]++;
	}
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[i][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[i][j] = pre[i][j - 1];
			if(check(i, j))
				pre[i][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[i - 1][j];
		}
	}
	/*for(int i = 0; i < n; i++)
		for(int j = i; j < n; j++)
			cout << i << " " << j << " " << dp[i][j] << endl;*/
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, now, bef, dp[maxn][maxn], pre[2][maxn], pal[maxn][maxn];
string s;

bool check(int l, int r){
	if(pal[l][r])
		return 1;
	return 0;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n; j++){
			if(i + j - 1 > n - 1)
				break;
			if(i == 1)
				pal[j][j] = 1;
			else{
				if(s[j] == s[i + j - 1] and (pal[j + 1][i + j - 2] or i == 2))
					pal[j][i + j - 1] = 1;
			}
		}
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(pal[j][i])
				dp[0][i]++;
		if(pal[0][i])
			pre[0][i]++;
	}
	now = 1;
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[now][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[now][j] = pre[now][j - 1];
			if(pal[i][j])
				pre[now][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[bef][j];
			//cout << i << " " << j << " " << pal[i][j] << endl;
		}
		swap(now, bef);
	}
	for(int f = 0; f < q; f++){
		cin >> l >> r, l--, r--;
		cout << dp[l][r] << endl;
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, now, bef, dp[maxn][maxn], pre[2][maxn], pal[maxn][maxn];
string s;

int main(){
	//ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s;
	scanf("%I64d",&q);
	n = s.size();
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n - i + 1; j++)
			if(i == 1)
				pal[j][j] = 1;
			else if(s[j] == s[i + j - 1] and (pal[j + 1][i + j - 2] or i == 2))
				pal[j][i + j - 1] = 1;
		if(i == n)
			break;
			pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(pal[j][i])
				dp[0][i]++;
		if(pal[0][i])
			pre[0][i]++;
	}
	now = 1;
	//cout << "salam2" << endl;
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[now][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[now][j] = pre[now][j - 1];
			if(pal[i][j])
				pre[now][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[bef][j];
		}
		swap(now, bef);
	}
	//cout << "salam3" << endl;
	for(int f = 0; f < q; f++){
		scanf("%I64d %I64d",&l,&r);
		printf( "%I64d\n", dp[l-1][r-1]);
	}
	return 0;
}


//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std ;
typedef long long ll;
typedef pair<int,int> pii;

const ll maxn = 5e3;
const ll INF = 4e18 ;
const int M = 1e9 + 7;
const int b = 727;
const int b1 = 733;

int l, r, n, q, now, bef, dp[maxn][maxn], pre[2][maxn], pal[maxn][maxn];
string s;

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> s >> q;
	n = s.size();
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n - i + 1; j++){
			if(i == 1)
				pal[j][j] = 1;
			else{
				if(s[j] == s[i + j - 1] and (pal[j + 1][i + j - 2] or i == 2))
					pal[j][i + j - 1] = 1;
			}
		}
	}
	dp[0][0] = pre[0][0] = 1;
	for(int i = 1; i < n; i++){
		pre[0][i] = pre[0][i - 1];
		dp[0][i] = dp[0][i - 1];
		for(int j = 0; j <= i; j++)
			if(pal[j][i])
				dp[0][i]++;
		if(pal[0][i])
			pre[0][i]++;
	}
	now = 1;
	for(int i = 1; i < n; i++){
		dp[i][i] = 1;
		pre[now][i] = 1;
		for(int j = i + 1; j < n; j++){
			pre[now][j] = pre[now][j - 1];
			if(pal[i][j])
				pre[now][j]++;
			dp[i][j] = dp[i - 1][j];
			dp[i][j] -= pre[bef][j];
		}
		swap(now, bef);
	}
	for(int f = 0; f < q; f++){
		cin >> l >> r;
		//scanf("%d", l);
		//scanf("%d", r);
		//cout << dp[l - 1][r - 1] << "\n";
		printf( "%d\n", dp[l-1][r-1]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long mod = 1e9+7;

int xarisxi(int a, int b){
   a = a % mod;
   int ans = 1;
   while(b){
     if(b % 2 == 1){
        ans = ans * a % mod;
     }
     b /= 2;
     a = a * a % mod;
   }
   return ans;
}

long long a,b,q,r,l,m,pre[100000],sum,ans[5000][5000];

string s;

int main(){
   cin>>s;
   a=s.size();
   m = 1;
   for(int i = 1; i <= a; i++){
       pre[i]+=(pre[i-1]+m*(s[i-1]-'a'+1)%mod)%mod;
       m = xarisxi(27,i);
   }
   for(int i = 1; i <= a; i++){
       sum = 0;
       for(int j = i; j <= a; j++){
           if(((pre[i+(i+j)/2]-pre[i-1])/xarisxi(27,i-1)+mod)%mod==((pre[i+j]-pre[i+(i+j)/2-1])/xarisxi(27,i+(i+j)/2-1)+mod)%mod){
              sum++;
           }
           ans[i][j] = sum;
       }
   }
   cin>>q;
   while(q--){
     cin>>r>>l;
     cout<<ans[r][l]<<endl;
   }
}


#include <bits/stdc++.h>
using namespace std;
string s ;
int q;
int dist[5010][5010];
int pa[5010][5010];
int main(){
    ios_base::sync_with_stdio(false) ;
 	cin.tie(nullptr) ;
    cin >> s;
    //scanf("%s",s);
    int r = s.size();
    for(int i=0;i<r;i++){
        for(int j=0;j<i;j++){
            if(s[i] == s[j] && (j+1 == i || pa[j+1][i-1] == 1))
                pa[j][i] =1;
        }
        pa[i][i] = 1;
        dist[i][i] = 1;
    }
    for(int i=0;i+1<r;i++)
        dist[i][i+1] = 2 + pa[i][i+1];
    for(int j = 2;j<r;j++){
        for(int i=0;i+j<r;i++){
            dist[i][i+j]+=pa[i][i+j];
            dist[i][i+j]+=dist[i][i+j-1];
            dist[i][i+j]+=dist[i+1][i+j];
            dist[i][i+j]-=dist[i+1][i+j-1];
          //  cout << i << " " << j << " " <<dist[i][i+j] << endl;
        }
    }
    int a,b,q;
    //scanf("%d",&q);
    cin >> q;
    while(q--){
        cin >> a >> b;
        //scanf("%d%d",&a,&b);
        a--;
        b--;
        cout << dist[a][b] << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

long long dp[5005][5005];

void countSubstring(string str,int n)
{
    memset(dp,0,sizeof(dp));
    bool P[n+5][n+5];
    memset(P,false,sizeof(P));
    for (int i=0; i<n; i++)
    {
        P[i][i]=true;
        dp[i][i]=1;
        P[i+1][i]=true;
    }

    for (int gap=2; gap<=n; gap++)
    {
        for (int i=0;i<=n-gap;i++)
        {
            int j=gap+i-1;
            if (str[i]==str[j] && P[i+1][j-1]==true)
                P[i][j]=true;
            if (P[i][j])
                dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
            else
                dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
        }
    }
}

int main()
{
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

int dp[5005][5005];
bool P[5005][5005];

void countSubstring(string str,int n)
{
    for (int i=n-1;i>=0;i--){
		for (int j=i;j<n;j++) {
			P[i][j] = (str[i]==str[j] && (i+1>=j || P[i+1][j-1]));
			if(i==j) dp[i][j]=1;
            else dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] + P[i][j];
		}
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

long long dp[5005][5005];

void countSubstring(string str,int n)
{
    memset(dp,0,sizeof(dp));
    bool P[n][n];
    memset(P,false,sizeof(P));
    for (int i=0; i<n; i++)
    {
        P[i][i]=true;
        dp[i][i]=1;
        P[i+1][i]=true;
    }

    for (int gap=2; gap<=n; gap++)
    {
        for (int i=0;i<=n-gap;i++)
        {
            int j=gap+i-1;
            if (str[i]==str[j] && P[i+1][j-1]==true)
                P[i][j]=true;
            if (P[i][j])
                dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
            else
                dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
        }
    }
}

int main()
{
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

long long dp[5000][5000];

void countSubstring(string str,int n)
{
    memset(dp,0,sizeof(dp));
    bool P[n][n];
    memset(P,false,sizeof(P));
    for (int i=0; i<n; i++)
    {
        P[i][i]=true;
        dp[i][i]=1;
    }
    for (int i=0; i<n-1; i++)
    {
        if (str[i]==str[i+1])
        {
            P[i][i+1]=true;
            dp[i][i+1]=3;
        }
        else dp[i][i+1]=2;
    }

    for (int gap=2; gap<n; gap++)
    {
        for (int i=0;i<n-gap;i++)
        {
            int j=gap+i;
            if (str[i]==str[j] && P[i+1][j-1]==true)
                P[i][j]=true;
            if (P[i][j]==true)
                dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
            else
                dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
        }
    }
}

int main()
{
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

long long dp[5005][5005];
int P[5005][5005];

void countSubstring(string str,int n)
{
    for (int i=0; i<n; i++)
    {
        P[i][i]=1;
        dp[i][i]=1;
        P[i+1][i]=1;
    }

    for (int gap=2; gap<=n; gap++)
    {
        for (int i=0;i<=n-gap;i++)
        {
            int j=gap+i-1;
            P[i][j]=(str[i]==str[j] & P[i+1][j-1]);
            dp[i][j]=dp[i][j-1]+dp[i+1][j]+P[i][j]-dp[i+1][j-1];
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

long long dp[5000][5000];

void countSubstring(string str,int n)
{
    memset(dp,0,sizeof(dp));
    bool P[n][n];
    memset(P,false,sizeof(P));
    for (int i=0; i<n; i++)
    {
        P[i][i]=true;
        dp[i][i]=1;
        P[i+1][i]=true;
    }

    for (int gap=2; gap<=n; gap++)
    {
        for (int i=0;i<=n-gap;i++)
        {
            int j=gap+i-1;
            if (str[i]==str[j] && P[i+1][j-1]==true)
                P[i][j]=true;
            if (P[i][j])
                dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
            else
                dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
        }
    }
}

int main()
{
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

long long dp[5000][5000];

void countSubstring(string str,int n)
{
    memset(dp,0,sizeof(dp));
    bool P[n][n];
    memset(P,false,sizeof(P));
    for (int i=0; i<n; i++)
    {
        P[i][i]=true;
        dp[i][i]=1;
    }
    for (int i=0; i<n-1; i++)
    {
        if (str[i]==str[i+1])
        {
            P[i][i+1]=true;
            dp[i][i+1]=3;
        }
        else dp[i][i+1]=2;
    }

    for (int gap=2; gap<n; gap++)
    {
        for (int i=0;i<n-gap;i++)
        {
            int j=gap+i;
            if (str[i]==str[j] && P[i+1][j-1]==true)
                P[i][j]=true;
            if (P[i][j]==true)
                dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
            else
                dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
        }
    }
}

long long solve(int l,int r){
    return dp[l-1][r-1];
}

int main()
{
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<solve(l,r)<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

int dp[5005][5005];
bool P[5005][5005];

void countSubstring(string str,int n)
{
    for (int i=0;i<n;i++){
		for (int j=i;j<n;j++) {
			P[i][j] = (str[i]==str[j] && (i+1>=j || P[i+1][j-1]));
			dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] + P[i][j];
		}
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

int dp[5005][5005];
bool P[5005][5005];

void countSubstring(string str,int n)
{
    for (int i=0;i<n;i++){
		for (int j=i;j<n;j++) {
			P[i][j] = (str[i]==str[j] && (i+1>=j || P[i+1][j-1]));
			if(j>i) dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] + P[i][j];
			else dp[i][j]= P[i][j];
		}
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

long long dp[5005][5005];

void countSubstring(string str,int n)
{
    memset(dp,0,sizeof(dp));
    bool P[n+5][n+5];
    memset(P,false,sizeof(P));
    for (int i=0; i<n; i++)
    {
        P[i][i]=true;
        dp[i][i]=1;
        P[i+1][i]=true;
    }

    for (int gap=2; gap<=n; gap++)
    {
        for (int i=0;i<=n-gap;i++)
        {
            int j=gap+i-1;
            P[i][j]=(str[i]==str[j] & P[i+1][j-1]);
            if (P[i][j])
                dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
            else
                dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

int dp[5005][5005];
bool P[5005][5005];

void countSubstring(string str,int n)
{
    for (int i=0; i<n; i++)
    {
        P[i][i]=1;
        dp[i][i]=1;
        P[i+1][i]=1;
    }

    for (int gap=2; gap<=n; gap++)
    {
        for (int i=0;i<=n-gap;i++)
        {
            int j=gap+i-1;
            P[i][j]=(str[i]==str[j] & P[i+1][j-1]);
            dp[i][j]=dp[i][j-1]+dp[i+1][j]+P[i][j]-dp[i+1][j-1];
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

long long dp[5005][5005];

void countSubstring(string str,int n)
{
    memset(dp,0,sizeof(dp));
    int P[n+5][n+5];
    memset(P,0,sizeof(P));
    for (int i=0; i<n; i++)
    {
        P[i][i]=1;
        dp[i][i]=1;
        P[i+1][i]=1;
    }

    for (int gap=2; gap<=n; gap++)
    {
        for (int i=0;i<=n-gap;i++)
        {
            int j=gap+i-1;
            P[i][j]=(str[i]==str[j] & P[i+1][j-1]);
            dp[i][j]=dp[i][j-1]+dp[i+1][j]+P[i][j]-dp[i+1][j-1];
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    string s;
    cin>>s;
    countSubstring(s,s.size());
    int q;
    cin>>q;
    while(q--){
        int r,l;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

int dp[5005][5005];
bool P[5005][5005];

void countSubstring(string str,int n)
{
    for (int i=n-1;i>=0;i--){
		for (int j=i;j<n;j++) {
			P[i][j] = (str[i]==str[j] && (i+1>=j || P[i+1][j-1]));
			if(i==j) dp[i][j]=1;
            else dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] + P[i][j];
		}
    }
}

int main()
{
    char s[5005];
    scanf("%s",s);
    countSubstring(s,strlen(s));
    int q;
    scanf("%d",&q);
    while(q--){
        int r,l;
        scanf("%d%d",&l,&r);
        printf("%d\n",dp[l-1][r-1]);
    }

}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5e3 + 10, MAXQ = 1e6 + 10;
string s;
int n, q;
int isPal[ MAXN ][ MAXN ], DP[ MAXN ][ MAXN ];

void build(int l, int r) {
  if(r - l == 1) isPal[ l ][ r ] = 1;
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    isPal[ l ][ r ] = 1;
  }
}

int ans[ MAXQ ];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(int i = 0; i < n; i++) DP[ i ][ i + 1 ] = 1;
  for(int len = 2; len <= n; len++) {
    for(int l = 0; l + len <= n; l++) {
      int r = l + len;
      DP[ l ][ r ] = DP[ l ][ r - 1 ] + DP[ l + 1 ][ r ] - DP[ l + 1 ][ r - 1 ] + isPal[ l ][ r ];
    }
  }
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    ans[ i ] = DP[ a - 1 ][ b ];
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5e3 + 10, MAXQ = 1e6 + 10;
string s;
int n, q;
int isPal[ MAXN ][ MAXN ], DP[ MAXN ][ MAXN ];

void build(int l, int r) {
  if(r - l == 1) isPal[ l ][ r ] = 1;
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    isPal[ l ][ r ] = 1;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(int i = 0; i < n; i++) DP[ i ][ i + 1 ] = 1;
  for(int len = 2; len <= n; len++) {
    for(int l = 0; l + len <= n; l++) {
      int r = l + len;
      DP[ l ][ r ] = DP[ l ][ r - 1 ] + DP[ l + 1 ][ r ] - DP[ l + 1 ][ r - 1 ] + isPal[ l ][ r ];
    }
  }
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    cout << DP[ a - 1 ][ b ] << '\n';
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

const int MAXN = 5e3 + 10, MAXQ = 1e6 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXQ ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    assert(pos >= 0 && pos < sz);
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    assert(pos >= 0 && pos < sz);
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    for(int cur: even_I[ i ]) FT.add(cur, 1);

    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) {ans[ ind ] = cnt - FT.sum(queries[ ind ].x); }
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

const int MAXN = 5e3 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXN ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    assert(pos >= 0 && pos < sz);
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    assert(pos >= 0 && pos < sz);
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  if(n > 100) cout << "BOK ";
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  if(n > 100) cout << "OPET ";
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    if(n > 100) cout << "a ";
    for(int cur: even_I[ i ]) FT.add(cur, 1);

    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) {ans[ ind ] = cnt - FT.sum(queries[ ind ].x); }
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

const int MAXN = 5e3 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXN ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    assert(pos >= 0 && pos < sz);
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    assert(pos >= 0 && pos < sz);
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    if(n > 100) cout << "a ";
    for(int cur: even_I[ i ]) FT.add(cur, 1);

    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) {assert(ind < q);ans[ ind ] = cnt - FT.sum(queries[ ind ].x); }
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= s.size() && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

enum type {RI, RQ};
const int MAXN = 5e3 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXN ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    for(int cur: even_I[ i ]) FT.add(cur, 1);
    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) {
      ans[ ind ] = cnt - FT.sum(queries[ ind ].x);
    }
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

const int MAXN = 5e3 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXN ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    assert(pos >= 0 && pos < sz);
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    assert(pos >= 0 && pos < sz);
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    for(int cur: even_I[ i ]) FT.add(cur, 1);
    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) {assert(ind < q);ans[ ind ] = cnt - FT.sum(queries[ ind ].x); if(n > 100) cout << ans[ ind ] << " ";}
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

const int MAXN = 5e3 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXN ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    assert(pos >= 0 && pos < sz);
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    assert(pos >= 0 && pos < sz);
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  cout << queries.size() << endl;
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    for(int cur: even_I[ i ]) FT.add(cur, 1);
    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) {assert(ind < q);ans[ ind ] = cnt - FT.sum(queries[ ind ].x);}
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

enum type {RI, RQ};
const int MAXN = 5e3 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXN ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    assert(pos >= 0 && pos < sz);
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    assert(pos >= 0 && pos < sz);
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    for(int cur: even_I[ i ]) FT.add(cur, 1);
    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) ans[ ind ] = cnt - FT.sum(queries[ ind ].x);
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

const int MAXN = 5e3 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXN ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    assert(pos >= 0 && pos < sz);
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    assert(pos >= 0 && pos < sz);
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  if(n > 100) cout << "BOK ";
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  if(n > 100) cout << "OPET ";
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    if(n > 100) cout << "a ";
    for(int cur: even_I[ i ]) FT.add(cur, 1);

    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) {assert(ind < q);ans[ ind ] = cnt - FT.sum(queries[ ind ].x); }
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct pii {
  int x, y;
  pii() {x = y = 0;}
  pii(int _x, int _y) {x = _x; y = _y;}
};
#define pb push_back
#define _ << " " <<

string s;

int q, n;
vector <pii> queries;
vector <pii> palin;

void build(int l, int r) {
  if(r - l == 1) palin.pb(pii(l, r));
  for(; l - 1 >= 0 && r + 1 <= n && s[ l - 1 ] == s[ r ];) {
    l--; r++;
    palin.pb(pii(l, r));
  }
}

const int MAXN = 5e3 + 10;
vector <int> even_Q[ MAXN ], even_I[ MAXN ]; ///INDEKS, LEFT BOUND
int ans[ MAXN ];

struct Logaritamska {
  int sz;
  vector <int> a;
  Logaritamska(int _sz) {
    sz = _sz + 1;
    a.resize(sz, 0);
  }

  void add(int pos, int val) {
    assert(pos >= 0 && pos < sz);
    pos++;
    for(; pos < sz; pos += pos & -pos) {
      a[ pos ] += val;
    }
  }

  int sum(int pos) {
    assert(pos >= 0 && pos < sz);
    if(pos == 0) return 0;
    int res = 0;
    for(; pos > 0; pos -= pos & -pos) {
      res += a[ pos ];
    }
    return res;
  }
} FT(MAXN);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> q;
  n = s.size();
  int a, b;
  for(int i = 0; i < q; i++) {
    cin >> a >> b;
    queries.pb(pii(a - 1, b));
    even_Q[ b ].pb(i);
  }
  for(int i = 0; i < n; i++) {
    build(i, i);
    build(i, i + 1);
  }
  for(pii cur: palin) {
    even_I[ cur.y ].pb(cur.x);
  }
  if(n > 100) cout << queries.size() << endl;
  int cnt = 0;
  for(int i = 0; i <= n; i++) {
    for(int cur: even_I[ i ]) FT.add(cur, 1);
    cnt += even_I[ i ].size();
    for(int ind: even_Q[ i ]) {assert(ind < q);ans[ ind ] = cnt - FT.sum(queries[ ind ].x);}
  }
  for(int i = 0; i < q; i++) cout << ans[ i ] << '\n';

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
ll gcd(ll a, ll b){ return !b ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a*(b / gcd(a, b)); }
#define all(a) a.begin(),a.end()
#define MOD 1000000007
#define mt(a,b,c) make_tuple(a,b,c)
#define ii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define N 5005

const ll base = 31;
int n;
ll po[N], pre[N], suf[N];
int dp[N][N];

ll invMod(ll n, ll p){
	if (!p)return 1;
	if (p & 1)return (n*invMod(n, p - 1)) % MOD;
	ll r = invMod(n, p >> 1ll) % MOD;
	return (r*r) % MOD;
}

int main(){
	po[0] = 1;
	for (int i = 1; i < N; ++i)po[i] = (po[i - 1] * base) % MOD;
	char s[5005], xx[5005];
	scanf(" %s", xx);
	strcpy(s, "#");
	strcat(s, xx);
	n = strlen(s);
	pre[0] = suf[0] = 0ll;
	for (int i = 1; i < n; ++i){
		pre[i] = pre[i - 1] + (s[i] - 96) * po[i] % MOD;
	}
	for (int i = n - 1, j = 1; i > 0; --i, ++j){
		suf[j] = suf[j - 1] + (s[i] - 96)*po[j] % MOD;
	}
	for (int i = 1; i < N; ++i)
		po[i] = invMod(po[i], MOD - 2) % MOD;
	for (int l = 1; l < n; ++l){
		for (int r = l; r < n; ++r){
			ll h = ((pre[r] - pre[l - 1])*po[l]) % MOD;
			int l1 = n - r, r1 = n - l;
			ll h1 = ((suf[r1] - suf[l1 - 1])*po[l1]) % MOD;
			dp[l][r] = (h1 == h);
		}
	}

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


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
ll gcd(ll a, ll b){ return !b ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a*(b / gcd(a, b)); }
#define all(a) a.begin(),a.end()
#define MOD 1000000007
#define mt(a,b,c) make_tuple(a,b,c)
#define ii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define N 5005

const ll base = 31;
int n;
ll po[N], pre[N], suf[N], dp[N][N];

ll invMod(ll n, ll p){
	if (!p)return 1;
	if (p & 1)return (n*invMod(n, p - 1)) % MOD;
	ll r = invMod(n, p >> 1ll) % MOD;
	return (r*r) % MOD;
}

int main(){
	po[0] = 1;
	for (int i = 1; i < N; ++i)po[i] = (po[i - 1] * base) % MOD;
	char s[5005], xx[5005];
	scanf(" %s", xx);
	strcpy(s, "#");
	strcat(s, xx);
	n = strlen(s);
	pre[0] = suf[0] = 0ll;
	for (int i = 1; i < n; ++i){
		pre[i] = pre[i - 1] + (s[i] - 96) * po[i] % MOD;
	}
	for (int i = n - 1, j = 1; i > 0; --i, ++j){
		suf[j] = suf[j - 1] + (s[i] - 96)*po[j] % MOD;
	}
	for (int i = 1; i < N; ++i)
		po[i] = invMod(po[i], MOD - 2) % MOD;
	for (int l = 1; l < n; ++l){
		for (int r = l; r < n; ++r){
			ll h = ((pre[r] - pre[l - 1])*po[l]) % MOD;
			int l1 = n - r, r1 = n - l;
			ll h1 = ((suf[r1] - suf[l1 - 1])*po[l1]) % MOD;
			dp[l][r] = (h1 == h);
		}
	}

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


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
ll gcd(ll a, ll b){ return !b ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a*(b / gcd(a, b)); }
#define all(a) a.begin(),a.end()
#define MOD 1000000007
#define mt(a,b,c) make_tuple(a,b,c)
#define ii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define N 5005

const ll base = 31;
int n;
ll po[N], dp[N][N], pre[N], suf[N], bit[N][N];

void update(int x, int y, int v){
	for (; x < n; x += (x&-x))for (int j = y; j < n; j += (j&-j))bit[x][j] += v;
}
int query(int x, int y){
	int ans = 0;
	for (; x > 0; x -= (x&-x))for (int j = y; j > 0; j -= (j&-j))
		ans += bit[x][j];
	return ans;
}

ll invMod(ll n, ll p){
	if (!p)return 1;
	if (p & 1)return (n*invMod(n, p - 1)) % MOD;
	ll r = invMod(n, p >> 1ll) % MOD;
	return (r*r) % MOD;
}

int main(){
	po[0] = 1;
	for (int i = 1; i < N; ++i)po[i] = (po[i - 1] * base) % MOD;
	char s[5005], xx[5005];
	scanf(" %s", xx);
	strcpy(s, "#");
	strcat(s, xx);
	n = strlen(s);
	pre[0] = suf[0] = 0ll;
	for (int i = 1; i < n; ++i){
		pre[i] = pre[i - 1] + (s[i] - 96) * po[i] % MOD;
	}
	for (int i = n - 1, j = 1; i > 0; --i, ++j){
		suf[j] = suf[j - 1] + (s[i] - 96)*po[j] % MOD;
	}

	for (int l = 1; l < n; ++l){
		for (int r = l; r < n; ++r){
			ll h = ((pre[r] - pre[l - 1])*invMod(po[l], MOD - 2)) % MOD;
			int l1 = n - r, r1 = n - l;
			ll h1 = ((suf[r1] - suf[l1 - 1])*invMod(po[l1], MOD - 2)) % MOD;
			dp[l][r] = (h1 == h);
		}
	}

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


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define N 5005

const ll base = 31;
int n;
ll po[N], pre[N], suf[N];
int bit[N][N];

void update(int x, int y, int v){
	for (; x < n; x += (x&-x))for (int j = y; j < n; j += (j&-j))bit[x][j] += v;
}
int query(int x, int y){
	int ans = 0;
	for (; x > 0; x -= (x&-x))for (int j = y; j > 0; j -= (j&-j))
		ans += bit[x][j];
	return ans;
}

ll invMod(ll n, ll p){
	if (!p)return 1;
	if (p & 1)return (n*invMod(n, p - 1)) % MOD;
	ll r = invMod(n, p >> 1ll) % MOD;
	return (r*r) % MOD;
}

int main(){
	po[0] = 1;
	for (int i = 1; i < N; ++i)po[i] = (po[i - 1] * base) % MOD;
	char s[5005], xx[5005];
	scanf(" %s", xx);
	strcpy(s, "#");
	strcat(s, xx);
	n = strlen(s);
	pre[0] = suf[0] = 0ll;
	for (int i = 1; i < n; ++i){
		pre[i] = pre[i - 1] + (s[i] - 96) * po[i] % MOD;
	}
	for (int i = n - 1, j = 1; i > 0; --i, ++j){
		suf[j] = suf[j - 1] + (s[i] - 96)*po[j] % MOD;
	}

	for (int l = 1; l < n; ++l){
		for (int r = l; r < n; ++r){
			ll h = ((pre[r] - pre[l - 1])*invMod(po[l], MOD - 2)) % MOD;
			int l1 = n - r, r1 = n - l;
			ll h1 = ((suf[r1] - suf[l1 - 1])*invMod(po[l1], MOD - 2)) % MOD;
			if (h1 == h)
				update(l, r, 1);
		}
	}
	int q; scanf("%d", &q);
	while (q--){
		int l, r; scanf("%d%d", &l, &r);
		printf("%d\n", query(r, r) + query(l - 1, l - 1) - query(l - 1, r) - query(r, l - 1));
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
ll gcd(ll a, ll b){ return !b ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a*(b / gcd(a, b)); }
#define all(a) a.begin(),a.end()
#define MOD 1000000007
#define mt(a,b,c) make_tuple(a,b,c)
#define ii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define N 5005

const ll base = 31;
int n;
ll po[N], pre[N], suf[N];
int dp[N][N];

ll invMod(ll n, ll p){
	if (!p)return 1;
	if (p & 1)return (n*invMod(n, p - 1)) % MOD;
	ll r = invMod(n, p >> 1ll) % MOD;
	return (r*r) % MOD;
}

int main(){
	po[0] = 1;
	for (int i = 1; i < N; ++i)po[i] = (po[i - 1] * base) % MOD;
	char s[5005], xx[5005];
	scanf(" %s", xx);
	strcpy(s, "#");
	strcat(s, xx);
	n = strlen(s);
	pre[0] = suf[0] = 0ll;
	for (int i = 1; i < n; ++i){
		pre[i] = (pre[i - 1] + (s[i] - 96) * po[i]) % MOD;
	}
	for (int i = n - 1, j = 1; i > 0; --i, ++j){
		suf[j] = (suf[j - 1] + (s[i] - 96) * po[j]) % MOD;
	}
	for (int i = 1; i < N; ++i)
		po[i] = invMod(po[i], MOD - 2) % MOD;
	for (int l = 1; l < n; ++l){
		for (int r = l; r < n; ++r){
			ll pr1 = (pre[r] - pre[l - 1] + MOD) % MOD;
			ll h = (pr1*po[l] + MOD) % MOD;
			int l1 = n - r, r1 = n - l;
			ll pr2 = (suf[r1] - suf[l1 - 1] + MOD) % MOD;
			ll h1 = (pr2*po[l1]) % MOD;
			dp[l][r] = (h1 == h);
		}
	}
	for (int i = 1; i < n; ++i){
		for (int j = 1; j < n; ++j){
			dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
		}
	}
	int q; scanf("%d", &q);
	while (q--){
		int l, r; scanf("%d%d", &l, &r);
		printf("%d\n", dp[r][r] + dp[l - 1][l - 1] - dp[l - 1][r] - dp[r][l - 1]);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
ll gcd(ll a, ll b){ return !b ? a : gcd(b, a%b); }
ll lcm(ll a, ll b){ return a*(b / gcd(a, b)); }
#define all(a) a.begin(),a.end()
#define MOD 1000000007
#define mt(a,b,c) make_tuple(a,b,c)
#define ii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define N 5005

const ll base = 31;
int n;
ll po[N], pre[N], suf[N];
int dp[N][N];

ll invMod(ll n, ll p){
	if (!p)return 1;
	if (p & 1)return (n*invMod(n, p - 1)) % MOD;
	ll r = invMod(n, p >> 1ll) % MOD;
	return (r*r) % MOD;
}

int main(){
	po[0] = 1;
	for (int i = 1; i < N; ++i)po[i] = (po[i - 1] * base) % MOD;
	char s[5005], xx[5005];
	scanf(" %s", xx);
	strcpy(s, "#");
	strcat(s, xx);
	n = strlen(s);
	pre[0] = suf[0] = 0ll;
	for (int i = 1; i < n; ++i){
		pre[i] = pre[i - 1] + (s[i] - 96) * po[i] % MOD;
	}
	for (int i = n - 1, j = 1; i > 0; --i, ++j){
		suf[j] = suf[j - 1] + (s[i] - 96)*po[j] % MOD;
	}

	for (int l = 1; l < n; ++l){
		for (int r = l; r < n; ++r){
			ll h = ((pre[r] - pre[l - 1])*invMod(po[l], MOD - 2)) % MOD;
			int l1 = n - r, r1 = n - l;
			ll h1 = ((suf[r1] - suf[l1 - 1])*invMod(po[l1], MOD - 2)) % MOD;
			dp[l][r] = (h1 == h);
		}
	}

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


#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define sz 200005
#define pi pair<ll,ll>
#define mod (ll)(1e9+7)
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
ll dp[5001][5001];
int main()
{
    ios::sync_with_stdio(false);
    for(int i = 0; i <= 5000; i++){
        dp[i][i] = 1;
    }

    string s;
    cin >> s;
    ll q;
    cin >> q;

    ll jump = 1;

    while(jump < s.size())
    {
       for (int i = 0; i < s.size(); i++)
       {
            ll j = i+jump;
            if(j > s.size())
                break;
            if(j - i == 1) {
                if(s[j] == s[i]){
                    dp[i][j] = 1;
                    continue;
                }
            }

            if(s[i] == s[j] and dp[i+1][j-1] == 1)
                dp[i][j] = 1;
       }
       jump++;
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }
    // vector<ll> final(5001);

    for(int i = 0; i < 5000; i++){
        for(int j = i+1; j < 5000; j++){
            dp[i][j] += dp[i][j-1];
        }
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }

    for(int j = 1; j < 5000; j++){
        ll ans = 0;
        for(int i = j-1; i >= 0; i--){
            dp[i][j] += dp[i+1][j];
        }
    }

    while(q--){
        ll l,r;
        cin >> l >> r;
        ll ans = dp[l-1][r-1];
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define sz 200005
#define pi pair<ll,ll>
#define mod (ll)(1e9+7)
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
ll dp[5001][5001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    for(int i = 0; i <= 5000; i++){
        dp[i][i] = 1;
    }

    string s;
    cin >> s;
    ll q;
    cin >> q;

    ll jump = 1;

    while(jump < s.size())
    {
       for (int i = 0; i < s.size(); i++)
       {
            ll j = i+jump;
            if(j > s.size())
                break;
            if(j - i == 1) {
                if(s[j] == s[i]){
                    dp[i][j] = 1;
                    continue;
                }
            }

            if(s[i] == s[j] and dp[i+1][j-1] == 1)
                dp[i][j] = 1;
       }
       jump++;
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }
    // vector<ll> final(5001);

    for(int i = 0; i < 5000; i++){
        for(int j = i+1; j < 5000; j++){
            dp[i][j] += dp[i][j-1];
        }
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }

    for(int j = 1; j < 5000; j++){
        ll ans = 0;
        for(int i = j-1; i >= 0; i--){
            dp[i][j] += dp[i+1][j];
        }
    }

    while(q--){
        ll l,r;
        cin >> l >> r;
        ll ans = dp[l-1][r-1];
        cout << ans << '\n';
    }
}


#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define sz 200005
#define pi pair<ll,ll>
#define mod (ll)(1e9+7)
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
ll dp[5001][5001];
int main()
{
    for(int i = 0; i <= 5000; i++){
        dp[i][i] = 1;
    }

    string s;
    cin >> s;
    ll q;
    cin >> q;

    ll jump = 1;

    while(jump < s.size())
    {
       for (int i = 0; i < s.size(); i++)
       {
            ll j = i+jump;
            if(j > s.size())
                break;
            if(j - i == 1) {
                if(s[j] == s[i]){
                    dp[i][j] = 1;
                    continue;
                }
            }

            if(s[i] == s[j] and dp[i+1][j-1] == 1)
                dp[i][j] = 1;
       }
       jump++;
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }
    // vector<ll> final(5001);

    for(int i = 0; i < 5000; i++){
        for(int j = i+1; j < 5000; j++){
            dp[i][j] += dp[i][j-1];
        }
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }

    for(int j = 1; j < 5000; j++){
        ll ans = 0;
        for(int i = j-1; i >= 0; i--){
            dp[i][j] += dp[i+1][j];
        }
    }

    while(q--){
        ll l,r;
        cin >> l >> r;
        ll ans = dp[l-1][r-1];
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define sz 200005
#define pi pair<ll,ll>
#define mod (ll)(1e9+7)
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
ll dp[5001][5001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    for(int i = 0; i <= 5000; i++){
        dp[i][i] = 1;
    }

    string s;
    cin >> s;
    ll q;
    cin >> q;

    ll jump = 1;

    while(jump < s.size())
    {
       for (int i = 0; i < s.size(); i++)
       {
            ll j = i+jump;
            if(j > s.size())
                break;
            if(j - i == 1) {
                if(s[j] == s[i]){
                    dp[i][j] = 1;
                    continue;
                }
            }

            if(s[i] == s[j] and dp[i+1][j-1] == 1)
                dp[i][j] = 1;
       }
       jump++;
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }
    // vector<ll> final(5001);

    for(int i = 0; i < 5000; i++){
        for(int j = i+1; j < 5000; j++){
            dp[i][j] += dp[i][j-1];
        }
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }

    for(int j = 1; j < 5000; j++){
        ll ans = 0;
        for(int i = j-1; i >= 0; i--){
            dp[i][j] += dp[i+1][j];
        }
    }

    while(q--){
        ll l,r;
        cin >> l >> r;
        ll ans = dp[l-1][r-1];
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define sz 200005
#define pi pair<ll,ll>
#define mod (ll)(1e9+7)
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
ll dp[5001][5001];
ll final[5001][5001];

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

    string s;
    cin >> s;
    ll q;
    cin >> q;

    ll jump = 1;

    while(jump < s.size())
    {
       for (int i = 0; i < s.size(); i++)
       {
            ll j = i+jump;
            if(j > s.size())
                break;
            if(j - i == 1) {
                if(s[j] == s[i]){
                    dp[i][j] = 1;
                    continue;
                }
            }

            if(s[i] == s[j] and dp[i+1][j-1] == 1)
                dp[i][j] = 1;
       }
       jump++;
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }
    // vector<ll> final(5001);

    for(int i = 0; i < 5000; i++){
        for(int j = i+1; j < 5000; j++){
            dp[i][j] += dp[i][j-1];
        }
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }

    for(int j = 1; j < 5000; j++){
        ll ans = 0;
        for(int i = j-1; i >= 0; i--){
            dp[i][j] += dp[i+1][j];
        }
    }

    while(q--){
        ll l,r;
        cin >> l >> r;
        ll ans = dp[l-1][r-1];
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define sz 200005
#define pi pair<ll,ll>
#define mod (ll)(1e9+7)
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
ll dp[5001][5001];
int main()
{
    ios::sync_with_stdio(false)
    for(int i = 0; i <= 5000; i++){
        dp[i][i] = 1;
    }

    string s;
    cin >> s;
    ll q;
    cin >> q;

    ll jump = 1;

    while(jump < s.size())
    {
       for (int i = 0; i < s.size(); i++)
       {
            ll j = i+jump;
            if(j > s.size())
                break;
            if(j - i == 1) {
                if(s[j] == s[i]){
                    dp[i][j] = 1;
                    continue;
                }
            }

            if(s[i] == s[j] and dp[i+1][j-1] == 1)
                dp[i][j] = 1;
       }
       jump++;
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }
    // vector<ll> final(5001);

    for(int i = 0; i < 5000; i++){
        for(int j = i+1; j < 5000; j++){
            dp[i][j] += dp[i][j-1];
        }
    }

    // for(int i = 0; i < 6; i++){
    //     for(int j = 0; j < 6; j++){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }

    for(int j = 1; j < 5000; j++){
        ll ans = 0;
        for(int i = j-1; i >= 0; i--){
            dp[i][j] += dp[i+1][j];
        }
    }

    while(q--){
        ll l,r;
        cin >> l >> r;
        ll ans = dp[l-1][r-1];
        cout << ans << endl;
    }
}


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

int main(){
    string s;
    cin >> s;

    int n = s.size();

    s = 'x'+s;

    vector< vector< int > > dp(n+1, vector<int>(n+1));

    for(int i = 1; i <= n; i++){
        int left, right;
        left = i;
        right = i;
        while(left > 0 && right <= n){
            if(s[left] == s[right]){
                dp[left][right]++;
            }
            else break;
            left--;
            right++;
        }
    }
    for(int i = 1; i < n; i++){
        int left, right;
        left = i;
        right = i+1;
        while(left > 0 && right <= n){
            if(s[left] == s[right]){
                dp[left][right]++;
            }
            else break;
            left--;
            right++;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            dp[i][j] += dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];
        }
    }
    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int a, b;
        scanf("%d%d", &a, &b);
        int ans = dp[b][b]-dp[a-1][b]-dp[b][a-1]+dp[a-1][a-1];
        printf("%d\n", ans);
    }
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
	long long maxElementIndex;
	int n, s1=0, Max=0;
	cin>>n;
	struct tese {
		string f, im;
		int s, b;
	} a[200];
	for (int i=0;i<n;++i)
	{
		cin>>a[i].f>>a[i].im>>a[i].s>>a[i].b;
		if (a[i].s==50)
		{
			if (a[i].b>Max)
				Max=a[i].b;
		}
	}
	for (int i=0;i<n;++i)
	{
		if (a[i].s==50)
		{
			if (a[i].b==Max)
				s1++;
		}
	}
	if (s1>2) cout<<s1;
	else for (int i=0;i<n;++i)
	{
		if (a[i].s==50 && a[i].b==Max)
			cout<<a[i].f<<" "<<a[i].im<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

string s;
int q;
int is[5555][5555]={};

int main() {
		ios::sync_with_stdio(false);
		//freopen("out.out","w",stdout);
		cin>>s;
		//for solve
		FOR(len,1,s.size()+1){
				for(int i=s.size()-len;i>=0;i--){
						int j=i+len-1;
						if(s[i]==s[j]){
								if(len<=3)is[i][j]=1;
								else {
										is[i][j]=is[i+1][j-1];
								}
						}
				}
		}

		FOR(i,0,s.size())FOR(j,i+1,s.size())is[i][j]+=is[i][j-1];

		FOR(i,1,s.size())FOR(j,0,s.size())is[i][j]+=is[i-1][j];

/*
		FOR(i,0,s.size()){
				FOR(j,0,s.size())cout<<is[i][j]<<" ";
				cout<<endl;
		}
		*/

		cin>>q;
		//for query
		FOR(i,0,q){
				int l,r;
				cin>>l>>r;
				l--;
				r--;
				cout<<is[r][r]-(l==0?0:is[l-1][r])<<endl;
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

string s;
int q;
int is[5555][5555]={};

int main() {
		ios::sync_with_stdio(false);
		cin.tie(0);
		//freopen("out.out","w",stdout);
		cin>>s;
		//for solve
		FOR(len,1,s.size()+1){
				for(int i=s.size()-len;i>=0;i--){
						int j=i+len-1;
						if(s[i]==s[j]){
								if(len<=3)is[i][j]=1;
								else {
										is[i][j]=is[i+1][j-1];
								}
						}
				}
		}

		FOR(i,0,s.size())FOR(j,i+1,s.size())is[i][j]+=is[i][j-1];

		FOR(i,1,s.size())FOR(j,0,s.size())is[i][j]+=is[i-1][j];

/*
		FOR(i,0,s.size()){
				FOR(j,0,s.size())cout<<is[i][j]<<" ";
				cout<<endl;
		}
		*/

		cin>>q;
		//for query
		FOR(i,0,q){
				int l,r;
				cin>>l>>r;
				l--;
				r--;
				cout<<is[r][r]-(l==0?0:is[l-1][r])<<endl;
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

char s[5555];
int q;
int is[5555][5555]={};

int main() {
		scanf("%s",s);
		int n=strlen(s);
		FOR(len,1,n+1){
				bool out=0;
				for(int i=n-len;i>=0;i--){
						int j=i+len-1;
						if(s[i]==s[j]){
								if(len<=3)is[i][j]=1,out=1;
								else {
										is[i][j]=is[i+1][j-1],out|=is[i][j];
								}
						}
				}
				if(!out)break;
		}

		FOR(i,0,n)FOR(j,i+1,n)is[i][j]+=is[i][j-1];

		FOR(i,1,n)FOR(j,i-1,n)is[i][j]+=is[i-1][j];

		scanf("%d",&q);
		FOR(i,0,q){
				int l,r;
				scanf("%d%d",&l,&r);
				l--;
				r--;
				printf("%d\n",is[r][r]-(l==0?0:is[l-1][r]));
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

string s;
int q;
int is[5555][5555]={};
int getint() {
		char c;
		while(c=getchar(),c<'0'||c>'9');
		int x=0;
		while(c>='0'&&c<='9') {
				x=x*10+c-48;
				c=getchar();
		}
		return x;
}

void putint(int x) {
		int len=0,data[20];
		while(x) {
				data[len++]=x%10;
				x/=10;
		}
		if(!len)
				data[len++]=0;
		while(len--)
				putchar(data[len]+48);
		putchar('\n');
}

int main() {
		cin>>s;
		int n=s.size();
		for(int i=n-1;i>=0;i--){
				bool out=0;
						FOR(len,1,n-i+1){
						int j=i+len-1;
						if(s[i]==s[j]){
								if(len<=3)is[i][j]=1,out=1;
								else {
										is[i][j]=is[i+1][j-1],out|=is[i][j];
								}
						}
				}
				if(!out)break;
		}
		FOR(i,0,n)FOR(j,i+1,n)is[i][j]+=is[i][j-1];
		FOR(i,1,n)FOR(j,i-1,n)is[i][j]+=is[i-1][j];
		q=getint();
		FOR(i,0,q){
				int l,r;
				l=getint()-1;
				r=getint()-1;
				putint(is[r][r]-(l==0?0:is[l-1][r]));
		}
		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

char s[5555];
int q;
int is[5555][5555]={};

int main() {
		scanf("%s",s);
		int n=strlen(s);
		FOR(len,1,n+1){
				//bool out=0;
				for(int i=n-len;i>=0;i--){
						int j=i+len-1;
						if(s[i]==s[j]){
								if(len<=3)is[i][j]=1;//out=1;
								else {
										is[i][j]=is[i+1][j-1];//out|=is[i][j];
								}
						}
				}
				//if(!out)break;
		}

		FOR(i,0,n)FOR(j,i+1,n)is[i][j]+=is[i][j-1];

		FOR(i,1,n)FOR(j,i-1,n)is[i][j]+=is[i-1][j];

		scanf("%d",&q);
		FOR(i,0,q){
				int l,r;
				scanf("%d%d",&l,&r);
				l--;
				r--;
				printf("%d\n",is[r][r]-(l==0?0:is[l-1][r]));
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

char s[5555];
ll h1[5555],h2[5555];
ll pw[5555];
int n;
int is[5555][5555]={};
int i,j;

ll hs1(int &i,int &j){
		return h1[j+1]-h1[i]*pw[j-i+1];
}

ll hs2(const int &i,const int &j){
		return h2[j+1]-h2[i]*pw[j-i+1];
}

bool palin(int &i,int &j){
		return hs1(i,j)==hs2(n-j-1,n-i-1);
}

int main() {
		scanf("%s",s);
		n=strlen(s);
		pw[0]=1;
		FOR(i,1,n+1)pw[i]=pw[i-1]*230617;
		h1[0]=0;
		FOR(i,0,n)h1[i+1]=h1[i]*230617+s[i];
		h2[0]=0;
		for(int i=n-1;i>=0;i--)h2[n-i]=h2[n-i-1]*230617+s[i];
		FOR(i,0,n)FOR(j,i,n)if(palin(i,j))is[i][j]=1;
		FOR(i,0,n)FOR(j,i+1,n)is[i][j]+=is[i][j-1];
		FOR(i,1,n)FOR(j,i-1,n)is[i][j]+=is[i-1][j];
		int q;
		scanf("%d",&q);
		FOR(i,0,q){
				int l,r;
				scanf("%d%d",&l,&r);
				l--;
				r--;
				printf("%d\n",is[r][r]-(l==0?0:is[l-1][r]));
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

char s[5555];
int q;
int is[5555][5555]={};

int main() {
		scanf("%s",s);
		int n=strlen(s);
		for(int i=n-1;i>=0;i--){
				bool out=0;
						FOR(len,1,n-i+1){
						int j=i+len-1;
						if(s[i]==s[j]){
								if(len<=3)is[i][j]=1,out=1;
								else {
										is[i][j]=is[i+1][j-1],out|=is[i][j];
								}
						}
				}
				if(!out)break;
		}

		FOR(i,0,n)FOR(j,i+1,n)is[i][j]+=is[i][j-1];

		FOR(i,1,n)FOR(j,i-1,n)is[i][j]+=is[i-1][j];

		scanf("%d",&q);
		FOR(i,0,q){
				int l,r;
				scanf("%d%d",&l,&r);
				l--;
				r--;
				printf("%d\n",is[r][r]-(l==0?0:is[l-1][r]));
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

char s[5555];
int q;
int is[5555][5555]={};

int main() {
		scanf("%s",s);
		int n=strlen(s);
		FOR(len,1,n+1){
				//bool out=0;
				for(int i=n-len;i>=0;i--){
						int j=i+len-1;
						if(s[i]==s[j]){
								if(len<=3)is[i][j]=1;//out=1;
								else {
										is[i][j]=is[i+1][j-1];//out|=is[i][j];
								}
						}
				}
				//if(!out)break;
		}

		FOR(i,0,n)FOR(j,i+1,n)is[i][j]+=is[i][j-1];

		FOR(i,1,n)FOR(j,0,n)is[i][j]+=is[i-1][j];

		scanf("%d",&q);
		FOR(i,0,q){
				int l,r;
				scanf("%d%d",&l,&r);
				l--;
				r--;
				printf("%d\n",is[r][r]-(l==0?0:is[l-1][r]));
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

char s[5555];
ll h1[5555],h2[5555];
ll pw[5555];
int n;
int is[5555][5555]={};

void pre(){
		pw[0]=1;
		FOR(i,1,n+1)pw[i]=pw[i-1]*230617;
		h1[0]=0;
		FOR(i,0,n)h1[i+1]=h1[i]*230617+s[i];
		h2[0]=0;
		for(int i=n-1;i>=0;i--)h2[n-i]=h2[n-i-1]*230617+s[i];
}

ll hs1(int i,int j){
		return h1[j+1]-h1[i]*pw[j-i+1];
}

ll hs2(int i,int j){
		return h2[j+1]-h2[i]*pw[j-i+1];
}

bool palin(int i,int j){
		return hs1(i,j)==hs2(n-j-1,n-i-1);
}

int main() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		scanf("%s",s);
		n=strlen(s);
		pre();
		FOR(i,0,n)FOR(j,i,n)if(palin(i,j))is[i][j]=1;

		FOR(i,0,n)FOR(j,i+1,n)is[i][j]+=is[i][j-1];
		FOR(i,1,n)FOR(j,i-1,n)is[i][j]+=is[i-1][j];
		int q;
		scanf("%d",&q);
		FOR(i,0,q){
				int l,r;
				scanf("%d%d",&l,&r);
				l--;
				r--;
				printf("%d\n",is[r][r]-(l==0?0:is[l-1][r]));
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

string s;
int ans[5555][5555];
int q;

bool pal(string s){
		int i=0,j=s.size()-1;
		while(i<=j){
				if(s[i]!=s[j])return 0;
				i++;j--;
		}
		return 1;
}

int main() {
		ios::sync_with_stdio(false);
		cin>>s;
		FOR(i,0,s.size()){
				ans[i][i]=1;
				FOR(j,i+1,s.size()){
						ans[i][j]=ans[i][j-1];
						string ha="";
						for(int k=j;k>=i;k--){
								ha=s[k]+ha;
								if(pal(ha))ans[i][j]++;
						}
				}
		}

		cin>>q;
		FOR(I,0,q){
				int i,j;
				cin>>i>>j;
				cout<<ans[i-1][j-1]<<endl;
		}

		return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

char s[5555];
int q;
int is[5555][5555]={};

int main() {
		scanf("%s",s);
		int n=strlen(s);
		FOR(len,1,n+1){
				bool out=0;
				for(int i=n-len;i>=0;i--){
						int j=i+len-1;
						if(s[i]==s[j]){
								if(len<=3)is[i][j]=1,out=1;
								else {
										is[i][j]=is[i+1][j-1],out|=is[i][j];
								}
						}
				}
				if(!out)break;
		}

		FOR(i,0,n)FOR(j,i+1,n)is[i][j]+=is[i][j-1];

		FOR(i,1,n)FOR(j,0,n)is[i][j]+=is[i-1][j];

		scanf("%d",&q);
		FOR(i,0,q){
				int l,r;
				scanf("%d%d",&l,&r);
				l--;
				r--;
				printf("%d\n",is[r][r]-(l==0?0:is[l-1][r]));
		}

		return 0;
}