#include <bits/stdc++.h>
using namespace std;
const int maxn = 5001;
char s[maxn];
int ispal[maxn][maxn];
int prec[maxn][maxn];
int main() {
  while (scanf("%s", s) >= 1) {
    int n = strlen(s);
    memset(ispal, 0, sizeof(ispal));
    for (int len = 0; len <= n; len++) {
      for (int i = 0; i <= n - len; i++) {
        ispal[i][i + len] =
            (len <= 1 ? 1
                      : ispal[i + 1][i + len - 1] && (s[i] == s[i + len - 1]));
        prec[i][i + len] = (len ? prec[i][i + len - 1] : 0) +
                           (len ? prec[i + 1][i + len] : 0) -
                           (len >= 2 ? prec[i + 1][i + len - 1] : 0) +
                           (len ? ispal[i][i + len] : 0);
      }
    }
    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      int l, r;
      scanf("%d%d", &l, &r);
      --l;
      printf("%d\n", prec[l][r]);
    }
  }
  return 0;
}
