#include <bits/stdc++.h>   using namespace std;   int cnt[5005][5005];   int len;   char st[5005];   bool palin[5005][5005];   void updateFreq(int r, int c) {     int cc;     while (r <= len) {       cc = c;       while (cc <= len) {         cnt[r][cc]++;         cc += (cc & (-cc));       }       r += (r & (-r));     }   }   int readFreq(int r, int c) {     int ret = 0;     int cc;     while (r > 0) {       cc = c;       while (cc > 0) {         ret += cnt[r][cc];         cc -= (cc & (-cc));       }       r -= (r & (-r));     }     return ret;   }   int main() {     scanf( %s , st + 1);     len = strlen(st + 1);     int q;     scanf( %d , &q);     for (int d = 0, lets_stop_here = (int)len + 1; d < lets_stop_here; d++) {       for (int i = 1, lets_stop_here = (int)len; i <= lets_stop_here; i++) {         int j = i + d;         if (j > len) break;         if (i == j)           palin[i][j] = true;         else if (j - i == 1)           palin[i][j] = st[i] == st[j] ? true : false;         else if (palin[i + 1][j - 1])           palin[i][j] = st[i] == st[j] ? true : false;         else           palin[i][j] = false;         if (palin[i][j]) updateFreq(i, j);       }     }     while (q--) {       int l, r;       scanf( %d %d , &l, &r);       int ans = readFreq(r, r) - readFreq(r, l - 1) - readFreq(l - 1, r) +                 readFreq(l - 1, l - 1);       printf( %d n , ans);     }     return 0;   }   