// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define ll long long int

using namespace std;

int a[500500],h,x,y,ans,m=1;
int qry[707][707],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        for(int i=1;i<=700;i++)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<700)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(int i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define INF 2147483647
#define SIZE 700

int inp(){
    char c = getchar();
    int neg = 1;
    while(c < '0' || c > '9'){
        if(c == '-')
            neg = -1;
        c = getchar();
    }
    int sum = 0;
    while(c >= '0' && c <= '9'){
        sum = sum * 10 + c - '0';
        c = getchar();
    }
    return sum * neg;
}

int a[500010];
int f[1010][1010];

int main(){
    int n = inp();
    while(n--){
        int op = inp();
        if(op == 1){
            int x = inp();
            int y = inp();
            for(int i = 1; i <= SIZE; i++){
                f[i][x % i] -= a[x];
                f[i][x % i] += y;
            }
            a[x] = y;
        } else {
            int x = inp();
            int y = inp();
            if(x <= SIZE)
                printf("%d\n", f[x][y]);
            else {
                int ans = 0;
                for(int i = y; i <= 500000; i += x)
                    ans += a[i];
                printf("%d\n", ans);
            }
        }
    }
}


#include<bits/stdc++.h>

#define ll long long
#define INF 2147483647
#define SIZE 500

int inp(){
    char c = getchar();
    int neg = 1;
    while(c < '0' || c > '9'){
        if(c == '-')
            neg = -1;
        c = getchar();
    }
    int sum = 0;
    while(c >= '0' && c <= '9'){
        sum = sum * 10 + c - '0';
        c = getchar();
    }
    return sum * neg;
}

int a[50010];
int f[510][510];

int main(){
    int n = inp();
    while(n--){
        int op = inp();
        if(op == 1){
            int x = inp();
            int y = inp();
            for(int i = 1; i <= SIZE; i++){
                f[i][x % i] -= a[x];
                f[i][x % i] += y;
            }
            a[x] = y;
        } else {
            int x = inp();
            int y = inp();
            if(x <= SIZE)
                printf("%d\n", f[x][y]);
            else {
                int ans = 0;
                for(int i = y; i <= n; i += x)
                    ans += a[i];
                printf("%d\n", ans);
            }
        }
    }
}


#include<bits/stdc++.h>

#define ll long long
#define INF 2147483647
#define SIZE 800

int inp(){
    char c = getchar();
    int neg = 1;
    while(c < '0' || c > '9'){
        if(c == '-')
            neg = -1;
        c = getchar();
    }
    int sum = 0;
    while(c >= '0' && c <= '9'){
        sum = sum * 10 + c - '0';
        c = getchar();
    }
    return sum * neg;
}

int a[500010];
int f[1010][1010];

int main(){
    int n = inp();
    while(n--){
        int op = inp();
        if(op == 1){
            int x = inp();
            int y = inp();
            for(int i = 1; i <= SIZE; i++)
                f[i][x % i] += y;
            a[x] += y;
        } else {
            int x = inp();
            int y = inp();
            if(x <= SIZE)
                printf("%d\n", f[x][y]);
            else {
                int ans = 0;
                for(int i = y; i <= 500000; i += x)
                    ans += a[i];
                printf("%d\n", ans);
            }
        }
    }
}


#include<bits/stdc++.h>

#define ll long long
#define INF 2147483647
#define SIZE 700

int inp(){
    char c = getchar();
    int neg = 1;
    while(c < '0' || c > '9'){
        if(c == '-')
            neg = -1;
        c = getchar();
    }
    int sum = 0;
    while(c >= '0' && c <= '9'){
        sum = sum * 10 + c - '0';
        c = getchar();
    }
    return sum * neg;
}

int a[500010];
int f[1010][1010];

int main(){
    int n = inp();
    while(n--){
        int op = inp();
        if(op == 1){
            int x = inp();
            int y = inp();
            for(int i = 1; i <= SIZE; i++){
                f[i][x % i] -= a[x];
                f[i][x % i] += y;
            }
            a[x] = y;
        } else {
            int x = inp();
            int y = inp();
            if(x <= SIZE)
                printf("%d\n", f[x][y]);
            else {
                int ans = 0;
                for(int i = y; i <= n; i += x)
                    ans += a[i];
                printf("%d\n", ans);
            }
        }
    }
}


#include<bits/stdc++.h>

#define ll long long
#define INF 2147483647
#define SIZE 1

int inp(){
    char c = getchar();
    int neg = 1;
    while(c < '0' || c > '9'){
        if(c == '-')
            neg = -1;
        c = getchar();
    }
    int sum = 0;
    while(c >= '0' && c <= '9'){
        sum = sum * 10 + c - '0';
        c = getchar();
    }
    return sum * neg;
}

int a[500010];
int f[1010][1010];

int main(){
    int n = inp();
    while(n--){
        int op = inp();
        if(op == 1){
            int x = inp();
            int y = inp();
            for(int i = 1; i <= SIZE; i++)
                f[i][x % i] += y;
            a[x] += y;
        } else {
            int x = inp();
            int y = inp();
            if(x <= SIZE)
                printf("%d\n", f[x][y]);
            else {
                int ans = 0;
                for(int i = y; i <= 500000; i += x)
                    ans += a[i];
                printf("%d\n", ans);
            }
        }
    }
}


#include<bits/stdc++.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m,val[1010][1010],a[1000010],len,cnt,ans;
signed main(){
    m=read();
    for(int i=1;i<=m;i++){
        int type=read();int x=read();int y=read();
        if(type==1){
            a[x]+=y;
            for(int i=1;i<=1000;i++){
                val[i][x%i]+=y;
            }
        }
        else{
            int cur=0;
            if(x<1000){
                printf("%lld\n",val[x][y]);
            }
            else{
                for(int j=0;j*x+y<=n;j++){
                    cur+=a[j*x+y];
                }
                printf("%lld\n",cur);
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m,val[1010][1010],a[1000010],len,cnt,ans;
signed main(){
    m=read();
    while(m--){
        int type=read();int x=read();int y=read();
        if(type==1){
            a[x]+=y;
            for(int i=1;i<=1000;i++){
                val[i][x%i]+=y;
            }
        }
        else{
            int cur=0;
            if(x<1000)cur=val[x][y];
            else{
                for(int i=0;i*x+y<=n;i++){
                    cur+=a[i*x+y];
                }
            }
            printf("%lld\n",cur);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m,val[1010][1010],a[1000010],len,cnt,ans;
signed main(){
    m=read();
    while(m--){
        int type=read();int x=read();int y=read();
        if(type==1){
            a[x]+=y;
            for(int i=1;i<=1000;i++){
                val[i][x%i]+=y;
            }
        }
        else{
            int cur=0;
            if(x<1000){
                printf("%lld\n",val[x][y]);
            }
            else{
                for(int i=0;i*x+y<=n;i++){
                    cur+=a[i*x+y];
                }
                printf("%lld\n",cur);
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m,val[1010][1010],a[1000010],len,cnt,ans;
signed main(){
    m=read();
    while(m--){
        int type=read();int x=read();int y=read();
        if(type==1){
            a[x]+=y;
            for(int i=1;i<=1000;i++){
                val[i][x%i]+=y;
            }
        }
        else{
            int cur=0;
            if(x<1000)cur=val[x][y];
            else{
                for(int i=0;i*x+y<=500000;i++){
                    cur+=a[i*x+y];
                }
            }
            printf("%lld\n",cur);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m,val[1010][1010],a[1000010],len,cnt,ans;
signed main(){
    m=read();
    len=1000;
    for(int i=1;i<=m;i++){
        int type=read();int x=read();int y=read();
        if(type==1){
            a[x]+=y;
            for(int i=1;i<=1000;i++){
                val[i][x%i]+=y;
            }
        }
        else{
            int cur=0;
            if(x<1000){
                printf("%lld\n",val[x][y]);
            }
            else{
                for(int i=0;i*x+y<=n;i++){
                    cur+=a[i*x+y];
                }
                printf("%lld\n",cur);
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m,val[1010][1010],a[1000010],len,cnt,ans;
signed main(){
    m=read();int len=sqrt(500000);
    while(m--){
        int type=read();int x=read();int y=read();
        if(type==1){
            a[x]+=y;
            for(int i=1;i<=len;i++){
                val[i][x%i]+=y;
            }
        }
        else{
            int cur=0;
            if(x<len)cur=val[x][y];
            else{
                for(int i=0;i*x+y<=500000;i++){
                    cur+=a[i*x+y];
                }
            }
            printf("%lld\n",cur);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define int long long
#define INF 0x3f3f3f3f
using namespace std;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m,val[1010][1010],a[1000010],len,cnt,ans;
signed main(){
    m=read();
    len=1000;
    for(int i=1;i<=m;i++){
        int type=read();int x=read();int y=read();
        if(type==1){
            a[x]+=y;
            for(int i=1;i<=1000;i++){
                val[i][x%i]+=y;
            }
        }
        else{
            int cur=0;
            if(x<=1000){
                printf("%lld\n",val[x][y]);
            }
            else{
                for(int i=0;i*x+y<=n;i++){
                    cur+=a[i*x+y];
                }
                printf("%lld\n",cur);
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxl=500010;
int q,n=maxl-10,len,cnt;
long long ans;
int a[maxl];
long long val[720][720];
int main()
{
	scanf("%d",&q);
	int op,x,y;
	for(int i=1;i<=q;i++)
	{
		scanf("%d%d%d",&op,&x,&y);
		if(op==1)
		{
			a[x]+=y;
			for(int i=1;i<720;i++)
				val[i][x%i]+=y;
		}
		else
		{
			ans=0;
			if(x<720)
				ans=val[x][y];
			else
			{
				for(int i=0;i*x+y<=n;i++)
					ans+=a[i*x+y];
			}
			printf("%lld\n",ans);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const int INF = 2e9;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 5e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/

int q;
ll a[limit];

void solveProblem() {
    int sq = sqrt(500000);

    while (q--) {
        int type, x, y;
        cin >> type >> x >> y;
        if (type == 1) a[x] += y;
        else {
            if (x > sq) {
                ll ans = 0;
                for (int i = y; i <= 500000; i += x) ans += a[i];
                cout << ans << endl;
            }

            else {
                ll ans = 0;
                REP(i, 1, x) {
                    if (i % x == y) ans += a[i];
                }

                cout << ans << endl;
            }
        }
    }
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> q;
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const int INF = 2e9;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 5e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/

int q;
ll a[limit];

void solveProblem() {
    int sq = sqrt(500000);

    while (q--) {
        int type, x, y;
        cin >> type >> x >> y;
        if (type == 1) a[x] += y;
        else {
            if (x > sq) {
                int j = y;
                ll ans = 0;
                while (j < 500000) {
                    ans += a[j];
                    j += x;
                }
            }

            else {
                ll ans = 0;
                REP(i, 1, x) {
                    if (i % x == y) ans += a[i];
                }

                cout << ans << endl;
            }
        }
    }
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> q;
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const int INF = 2e9;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 5e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/

int q;
ll a[limit];

void solveProblem() {
    int sq = sqrt(500000);

    while (q--) {
        int type, x, y;
        cin >> type >> x >> y;
        if (type == 1) a[x] += y;
        else {
            if (x > sq) {
                int j = y;
                ll ans = 0;
                while (j < 500000) {
                    ans += a[j];
                    j += x;
                }

                cout << ans << endl;
            }

            else {
                ll ans = 0;
                REP(i, 1, x) {
                    if (i % x == y) ans += a[i];
                }

                cout << ans << endl;
            }
        }
    }
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> q;
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const int INF = 2e9;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 5e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/

int q;
ll a[limit];

void solveProblem() {
    int sq = sqrt(500000);

    while (q--) {
        int type, x, y;
        cin >> type >> x >> y;
        if (type == 1) a[x] += y;
        else {
            if (x > sq) {
                int j = y;
                ll ans = 0;
                ans += a[j];
                while (j <= 500000) {
                    j += x;
                    if (j > 500000) break;
                    ans += a[j];
                }

                cout << ans << endl;
            }

            else {
                ll ans = 0;
                REP(i, 1, x) {
                    if (i % x == y) ans += a[i];
                }

                cout << ans << endl;
            }
        }
    }
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> q;
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const int INF = 2e9;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 5e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/

int q;
ll a[limit];
ll sum[750][750];
int sq = 710;

void add(int x, int val) {
    REP(i, 1, sq) {
        sum[i][x % i] += val;
    }
    a[x] += y;
}

void solveProblem() {

    while (q--) {
        int type, x, y;
        cin >> type >> x >> y;
        if (type == 1) add(x, y);
        else {
            if (x > sq) {
                ll ans = 0;
                for (int i = y; i <= 500000; i += x) ans += a[i];
                cout << ans << endl;
            }

            else {
                cout << sum[x][y] << endl;
            }
        }
    }
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> q;
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const int INF = 2e9;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 5e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/

int q;
ll a[limit];
ll sum[750][750];
int sq = 710;

void add(int x, int val) {
    REP(i, 1, sq) {
        sum[i][x % i] += val;
    }
}

void solveProblem() {

    while (q--) {
        int type, x, y;
        cin >> type >> x >> y;
        if (type == 1) add(x, y);
        else {
            if (x > sq) {
                ll ans = 0;
                for (int i = y; i <= 500000; i += x) ans += a[i];
                cout << ans << endl;
            }

            else {
                cout << sum[x][y] << endl;
            }
        }
    }
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> q;
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const int INF = 2e9;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 5e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/

int q;
ll a[limit];
ll sum[750][750];
int sq = 710;

void add(int x, int val) {
    REP(i, 1, sq) {
        sum[i][x % i] += val;
    }
    a[x] += val;
}

void solveProblem() {

    while (q--) {
        int type, x, y;
        cin >> type >> x >> y;
        if (type == 1) add(x, y);
        else {
            if (x > sq) {
                ll ans = 0;
                for (int i = y; i <= 500000; i += x) ans += a[i];
                cout << ans << endl;
            }

            else {
                cout << sum[x][y] << endl;
            }
        }
    }
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> q;
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
const ll maxn = 1001;
char s[maxn];
ll dp[maxn][maxn];
ll n;
ll q;
bool isRight(ll l,ll r){
	if(r - l == 1 && s[r] == s[l]) return true;
	else if(r - l == 1) return false;
	for(int i = l ; i < (l + r) / 2 ; i++){
		if(s[i] != s[l + r - i]) return false;
	}
	return true;
}
int main(){
	cin >> s;
	cin >> q;
	n = strlen(s);
	for(int i = 0 ; i < n ; i++){
		dp[i][i] = 1;
		if(i != n - 1) dp[i][i + 1] = isRight(i,i + 1) + 2;
	}
	for(int k = 2 ; k < n ; k++){
		for(int i = 0 ; i < n - k ; i++){
			dp[i][i + k] = dp[i + 1][i + k] + dp[i][i + k - 1] - dp[i + 1][i + k - 1] + isRight(i , i+k);
		}
	}
	for(int i = 0 ; i < q ; i++){
		ll l,r;
		cin >> l >> r;
		cout << dp[l - 1][r - 1];
		if(i != q - 1) cout << endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
ll __in(){ll x;cin>>x;return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    string s;
    cin>>s;
    for(int i=1;i<=s.size();i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    for(int i=1;i<=s.size();i++)
        for(int j=i-1;j>=0;j--)
            if(j>i-2)
                ans[j][i]=1;
            else
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=IN;
    while(q--)
        cout<<ans[IN-1][IN]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=5050;
bool p[N][N];
int ans[N][N];
int in()
{
    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 out(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()
{
    string s;
    cin>>s;
    int n=s.size();
    for(int i=1;i<=n;i++)
        for(int j=i;j>=0;j--)
            if(j-i)
                if(j>i-2)
                    p[j][i]=1,
                    ans[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]),
                    ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
            else
                if(j>i-2)
                    p[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    int q=in();
    while(q--)
    {
        int x=in(),y=in();
        out(ans[x-1][y]);
    }
    return 0;
}


#include<cstdio>
#include<string>
#include<cstrinf>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    char s[N];
    scanf("%s",s);
    for(int i=1;i<=strlen(s);i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    for(int i=1;i<=strlen(s);i++)
        for(int j=i-1;j>=0;j--)
            if(j>i-2)
                ans[j][i]=1;
            else
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=In;
    while(q--)
        printf("%d\n",ans[In-1][In]);
    return 0;
}


#include<cstdio>
#include<string>
#include<cstring>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    char s[N];
    scanf("%s",s);
    for(int i=1;i<=strlen(s);i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    for(int i=1;i<=strlen(s);i++)
        for(int j=i-1;j>=0;j--)
            if(j>i-2)
                ans[j][i]=1;
            else
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=In;
    while(q--)
        printf("%d\n",ans[In-1][In]);
    return 0;
}


#include<iostream>
#define ll long long
using namespace std;
const int N=5050;
bool p[N][N];
int ans[N][N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    string s;
    cin>>s;
    int n=s.size();
    for(int i=1;i<=n;i++)
        for(int j=i;j>=0;j--)
            if(j-i)
                if(j>i-2)
                    p[j][i]=1,
                    ans[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]),
                    ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
            else
                if(j>i-2)
                    p[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    int q;
    cin>>q;
    while(q--)
    {
        int x,y;
        cin>>x>>y;
        cout<<ans[x-1][y]<<'\n';
    }
    return 0;
}


#include<iostream>
#define ll long long
using namespace std;
const int N=5050;
bool p[N][N];
int ans[N][N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    string s;
    cin>>s;
    for(int i=1;i<=s.size();i++)
        for(int j=i;j>=0;j--)
            if(j-i)
                if(j>i-2)
                    p[j][i]=1,
                    ans[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]),
                    ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
            else
                if(j>i-2)
                    p[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    int q;
    cin>>q;
    while(q--)
    {
        int x,y;
        cin>>x>>y;
        cout<<ans[x-1][y]<<'\n';
    }
    return 0;
}


#include<cstdio>
#include<string>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    char s[N];
    scanf("%s",s);
    for(int i=1;i<=strlen(s);i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    for(int i=1;i<=strlen(s);i++)
        for(int j=i-1;j>=0;j--)
            if(j>i-2)
                ans[j][i]=1;
            else
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=In;
    while(q--)
        printf("%d\n",ans[In-1][In]);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
ll __in(){ll x;cin>>x;return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    string s;
    cin>>s;
    for(int i=1;i<=s.size();i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1,
                ans[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1],
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=IN;
    while(q--)
        cout<<ans[IN-1][IN]<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
ll __in(){ll x;cin>>x;return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    string s;
    cin>>s;
    for(int i=1;i<=s.size();i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1,
                ans[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]),
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=IN;
    while(q--)
        cout<<ans[IN-1][IN]<<endl;
    return 0;
}


#include<cstdio>
#include<string>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    ll cnt=0;
    char s[N];
    while(scanf("%c",&s[cnt]))cnt++;
    for(int i=1;i<=cnt;i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    for(int i=1;i<=cnt;i++)
        for(int j=i-1;j>=0;j--)
            if(j>i-2)
                ans[j][i]=1;
            else
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=In;
    while(q--)
        printf("%d\n",ans[In-1][In]);
    return 0;
}


#include<cstdio>
#include<string>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    string s;
    scanf("%s",&s);
    for(int i=1;i<=s.size();i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    for(int i=1;i<=s.size();i++)
        for(int j=i-1;j>=0;j--)
            if(j>i-2)
                ans[j][i]=1;
            else
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=In;
    while(q--)
        printf("%d\n",ans[In-1][In]);
    return 0;
}


#include<iostream>
#define ll long long
using namespace std;
const ll N=5050;
bool p[N][N];
int ans[N][N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    string s;
    cin>>s;
    for(int i=1;i<=s.size();i++)
        for(int j=i;j>=0;j--)
            if(j-i)
                if(j>i-2)
                    p[j][i]=1,
                    ans[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]),
                    ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
            else
                if(j>i-2)
                    p[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    int q;
    cin>>q;
    while(q--)
    {
        int x,y;
        cin>>x>>y;
        cout<<ans[x-1][y]<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
ll __in(){ll x;cin>>x;return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    string s;
    cin>>s;
    for(int i=1;i<=s.size();i++)
        for(int j=i;j>=0;j--)
            if(j-i)
                if(j>i-2)
                    p[j][i]=1,
                    ans[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]),
                    ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
            else
                if(j>i-2)
                    p[j][i]=1;
                else
                    p[j][i]=p[j+1][i-1]&(s[j]==s[i-1]);
    ll q=IN;
    while(q--)
        cout<<ans[IN-1][IN]<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<ll,ll>
#define X first
#define Y second
#define endl '\n'
#define In _i_n()
#define IN __in()
#define gcd __gcd
#define lcm(x,y) (((x)*(y))/(gcd((x),(y))))
#define smin(x,y) ((x)=min((x),(y)))
#define smax(x,y) ((x)=max((x),(y)))
#define obt __builtin_popcount
#define pb push_back
#define pob pop_back
#define dbg(x) cerr<<#x<<": "<<x<<endl
using namespace std;
const ld INF=1LL<<60,EPS=1LL>>60;
const ll N=5050;
ll _i_n(){ll x=0;char y=getchar();while(y<='9'&y>='0')x*=10,x+=y-'0',y=getchar();return x;}
ll __in(){ll x;cin>>x;return x;}
/*
ll pw(ll a,ll b)
{
    return b?(pw((a*a)%MOD,b/2)*(b%2?a:1))%MOD:1;
}
*/
bool p[N][N];
ll ans[N][N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    string s;
    cin>>s;
    for(int i=1;i<=s.size();i++)
        for(int j=0;j<=i;j++)
            if(j>i-2)
                p[j][i]=1,
                ans[j][i]=1;
            else
                p[j][i]=p[j+1][i-1]&(s[j]==s[i-1,
                ans[j][i]=ans[j+1][i]+ans[j][i-1]-ans[j+1][i-1]+p[j][i];
    ll q=IN;
    while(q--)
        cout<<ans[IN-1][IN]<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define LL long long
#define LD long double
char p[5005];
int n,m,f[5005][5005],sum[5005][5005],l,r;
int main()
{
	cin>>p+1;
	n=strlen(p+1);
	for(int i=1;i<=n*2;i++)
	{
		if(i&1)
		{
			int k=i/2+1;
			f[k][k]++;
			for(int len=1;len<=n;len++)
			{
				if(k-len<=0||k+len>n)break;
				if(p[k-len]!=p[k+len])break;
				f[k-len][k+len]++;
			}
		}
		else
		{
			int k=i/2;
			for(int len=1;len<=n;len++)
			{
				if(k-len+1<=0||k+len>n)break;
				if(p[k-len+1]!=p[k+len])break;
				f[k-len+1][k+len]++;
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			f[i][j]+=f[i-1][j]+f[i][j-1]-f[i-1][j-1];
		}
	}
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d%d",&l,&r);
		printf("%d\n",f[r][r]-f[l-1][r]-f[r][l-1]+f[l-1][l-1]);
	}
	return 0;
}


#include<bits/stdc++.h> //Ithea Myse Valgulious
using namespace std;
const int aoi=5038;
char s[aoi];
int dp[aoi][aoi];
int main() {
scanf("%s",s+1);
int i,j,k,q=read(),n=strlen(s+1);
for (i=1;i<=n;++i) {
  for (j=k=i;j&&k<=n&&s[j]==s[k];--j,++k) ++dp[j][k];
  for (j=i,k=i+1;j&&k<=n&&s[j]==s[k];--j,++k) ++dp[j][k];
  }
for (i=1;i<=n;++i)
  for (j=1;j<=n;++j)
    dp[i][j]+=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1];
for (int l,r;q--;) {
  read(l),read(r);
  write(dp[r][r]-dp[l-1][r]-dp[r][l-1]+dp[l-1][l-1]),pl;
  }
}


#include <bits/stdc++.h>
#define ll long long
#define re register
#define il inline

using namespace std;
const int maxn = 5010;
char s[maxn];
int q;
int dp[maxn][maxn];
int main() {
	scanf("%s", s+1);
	memset(dp, 0, sizeof(dp));
	int len = strlen(s+1);
	for(re int k = 1; k <= len; ++k) {
		int i, j;
		//i  k  j处理区间回文串
		for(i = j = k; i && j <= len; --i, ++j) {
			if(s[i] == s[j]) dp[i][j]++;
			else break;
		}
		for(i = k, j = k+1; i && j <= len; --i, ++j) {
			if(s[i] == s[j]) dp[i][j]++;
			else break;
		}
	}
	for(re int i = 1; i <= len; ++i) {
		for(re int j = 1; j <= len; ++j) {
			dp[i][j] += dp[i-1][j]+dp[i][j-1] - dp[i-1][j-1];
		}
	}
	int l, r;
	scanf("%d", &q);
	for(re int i = 1; i <= q; ++i) {
		scanf("%d%d", &l, &r);
		printf("%d\n", dp[r][r]-dp[l-1][r]-dp[r][l-1]+dp[l-1][l-1]);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int Maxx=5005;
char S[Maxx];int r,c[Maxx];
int lst,tot,ln[Maxx],prt[Maxx],d[Maxx],ch[Maxx][26];
inline void Ins(int x){
	int p=lst,q;c[++r]=x;
	while(c[r-ln[p]-1]^x)p=prt[p];
	if(!ch[q=p][x]){
		do q=prt[q];while(c[r-ln[q]-1]^x);
		++tot,d[tot]=d[prt[tot]=ch[q][x]]+1;
		ch[p][x]=tot,ln[tot]=ln[p]+2;
	}lst=ch[p][x];
}
int n,Ans[Maxx][Maxx];
int main()
{
//	freopen("in.txt","r",stdin);
//	freopen("out.txt","w",stdout);
	scanf("%s",S+1),n=strlen(S+1);
	for(int l=1;l<=n;++l){
		c[r=0]=-1,ln[0]=tot=lst=0,prt[0]=1,d[0]=0;
		ln[++tot]=-1,prt[1]=1,d[1]=0;
		for(int i=l;i<=n;++i)Ins(S[i]),Ans[l][i]=Ans[l][i-1]+d[lst];
		for(int i=0;i<=tot;++i)memset(ch[i],0,sizeof(ch[i]));
	}scanf("%d",&n),++n;int l,r;
	while(--n){
		scanf("%d %d",&l,&r),printf("%d\n",Ans[l][r]);
	}
	return 0;
}
/*
caaaba
5
1 1
1 4
2 3
4 6
4 5
*/


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ri register int
#define For(i,j,k) for(ri i=j;i<=k;i=-~i)
#define DFor(i,j,k) for(ri i=j;i>=k;i=~-i)
inline int v_in(){
    int f=1,sum=0;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){sum=(sum<<3)+(sum<<1)+(ch^48);ch=getchar();}
    return sum*f;
}
const int M=5e3+5,N=1e6+5,inf=1<<30;
char c[M];int n,m,f[M][M],s[M][M];
int main(){
	scanf("%s",c+1);n=strlen(c+1);
	For(i,1,n)For(j,0,n){
		if(i-j<1||i+j>n||c[i-j]!=c[i+j])break;
		f[i-j][i+j]=1;
	}
	For(i,1,n-1)For(j,0,n){
		if(i-j<1||i+j+1>n||c[i-j]!=c[i+j+1])break;
		f[i-j][i+j+1]=1;
	}For(i,1,n)For(j,1,n)s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+f[i][j];
	m=v_in();
	For(i,1,m){
		int l=v_in(),r=v_in();
		printf("%d\n",s[r][r]-s[l-1][r]-s[r][l-1]+s[l-1][l-1]);
	}return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define re register
#define il inline

using namespace std;
const int maxn = 5010;
char s[maxn];
int q;
int dp[maxn][maxn];
int main() {
	scanf("%s", s+1);
	memset(dp, 0, sizeof(dp));
	int len = strlen(s+1);
	for(re int k = 1; k <= len; ++k) {
		int i, j;
		//i  k  j处理区间回文串
		for(i = j = k; i && j <= len; --i, ++j) {
			if(s[i] == s[j]) dp[i][j]++;
			else break;
		}
		for(i = k, j = k+1; i && j <= len; --i, ++j) {
			if(s[i] == s[j]) dp[i][j]++;
			else break;
		}
	}
	for(re int i = 1; i <= len; ++i) {
		for(re int j = 1; j <= len; ++j) {
			dp[i][j] += dp[i-1][j]+dp[i][j-1] - dp[i-1][j-1];
		}
	}
	int l, r;
	scanf("%d", &q);
	for(re int i = 1; i <= q; ++i) {
		scanf("%d%d", &l, &r);
		printf("%d\n", dp[r][r]-dp[l-1][r]-dp[r][l-1]+dp[l-1][l-1]);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
string str;
int len, is[5050][5050], dp[5050][5050], n;
void huiwen(int l, int r)//预处理所有以l、r为中心的回文子串
{
	int x=1, y=r-l+1;
	while (l>=0 && r<=n)
	{
		if (str[l]==str[r]) is[l][r]=1;
		else break;
		l--;
		r++;
	}
}
int main()
{
	cin>>str;
	len=str.length();
	for (int i=1; i<=n; i++)//枚举回文串中心字符
	{
		huiwen(i, i);//奇数回文子串
		huiwen(i, i+1);//偶数回文子串
	}
	for (int i=1; i<=len; i++) dp[i][i]=1;//是len！字符串长度！不要手残写n
	for (int i=1; i<len; i++)
		for (int j=i+1; j<=len; j++)
			dp[i][j]=dp[i+1][j]+dp[i][j-1]+dp[i+1][j-1]+is[i][j];
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		printf("%d\n", dp[x][y]);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
const int N=5e3+5;
int n,q,l,r,dp[N][N],Hw[N][N];
char a[N];
int main()
{
	scanf("%s",a+1);
	n=strlen(a+1);
	for(int r=1;r<=n;r++)
	  for(int l=r;l>0;l--)
	    {
	      if(a[l]==a[r]) Hw[l][r]=Hw[l+1][r]+1;
	      else Hw[l][r]=Hw[l+1][r];
	      dp[l][r]=dp[l][r-1]+Hw[l][r];
	    }
	scanf("%d",&q);
	while(q--)
	  scanf("%d%d",&l,&r),printf("%d\n",dp[l][r]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
string s;
bool hui[5005][5005];
int dp[5005][5005],zhi[5005][5005];
inline int read(){
	int k=0; int f=1; char ch=getchar();
	while(!isdigit(ch)){if(ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)){k=(k<<1)+(k<<3)+(ch^48);ch=getchar();}
	return k*f;
}
int main(){
	cin>>s;
	int n=s.size();
	for(int i=0;i<=n;i++) hui[i][i]=1;
	for(int i=0;i<=n-1;i++) if(s[i]==s[i+1]) hui[i][i+1]=1;
	for(int l=2;l<=n;l++){
		for(int ss=0;ss+l<n;ss++){
			if(hui[ss+1][ss+l-1]&&s[ss]==s[ss+l]) hui[ss][ss+l]=1;
		}
	}
	for(int i=0;i<=n;i++){
		for(int j=i;j>=0;j--){
			if(hui[j][i]==1) zhi[j][i]=zhi[j+1][i]+1;
			else zhi[j][i]=zhi[j+1][i];
		}
	}
	for(int i=0;i<n;i++) dp[i][i]=1;
	for(int i=1;i<n;i++){
		for(int j=0;j<n-i;j++){
			dp[j][j+i]=dp[j][j+i-1]+zhi[j][i+j];
		}
	}
	int q; cin>>q;
	while(q--){
		int x,y; x=read(); y=read();
		cout<<dp[x-1][y-1]<<endl;
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<cstring>
using namespace std;
int dp[5005][5005];
bool hui[5005][5005];
int zhi[5005][5005];
string s;
int q;
int main(){
    cin>>s;
    int n=s.size();
    //-------------------------------
    //预处理部分
    for(register int i=0;i<=n;i++){
        hui[i][i]=1;
    }
    for(register int i=0;i<=n-1;i++){
        if(s[i]==s[i+1])hui[i][i+1]=1;
    }
    for(register int l=2;l<=n;l++){
        for(register int ss=0;ss+l<n;ss++){
            if(hui[ss+1][ss+l-1]==1&&s[ss]==s[ss+l])hui[ss][ss+l]=1;  // 先确定i到j是否为回文串，用bool数组记录
        }
    }
    for(register int i=0;i<=n;i++){
        for(register int j=i;j>=0;j--){
            if(hui[j][i]==1)zhi[j][i]=zhi[j+1][i]+1;    //然后进行统计，统计出以j结尾，开头在i之后的回文串个数
            else zhi[j][i]=zhi[j+1][i];
        }
    }
    //---------------------------------
    for(register int i=0;i<n;i++){
        dp[i][i]=1;
    }
    for(register int i=1;i<n;i++){
        for(register int j=0;j<n-i;j++){
            dp[j][i+j]=dp[j][i+j-1]+zhi[j][i+j];   //状态转移
        }
    }
    cin>>q;
    while(q){
        int x,y;
        scanf("%d%d",&x,&y);
        printf("%d\n",dp[x-1][y-1]);
        q--;
    }
}


/*
区间dp
https://blog.csdn.net/qq_26122039/article/details/51226209
dp[i][j]表示左端点为i，右端点为j的区间的回文子串数。
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
string str;
int len, is[5050][5050], dp[5050][5050], n;
void huiwen(int l, int r)//预处理所有以l、r为中心的回文子串
{
	while (l>=1 && r<=len)
	{
		if (str[l-1]==str[r-1]) is[l][r]=1;
		else break;
		l--;
		r++;
	}
}
int main()
{
	cin>>str;
	len=str.length();
	for (int i=1; i<=len; i++)//枚举回文串中心字符
	{
		huiwen(i, i);//奇数回文子串
		huiwen(i, i+1);//偶数回文子串
	}
	for (int i=1; i<=len; i++) dp[i][i]=1;//是len！字符串长度！不要手残写n
	for (int i=1; i<=len; i++)
		for (int j=1; i+j<=len; j++)
		{
			int l=j, r=i+j;//区间左端点l，右端点r
			dp[l][r]=dp[l][r-1]+dp[l+1][r]-dp[l+1][r-1]+is[i][j];//容斥原理
		}
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		printf("%d\n", dp[x][y]);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstring>
using namespace std;
int ans[4005][4005],res;
const int Mod=1e9+7;
struct Fuck_This_Asshole_Palindrmic_Tree
{
	int cnt,tot,lst,ch[4005],fail[4005],num[4005],nxt[4005][26],len[4005],sum[4005];
	int inline Set_Up_A_New_Point(int x)
	{
		memset(nxt[cnt],0,sizeof(nxt[cnt]));
		len[cnt]=x,num[cnt]=0,sum[cnt]=0;
		return cnt++;
	}
	void Reset()
	{
		cnt=0,tot=0,lst=0;
		Set_Up_A_New_Point(0);
		Set_Up_A_New_Point(-1);
		ch[0]=-1;
		fail[0]=1,fail[1]=1;
	}
	int GetFail(int x)
	{
		while(ch[tot-len[x]-1]!=ch[tot])x=fail[x];
		return x;
	}
	int Add(char x)
	{
		int tmp=x-'a';
		ch[++tot]=tmp;
		int Target=GetFail(lst);
		if(!nxt[Target][tmp])
		{
			int now=Set_Up_A_New_Point(len[Target]+2);
			fail[now]=nxt[GetFail(fail[Target])][tmp];
			nxt[Target][tmp]=now;
			num[now]=num[fail[now]]+1;
		}
		lst=nxt[Target][tmp];
		res+=num[lst];
	}
} Tree;
int main()
{
	char kcuffuck[4005];
	int lastans=0,len;
	scanf("%s",kcuffuck);
	Tree.Reset();
//	for(int i=0; i<lastans; i++)kcuffuck[i]=getchar();
	len=strlen(kcuffuck);
	for(int i=0; i<len; i++)
	{
		Tree.Reset();
		res=0;
		for(int j=i; j<len; j++)
		{
			Tree.Add(kcuffuck[j]);
			ans[i][j]=res;
		}
	}
	int q;
	scanf("%d",&q);
	for(int i=1; i<=q; i++)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",ans[l-1][r-1]);
	}
}


#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;
struct node
{
	int son[26],len,fail;
}t[5000+10];
int n,q,tot,last;
char s[5000+10],ch[5000+10];
int ans[5000+10][5000+10],dep[5000+10];
void init()
{
	t[tot=1].len=-1;
	t[0].fail=t[1].fail=1;
}
void clear()
{
	memset(t,0,sizeof(t));
	memset(dep,0,sizeof(dep));
	init();
}
void expand(int ch,int n,char *s)
{
	int x=last;
	while(s[n-t[x].len-1]!=s[n])
		x=t[x].fail;
	if(!t[x].son[ch]){
		int v=++tot,k=t[x].fail;
		t[v].len=t[x].len+2;
		while(s[n-t[k].len-1]!=s[n])
			k=t[k].fail;
		t[v].fail=t[k].son[ch];
		t[x].son[ch]=v;
		dep[v]=dep[t[k].son[ch]]+1;
	}
	last=t[x].son[ch];
}
int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;i++){
		clear();
		for(int j=i;j<=n;j++)
			ch[j-i+1]=s[j];
		for(int j=i;j<=n;j++){
			expand(s[j]-'a',j-i+1,ch);
			ans[i][j]=ans[i][j-1]+dep[last];
		}
	}
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int l,r;
		scanf("%d%d",&l,&r);
		printf("%d\n",ans[l][r]);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
string s;
int l,r,n,len,cnt,ans;
int main(){
    cin>>s; len=s.length();
	scanf("%d",&n);
	while(n--){
		cnt=0;
		scanf("%d%d",&l,&r);
		if(l==r){
			puts("1");
			continue;
		}
		for(register int i=l;i<=r;i++){
			int head=i,tail=i;
			if(s[i]==s[i-1]&&s[i]!=s[i+1] || s[i]==s[i+1]&&s[i]!=s[i-1]) cnt++;
			while(s[head]==s[tail]  && tail<=r && head>=l){
			    cnt++,head--,tail++;
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxlen = 5010;
char s[maxlen];
int len;
int n;

int sum[maxlen];//回文串数量前缀和

int p[maxlen][maxlen];//表示某个区间是否是回文串
//Palindromes

int main(){
	scanf("%s", s + 1);
	len = strlen(s + 1);
	for(register int i = 1; i <= len; i ++){//预处理长度为 1 和 2 的回文串
		p[i][i] = 1;
		p[i][i + 1] = (s[i] == s[i + 1]);
	}
	for(register int i = 3; i <= len; i ++){//枚举长度
		for(register int l = 1; l + i - 1 <= len; l ++){//枚举左界
			int r = l + i - 1;//右界
			if(s[l] == s[r] && p[l + 1][r - 1]){//回文
				p[l][r] = 1;
			}

		}
	}
	for(register int i = 1; i <= len; i ++){
		for(register int j = 1; j <= len; j ++){//求二维前缀和
			p[i][j] = p[i][j] + p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1];
		}
	}

	scanf("%d", &n);

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


#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<stack>
#include<ctime>
#include<set>
#include<map>
#include<bitset>
#include<vector>
#include<climits>
#include<complex>
//#define LL __int128
#define LL long long
//#define int LL
#define LBT(x) x&(-x)
using namespace std;
inline int read()
{
	int gans=0,gflag=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		gflag|=(ch=='-');
		ch=getchar();
	}
	while('0'<=ch&&ch<='9')
	{
		gans=(gans<<1)+(gans<<3)+ch-'0';
		ch=getchar();
	}
	return gflag?-gans:gans;
}
inline void write(int x)
{
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10^48);
}
inline void writenum(int x,char ch)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	write(x);
	putchar(ch);
}
inline void writechar(char x,char ch)
{
	putchar(x);
	putchar(ch);
}
inline int Min(int x,int y)
{
	return x<y?x:y;
}
inline int Max(int x,int y)
{
	return x>y?x:y;
}
const int Mx=5e3;
const int M=26;
int q[Mx+5],F[Mx+5];
struct PT//Palindromic_Tree
{
	int cnt,tot,lst,nxt[Mx+5][M+5],flt[Mx+5][M+5],fil[Mx+5],ch[Mx+5],len[Mx+5],sum[Mx+5],num[Mx+5];
	inline int New(int x)
	{
		memset(nxt[cnt],0,sizeof(nxt[cnt]));
		len[cnt]=x;
		sum[cnt]=0;
		num[cnt]=0;
		return cnt++;
	}
	inline void Csh()
	{
		cnt=0;
		tot=0;
		lst=0;
		New(0);//0:0
		New(-1);//-1:1
		ch[0]=-1;
		fil[0]=fil[1]=1;
		int i;
		for(i=0;i<=M;i++)
		{
			flt[0][i]=flt[1][i]=1;
		}
	}
	inline int Get(int x)
	{
		if(ch[tot-len[x]-1]!=ch[tot])
		{
			return flt[x][ch[tot]];
		}
		return x;
	}
	inline int Add(int c)
	{
		int now,p;
		c-='a';
		ch[++tot]=c;
		p=Get(lst);
		if(!nxt[p][c])
		{
			now=New(len[p]+2);
			F[now]=len[now];
			fil[now]=nxt[Get(fil[p])][c];
			memcpy(flt[now],flt[fil[now]],sizeof(flt[now]));
			flt[now][ch[tot-len[fil[now]]]]=fil[now];
			nxt[p][c]=now;
			num[now]=num[fil[now]]+1;
		}
		lst=nxt[p][c];
		sum[lst]++;
		return num[now];
	}
}T;
int Q,len,lst,tt[Mx+5][Mx+5];
char ch[Mx+5];
int main()
{
	int i,j;
	scanf("%s",ch+1);
	len=strlen(ch+1);
	for(i=1;i<=len;i++)
	{
		T.Csh();
		for(j=i;j<=len;j++)
		{
			tt[i][j]=tt[i][j-1]+T.Add(ch[j]);
		}
	}
	Q=read();
	while(Q--)
	{
		i=read();
		j=read();
		writenum(tt[i][j],'\n');
	}
	return 0;
}
/*
caaaba
5
1 1
1 4
2 3
4 6
4 5
*/


#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010;
string s;
int q, l, r;
int tp[maxn][maxn];
int dp[maxn][maxn];
void init() {
	memset(tp, -1, sizeof tp);
	memset(dp, -1, sizeof dp);
}
int pre(int l, int r) {
	if (tp[l][r] >= 0) return tp[l][r];
	if (l >= r) return tp[l][r] = 1;
	if (s[l] == s[r] && pre(l + 1, r - 1)) return tp[l][r] = 1;
	else return tp[l][r] = 0;
}
int dfs(int l, int r) {
	if (dp[l][r] >= 0) return dp[l][r];
	if (l == r) return dp[l][r] = 1;
	if (l > r) return dp[l][r] = 0;
	else return dp[l][r] = dfs(l, r - 1) + dfs(l + 1, r) - dfs(l + 1, r - 1) + pre(l, r);
}
int main() {
	init();
	cin >> s;
	scanf("%d", &q);
	while (q--) {
		scanf("%d%d", &l, &r);
		printf("%d\n", dfs(--l, --r));
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#define re register
#define ull unsigned long long
#define maxn 5005
char S[maxn];
ull ha[maxn],sh[maxn];
ull pow[maxn];
ull base=233;
int dp[maxn][maxn];
int n,Q;
inline int check(int l,int r)
{
	int len=r-l+1;
	return ha[r]-ha[l-1]*pow[len]==sh[l]-sh[r+1]*pow[len];
}
inline int read()
{
	char c=getchar();
	int x=0;
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9')
		x=(x<<3)+(x<<1)+c-48,c=getchar();
	return x;
}
int main()
{
	scanf("%s",S+1);
	n=strlen(S+1);
	for(re int i=1;i<=n;i++)
		ha[i]=ha[i-1]*base+S[i];
	pow[0]=1;
	for(re int i=1;i<=n;i++)
		pow[i]=pow[i-1]*base;
	for(re int i=n;i;--i)
		sh[i]=sh[i+1]*base+S[i];
	for(re int i=1;i<=n;i++)
		dp[i][i]=1;
	for(re int i=1;i<n;i++)
		dp[i][i+1]=check(i,i+1)+dp[i][i]+dp[i+1][i+1];
	for(re int p=2;p<n;p++)
	{
		for(re int i=1;i+p<=n;i++)
			dp[i][i+p]=dp[i][i+p-1]+dp[i+1][i+p]-dp[i+1][i+p-1]+check(i,i+p);
	}
	Q=read();
	int x,y;
	while(Q--)
	{
		x=read(),y=read();
		printf("%d\n",dp[x][y]);
	}
	return 0;
}


#include<map>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=5e3+100;
struct PAM{
	int son[maxn][26],len[maxn],fa[maxn],siz[maxn],ans;
	int last,tot,n,L;
	char s[maxn];
	PAM(){last=tot=fa[0]=fa[1]=L=1;len[1]=-1;}
	int getfail(int x){
		while(s[L-len[x]-1]!=s[L]) x=fa[x];
		return x;
	}
	void extend(int x){
		int cur=getfail(last),p=son[cur][x];
		if(!p){
			p=++tot;
			len[p]=len[cur]+2;
			memset(son[p],0,sizeof(son[p]));
			fa[p]=son[getfail(fa[cur])][x];
			son[cur][x]=p;
			siz[p]=siz[fa[p]]+1;
		}
		ans+=siz[p];
		last=p;
	}
	void clear(){
		last=tot=fa[0]=fa[1]=L=1;len[1]=-1;
		memset(son[0],0,sizeof(son[0]));
		memset(son[1],0,sizeof(son[1]));
		ans=0;
	}
}pam;
int n,ans[maxn][maxn];
char s[maxn];
int main(){
//	freopen(".in","r",stdin);
	scanf("%s",s+1),n=strlen(s+1);
	for(int i=1;i<=n;i++){
		pam.clear();
		pam.n=n-i+1;
		for(int j=i;j<=n;j++) pam.s[j-i+1]=s[j];
		for(int j=i;j<=n;j++){
			pam.extend(s[j]-'a'),pam.L++;
			ans[i][j]=pam.ans;
		}
	}
	int t,x,y;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&x,&y);
		printf("%d\n",ans[x][y]);
	}
	return 0;
}


/*
区间dp
https://blog.csdn.net/qq_26122039/article/details/51226209
dp[i][j]表示左端点为i，右端点为j的区间的回文子串数。
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
string str;
int len, is[5050][5050], dp[5050][5050], n;
void huiwen(int l, int r)//预处理所有以l、r为中心的回文子串
{
	while (l>=1 && r<=len)
	{
		if (str[l-1]==str[r-1]) is[l][r]=1;
		else break;
		l--;
		r++;
	}
}
int main()
{
	cin>>str;
	len=str.length();
	for (int i=1; i<=len; i++)//枚举回文串中心字符
	{
		huiwen(i, i);//奇数回文子串
		huiwen(i, i+1);//偶数回文子串
	}
	for (int i=1; i<=len; i++) dp[i][i]=1;//是len！字符串长度！不要手残写n
	for (int i=1; i<=len; i++)//区间长度
		for (int j=1; j<=len; j++)//左端点
		{
			dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1]+is[i][j];//容斥原理
		}

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


#include<bits/stdc++.h>
using namespace std;
typedef int  ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()

int main()
{
    ios;
    string s ;
    cin >> s ;
    ll n = s.length();
    ll dp[5005][5005];
ll p[5005][5005];
    memset( dp , 0 , so dp );
    memset( p , 0 , so p);
    ///cerr<<s<<endl ;
    ll q ;
    cin >> q ;
     for(ll i =0 ; i<n;++i){
        dp[i][i] = 1;
        p[i][i] = 1 ;
    }
    for(ll i = 0 ; i<n-1 ;++i){
        if(s[i] == s[i+1]) {
                dp[i][i+1] = 3;
             p[i][i+1] = 1 ;
        }
        else {
                dp[i][i+1] = 2;
        p[i][i+1] = 0 ;
        }
    }
    for(ll len = 3 ; len <= n ; ++len){
        for(ll i = 0 ; i<=n-len ; ++i){
            ll j = i + len -1;
            if(s[i] != s[j] ) p[i][j] = 0 ;
            else if(s[i] == s[j]){
                if(p[i+1][j-1] == 1 ){
                    p[i][j] = 1 ;
                }
                else p[i][j] = 0 ;
            }
             if(p[i][j] == 0){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = 1 + dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
        }
        }

    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        --from ;
        --to ;
        cout << dp[from][to]<<endl;
        cf;
        continue;
    }
    return 0  ;
}


#include<bits/stdc++.h>
using namespace std;
typedef int  ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()
ll dp[5005][5005];
ll p[5005][5005];
int main()
{
    ios;
    string s ;
    cin >> s ;
    ll n = s.length();

    memset( dp , 0 , so dp );
    memset( p , 0 , so p);
    ///cerr<<s<<endl ;
    ll q ;
    cin >> q ;
     for(ll i =0 ; i<n;++i){
        dp[i][i] = 1;
        p[i][i] = 1 ;
    }
    for(ll i = 0 ; i<n-1 ;++i){
        if(s[i] == s[i+1]) {
                dp[i][i+1] = 3;
             p[i][i+1] = 1 ;
        }
        else {
                dp[i][i+1] = 2;
        p[i][i+1] = 0 ;
        }
    }
    for(ll len = 3 ; len <= n ; ++len){
        for(ll i = 0 ; i<=n-len ; ++i){
            ll j = i + len -1;
            if(s[i] != s[j] ) p[i][j] = 0 ;
            else if(s[i] == s[j]){
                if(p[i+1][j-1] == 1 ){
                    p[i][j] = 1 ;
                }
                else p[i][j] = 0 ;
            }
             if(p[i][j] == 0){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = 1 + dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
        }
        }

    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        --from ;
        --to ;
        cout << dp[from][to]<<endl;
        cf;
        continue;
    }
    return 0  ;
}


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

const int N = 5e3 + 5;
bool isPal[N][N];
int dp[N][N];
char s[N];

int main() {
	scanf("%s", &s);
	int n = strlen(s);
	for (int i = n - 1; ~i; i--)
		for (int j = i; j < n; j++) {
			isPal[i][j] = (s[i] == s[j] && (i + 1 >= j || isPal[i + 1][j - 1]));
			dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1] + isPal[i][j];
		}

	int q;
	scanf("%d", &q);
	while (q--) {
		int l, r;
		scanf("%d%d", &l, &r);
		printf("%d\n", dp[--l][--r]);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()
int main()
{
    ios;
    string s ;
    cin >> s ;
    ll n = s.length();
    s.insert(0, "#");
    ///cerr<<s<<endl ;
    ll q ;
    cin >> q ;
    ll dp[n+2][n+2];
    memset(dp , 0 , so dp );
    for(ll i = 1 ; i<=n;++i){
        dp[i][i] = 1;
    }
    for(ll i = 1 ; i<n ;++i){
        if(s[i] == s[i+1]) dp[i][i+1] = 3;
        else dp[i][i+1] = 2;
    }
    for(ll len =3 ;len<= n ;++len){
        for(ll i = 1; i<=n-len+1 ; ++i){
            ll j = i+len-1;
            if(s[i]!=s[j]){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = 1 + dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
        }
    }
    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        cout << dp[from][to]<<endl;
        cf;
        continue;
    }
    return 0  ;
}


#include <iostream>

using namespace std;

const int MAXN = 5000 + 10;

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

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


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()
int main()
{
    ios;
    string s ;
    cin >> s ;
    ll n = s.length();
    s.insert(0, "#");
    ///cerr<<s<<endl ;
    ll q ;
    cin >> q ;
    ll dp[n+2][n+2];
    memset(dp , 0 , so dp );
    for(ll i = 1 ; i<=n;++i){
        dp[i][i] = 1;
    }
    for(ll i = 1 ; i<n ;++i){
        if(s[i] == s[i+1]) dp[i][i+1] = 3;
        else dp[i][i+1] = 2;
    }
    for(ll len =3 ;len<= n ;++len){
        for(ll i = 1; i<=n-len+1 ; ++i){
            ll j = i+len-1;
            if(s[i]!=s[j]){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = 1 + dp[i][j-1] + dp[i+1][j] ;
        }
    }
    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        cout << dp[from][to]<<endl;
        cf;
        continue;
    }
    return 0  ;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()
int main()
{
    ios;
    string s ;
    cin >> s ;
    ll n = s.length();
    s.insert(0, "#");
    ///cerr<<s<<endl ;
    ll q ;
    cin >> q ;
    ll dp[n+2][n+2];
    memset(dp , 0 , so dp );
    for(ll i = 1 ; i<=n;++i){
        dp[i][i] = 1;
    }
    for(ll i = 1 ; i<n ;++i){
        if(s[i] == s[i+1]) dp[i][i+1] = 3;
        else dp[i][i+1] = 2;
    }
    for(ll len =3 ;len<= n ;++len){
        for(ll i = 1; i<=n-len+1 ; ++i){
            ll j = i+len-1;
            if(s[i]!=s[j]){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = 1 + dp[i][j-1] + dp[i+1][j];
        }
    }
    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        cout << dp[from][to]<<endl;
        cf;
        continue;
    }
    return 0  ;
}


#include<bits/stdc++.h>
using namespace std;
typedef int  ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()

int main()
{

    ll dp[5005][5005];
ll p[5005][5005];
char s[5005];
    scanf("%s", &s);
	int n = strlen(s);

    memset( dp , 0 , so dp );
    memset( p , 0 , so p);
    ///cerr<<s<<endl ;
    ll q ;
    scanf("%d", &q);
     for(ll i =0 ; i<n;++i){
        dp[i][i] = 1;
        p[i][i] = 1 ;
    }
    for(ll i = 0 ; i<n-1 ;++i){
        if(s[i] == s[i+1]) {
                dp[i][i+1] = 3;
             p[i][i+1] = 1 ;
        }
        else {
                dp[i][i+1] = 2;
        p[i][i+1] = 0 ;
        }
    }
    for(ll len = 3 ; len <= n ; ++len){
        for(ll i = 0 ; i<=n-len ; ++i){
            ll j = i + len -1;
            if(s[i] != s[j] ) p[i][j] = 0 ;
            else if(s[i] == s[j]){
                if(p[i+1][j-1] == 1 ){
                    p[i][j] = 1 ;
                }
                else p[i][j] = 0 ;
            }
             if(p[i][j] == 0){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = 1 + dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
        }
        }

    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        --from ;
        --to ;
        cout << dp[from][to]<<"\n";
        cf;
        continue;
    }
    return 0  ;
}


#include<bits/stdc++.h>
using namespace std;
typedef int  ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()

int main()
{
    ios;
    ll dp[5005][5005];
ll p[5005][5005];
    string s ;
    cin >> s ;
    ll n = s.length();

    memset( dp , 0 , so dp );
    memset( p , 0 , so p);
    ///cerr<<s<<endl ;
    ll q ;
    cin >> q ;
     for(ll i =0 ; i<n;++i){
        dp[i][i] = 1;
        p[i][i] = 1 ;
    }
    for(ll i = 0 ; i<n-1 ;++i){
        if(s[i] == s[i+1]) {
                dp[i][i+1] = 3;
             p[i][i+1] = 1 ;
        }
        else {
                dp[i][i+1] = 2;
        p[i][i+1] = 0 ;
        }
    }
    for(ll len = 3 ; len <= n ; ++len){
        for(ll i = 0 ; i<=n-len ; ++i){
            ll j = i + len -1;
            if(s[i] != s[j] ) p[i][j] = 0 ;
            else if(s[i] == s[j]){
                if(p[i+1][j-1] == 1 ){
                    p[i][j] = 1 ;
                }
                else p[i][j] = 0 ;
            }
             if(p[i][j] == 0){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = 1 + dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
        }
        }

    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        --from ;
        --to ;
        cout << dp[from][to]<<"\n";
        cf;
        continue;
    }
    return 0  ;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()
int main()
{
    ios;
    string s ;
    cin >> s ;
    ll n = s.length();
    s.insert(0, "#");
    ///cerr<<s<<endl ;
    ll q ;
    cin >> q ;
    ll dp[n+2][n+2];
    memset(dp , 0 , so dp );
    for(ll i = 1 ; i<=n;++i){
        dp[i][i] = 1;
    }
    for(ll i = 1 ; i<n ;++i){
        if(s[i] == s[i+1]) dp[i][i+1] = 3;
        else dp[i][i+1] = 2;
    }
    for(ll len =3 ;len<= n ;++len){
        for(ll i = 1; i<=n-len+1 ; ++i){
            ll j = i+len-1;
            if(s[i]!=s[j]){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
        }
    }
    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        cout << dp[from][to]<<endl;
        cf;
        continue;
    }
    return 0  ;
}


#include<bits/stdc++.h>
using namespace std;
typedef int  ll;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define so sizeof
#define cf cout.flush()
ll dp[5005][5005];
ll p[5005][5005];
int main()
{
    ios;
    string s ;
    cin >> s ;
    ll n = s.length();

    memset( dp , 0 , so dp );
    memset( p , 0 , so p);
    ///cerr<<s<<endl ;
    ll q ;
    cin >> q ;
    for(ll len = 1 ; len <= n ; ++len){
        for(ll i = 0 ; i<=n-len ; ++i){
            if(len == 1 ){
                dp[i][i] = 1;
                p[i][i] = 1 ;
                continue;
            }
            else if(len == 2){
                if(s[i] == s[i+1]) {
                dp[i][i+1] = 3;
             p[i][i+1] = 1 ;
        }
        else {
                dp[i][i+1] = 2;
        p[i][i+1] = 0 ;
        }
         continue;
            }

            ll j = i + len -1;
            if(s[i] != s[j] ) p[i][j] = 0 ;
            else if(s[i] == s[j]){
                if(p[i+1][j-1] == 1 ){
                    p[i][j] = 1 ;
                }
                else p[i][j] = 0 ;
            }
             if(p[i][j] == 0){
                dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
            }
            else
            dp[i][j] = 1 + dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
        }
        }

    while(q--)
    {
        ll from , to ;
        cin >> from >> to ;
        --from ;
        --to ;
        cout << dp[from][to]<<endl;
        cf;
        continue;
    }
    return 0  ;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<ctime>
using namespace std;
char s[5200],ch;
int len,i,j,k,m,a,b;
int dp[5200][5200];
bool c[5200][5200];
void Output(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 read(){
	ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	int tem=ch-'0';ch=getchar();
	while(ch>='0'&&ch<='9'){
		tem=tem*10+ch-'0';
		ch=getchar();
	}
	return tem;
}
int main(){
	ch=getchar();
	while(ch>='a'&&ch<='z'){
		s[++len]=ch;ch=getchar();
	}
	for(i=1;i<=len;++i){
		c[i][i]=1;
		c[i][i-1]=1;
		dp[i][i]=1;
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			c[j][k]=c[j+1][k-1]&&(s[j]==s[k]);
		}
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			dp[j][k]=c[j][k]+dp[j+1][k]+dp[j][k-1]-dp[j+1][k-1];
		}
	}
	m=read();
	for(i=1;i<=m;++i){
		a=read();b=read();
		Output(dp[a][b]);
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<ctime>
using namespace std;
char s[5200];
long long len,i,j,k,m,a,b;
long long dp[5200][5200];
bool c[5200][5200];
int main(){
    scanf("%s",s+1);s[0]='0';
    len=strlen(s)-1;
    for(i=1;i<=len;++i){
        c[i][i]=1;
        c[i][i-1]=1;
        dp[i][i]=1;
    }
    for(i=1;i<=len;++i){
        for(j=1;j<=len;++j){
            k=i+j;if(k>len)break;
            c[j][k]=c[j+1][k-1]&&(s[j]==s[k]);
        }
    }
    for(i=1;i<=len;++i){
        for(j=1;j<=len;++j){
            k=i+j;if(k>len)break;
            dp[j][k]=c[j][k]+dp[j+1][k]+dp[j][k-1]-dp[j+1][k-1];
        }
    }
    scanf("%I64d",&m);
    for(i=1;i<=m;++i){
        scanf("%I64d%I64d",&a,&b);
        printf("%I64d\n",dp[a][b]);
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<ctime>
using namespace std;
char s[5555];
long long len,i,j,k,m,a,b;
long long dp[5555][5555];
bool c[5555][5555];
int main(){
	scanf("%s",s+1);s[0]='0';
	len=strlen(s)-1;
	for(i=1;i<=len;++i){
		c[i][i]=1;
		c[i][i-1]=1;
		dp[i][i]=1;
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			c[j][k]=c[j+1][k-1]&&(s[j]==s[k]);
		}
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			dp[j][k]=c[j][k]+dp[j+1][k]+dp[j][k-1]-dp[j+1][k-1];
		}
	}
	scanf("%I64d",&m);
	for(i=1;i<=m;++i){
		scanf("%I64d%I64d",&a,&b);
		printf("%I64d\n",dp[a][b]);
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<ctime>
using namespace std;
char s[5200],ch;
int len,i,j,k,m,a,b;
int dp[5200][5200];
bool c[5200][5200];
void Output(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 read(){
	ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	int tem=ch-'0';ch=getchar();
	while(ch>='0'&&ch<='9'){
		tem=tem*10+ch-'0';
	}
	return tem;
}
int main(){
	ch=getchar();
	while(ch>='a'&&ch<='z'){
		s[++len]=ch;ch=getchar();
	}
	for(i=1;i<=len;++i){
		c[i][i]=1;
		c[i][i-1]=1;
		dp[i][i]=1;
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			c[j][k]=c[j+1][k-1]&&(s[j]==s[k]);
		}
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			dp[j][k]=c[j][k]+dp[j+1][k]+dp[j][k-1]-dp[j+1][k-1];
		}
	}
	m=read();
	for(i=1;i<=m;++i){
		a=read();b=read();
		Output(dp[a][b]);
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<ctime>
using namespace std;
char s[5200];
int len,i,j,k,m,a,b;
int dp[5200][5200];
bool c[5200][5200];
void Output(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(){
	scanf("%s",s+1);s[0]='0';
	len=strlen(s)-1;
	for(i=1;i<=len;++i){
		c[i][i]=1;
		c[i][i-1]=1;
		dp[i][i]=1;
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			c[j][k]=c[j+1][k-1]&&(s[j]==s[k]);
		}
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			dp[j][k]=c[j][k]+dp[j+1][k]+dp[j][k-1]-dp[j+1][k-1];
		}
	}
	scanf("%d",&m);
	for(i=1;i<=m;++i){
		scanf("%d%d",&a,&b);
		Output(dp[a][b]);
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<ctime>
using namespace std;
char s[5200];
int len,i,j,k,m,a,b;
int dp[5200][5200];
bool c[5200][5200];
int main(){
	scanf("%s",s+1);s[0]='0';
	len=strlen(s)-1;
	for(i=1;i<=len;++i){
		c[i][i]=1;
		c[i][i-1]=1;
		dp[i][i]=1;
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			c[j][k]=c[j+1][k-1]&&(s[j]==s[k]);
		}
	}
	for(i=1;i<=len;++i){
		for(j=1;j<=len;++j){
			k=i+j;if(k>len)break;
			dp[j][k]=c[j][k]+dp[j+1][k]+dp[j][k-1]-dp[j+1][k-1];
		}
	}
	scanf("%d",&m);
	for(i=1;i<=m;++i){
		scanf("%d%d",&a,&b);
		printf("%d\n",dp[a][b]);
	}
	return 0;
}


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

    return 0;
}


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

    return 0;
}


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

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

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

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


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

    return 0;
}


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

    return 0;
}


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

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

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

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

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

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

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

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

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


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

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

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

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


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

    return 0;
}


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

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

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

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