You've got a string *s* = *s*<sub class="lower-index">1</sub>*s*<sub class="lower-index">2</sub>... *s*<sub class="lower-index">|*s*|</sub> of length |*s*|, consisting of lowercase English letters. There also are *q* queries, each query is described by two integers *l*<sub class="lower-index">*i*</sub>, *r*<sub class="lower-index">*i*</sub> (1 ≤ *l*<sub class="lower-index">*i*</sub> ≤ *r*<sub class="lower-index">*i*</sub> ≤ |*s*|). The answer to the query is the number of substrings of string *s*[*l*<sub class="lower-index">*i*</sub>... *r*<sub class="lower-index">*i*</sub>], which are palindromes.

String *s*[*l*... *r*] = *s*<sub class="lower-index">*l*</sub>*s*<sub class="lower-index">*l* + 1</sub>... *s*<sub class="lower-index">*r*</sub> (1 ≤ *l* ≤ *r* ≤ |*s*|) is a substring of string *s* = *s*<sub class="lower-index">1</sub>*s*<sub class="lower-index">2</sub>... *s*<sub class="lower-index">|*s*|</sub>.

String *t* is called a palindrome, if it reads the same from left to right and from right to left. Formally, if *t* = *t*<sub class="lower-index">1</sub>*t*<sub class="lower-index">2</sub>... *t*<sub class="lower-index">|*t*|</sub> = *t*<sub class="lower-index">|*t*|</sub>*t*<sub class="lower-index">|*t*| - 1</sub>... *t*<sub class="lower-index">1</sub>.
The first line contains string *s* (1 ≤ |*s*| ≤ 5000). The second line contains a single integer *q* (1 ≤ *q* ≤ 10<sup class="upper-index">6</sup>) — the number of queries. Next *q* lines contain the queries. The *i*-th of these lines contains two space-separated integers *l*<sub class="lower-index">*i*</sub>, *r*<sub class="lower-index">*i*</sub> (1 ≤ *l*<sub class="lower-index">*i*</sub> ≤ *r*<sub class="lower-index">*i*</sub> ≤ |*s*|) — the description of the *i*-th query.

It is guaranteed that the given string consists only of lowercase English letters.
#include <cstdio>
#include <iostream>
#include <cstring>

const int Maxn = 5005;
char s[Maxn];
int ans[Maxn][Maxn], q; 
bool visc[Maxn][Maxn], visj[Maxn][Maxn], jud[Maxn][Maxn];
void calc(void);
void cjudge(int, int);
void cans(int, int);

int main(){
//	freopen("data.in", "r", stdin);

	int l, r;
	scanf("%s", s+1);
	calc();
	scanf("%d", &q);
	while(q--){
		scanf("%d %d", &l, &r);
		printf("%d\n", ans[l][r]);
	}

	return 0;
}

void calc(void){
	int n = strlen(s+1);
	for(int i=1; i<=n; i++){
		for(int j=i; j<=n; j++){
			cjudge(i, j);
		}
	}
	ans[1][1] = 1;
	cans(1, strlen(s+1));
}

void cjudge(int l, int r){
	if(visj[l][r]) return;
	if(l==r || l+1==r){
		jud[l][r] = (s[l]==s[r]);
	}
	else{
		cjudge(l+1, r-1);
		jud[l][r] = jud[l+1][r-1] && (s[l]==s[r]);
	}
	visj[l][r] = true;
}

void cans(int l, int r){
	if(visc[l][r]) return;
	if(l==r){
		ans[l][r] = 1;
	}
	else if(l+1 == r){
		ans[l][r] = 2+(s[l]==s[r]);
	}
	else{
		cans(l+1, r); cans(l, r-1); cans(l+1, r-1);
		ans[l][r] = ans[l+1][r]+ans[l][r-1]-ans[l+1][r-1]+jud[l][r];
	}
	visc[l][r] = true;
}
